How to Deploy a PHP Site to an Ubuntu Server

Updated: January 13, 2024 By: Guest Contributor Post a comment

Introduction

Deploying a PHP site to an Ubuntu server is a multi-step procedure that entails setting up a web server with PHP, configuring databases, and transferring your website’s files to the server. This guide will walk you through the steps necessary for getting your PHP site running on an Ubuntu server.

Before we begin, let’s ensure you have the following prerequisites:

  • A running Ubuntu server (we will be using Ubuntu 20.04)
  • A non-root user with sudo privileges
  • A domain name pointed to your server’s public IP address (optional for local/testing setups)

Step-by-Step Guide

Step 1: Secure Your Server

Update your package lists:

sudo apt update
sudo apt upgrade

Set up a firewall using UFW (Uncomplicated Firewall) to ensure only certain services are accessible:

sudo ufw enable
sudo ufw allow OpenSSH
sudo ufw allow http
sudo ufw allow https

Step 2: Install Nginx, PHP, and Extensions

Install Nginx:

sudo apt install nginx

Install PHP and commonly required PHP extensions:

sudo apt install php-fpm php-mysql php-xml php-xmlrpc php-gd php-curl php-cli php-zip php-mbstring

Step 3: Configuring Nginx to Run PHP

Create a server block file for your site:

sudo nano /etc/nginx/sites-available/yourdomain.com

Insert the following block. Modify ‘root’ to point to your site’s directory:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Create a symbolic link to ‘sites-enabled’ to enable your site:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Test your configuration and restart Nginx:

sudo nginx -t
sudo systemctl restart nginx

Step 4: Setting Up the Database

Install MySQL:

sudo apt install mysql-server

Secure your installation:

sudo mysql_secure_installation

Log in to your database and create a database and user:

mysql -u root -p
CREATE DATABASE your_db_name;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_db_name.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
exit;

Step 5: Upload Your PHP Site

Upload files using SFTP or any secure method you prefer. Place your site files within the directory specified in your Nginx configuration (e.g., ‘/var/www/yourdomain’).

Step 6: Test the PHP Processing on your Web Server

Create a ‘info.php’ file in your site’s root directory:

echo "" | sudo tee /var/www/yourdomain/info.php

Visit http://yourdomain.com/info.php in your web browser. You should see the PHP information page. After testing, remember to remove the file:

sudo rm /var/www/yourdomain/info.php

Step 7: Configure SSL with Let’s Encrypt (Optional)

Install the ‘certbot’ package and the Nginx plug-in:

sudo apt install certbot python3-certbot-nginx

Run certbot and follow the prompts:

sudo certbot --nginx

Set up auto-renewal:

sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer

Conclusion

Congratulations! You’ve set up your PHP site on an Ubuntu server. Remember to back up regularly and monitor your server’s security.