Sling Academy
Home/PHP/How to Deploy a PHP Site to an Ubuntu Server

How to Deploy a PHP Site to an Ubuntu Server

Last updated: January 13, 2024

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.

Next Article: A few ways to host a PHP site for free

Previous Article: PHP error: Blank page, white screen of death

Series: Building Dynamic Web Pages with PHP

PHP

You May Also Like

  • Pandas DataFrame.value_counts() method: Explained with examples
  • Constructor Property Promotion in PHP: Tutorial & Examples
  • Understanding mixed types in PHP (5 examples)
  • Union Types in PHP: A practical guide (5 examples)
  • PHP: How to implement type checking in a function (PHP 8+)
  • Symfony + Doctrine: Implementing cursor-based pagination
  • Laravel + Eloquent: How to Group Data by Multiple Columns
  • PHP: How to convert CSV data to HTML tables
  • Using ‘never’ return type in PHP (PHP 8.1+)
  • Nullable (Optional) Types in PHP: A practical guide (5 examples)
  • Explore Attributes (Annotations) in Modern PHP (5 examples)
  • An introduction to WeakMap in PHP (6 examples)
  • Type Declarations for Class Properties in PHP (5 examples)
  • Static Return Type in PHP: Explained with examples
  • PHP: Using DocBlock comments to annotate variables
  • PHP: How to ping a server/website and get the response time
  • PHP: 3 Ways to Get City/Country from IP Address
  • PHP: How to find the mode(s) of an array (4 examples)
  • PHP: Calculate standard deviation & variance of an array