How to Configure Nginx to host a PHP site with a Database

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM


$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM


$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

In this guide, we’ll walk through the process of configuring Nginx to host a PHP website that interacts with a database, using MySQL or MariaDB as an example.

Step 1: Create or Upload PHP Website Files

You will need to create a directory for your PHP website and place your PHP files in it. For example:

sudo mkdir -p /var/www/myphpsite
sudo nano /var/www/myphpsite/index.php

Optionally, add a simple PHP script to index.php:


Step 2: Configure Nginx

If you don’t have Nginx installed and running on your server, use the following command:

sudo apt-get install nginx

The above command will install Nginx for you, and you can check its status using the following command:

sudo systemctl status nginx

If Nginx is not running, you can start it using the following command:

  sudo systemctl start nginx

Install and configure PHP for Nginx

Hosting a PHP website requires a couple of associated PHP packages to be installed. We can achieve this using the following command:

sudo apt-get install php php-fpm php-mysql

The above command will install PHP, the PHP FastCGI Process Manager, and the MySQL extension for PHP.

We need to set up PHP to work with the Nginx server, which can be done by editing the php-fpm configuration file. Let’s do it using the following command:

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

Now, let’s make sure we update the settings in the file as follows:

user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock

These settings enable PHP to run in the same user and group as Nginx while listening on the correct socket.

So, let’s save the changes we’ve made to the file and then restart PHP-FPM

sudo systemctl restart php8.1-fpm

Create the Nginx Server Blocks

Create an Nginx server block configuration file for your PHP site:

sudo nano /etc/nginx/sites-available/myphpsite

Add the following configuration:

server {
    listen 80;

    root /var/www/myphpsite;
    index index.php index.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

Enable the site by creating a symbolic link:

sudo ln -s /etc/nginx/sites-available/myphpsite /etc/nginx/sites-enabled/

Step 3: Configure Database

You need to first install the Database server. You choose to install either MYSQL or MariaDB server

If you want to use the MSQL server, use the following command:

sudo apt-get install mysql-server

Alternatively, if you prefer to use MariaDB other than MYSQL, use the following command to install it:

sudo apt-get install mariadb-server

Create the Database for PHP

Create a MySQL or MariaDB database for your PHP website. Replace myphpsite_db, myphpsite_user, and your_password with your desired values. Paste the following commands one by one on the terminal:

mysql -u root -p

CREATE DATABASE myphpsite_db;
CREATE USER 'myphpsite_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON myphpsite_db.* TO 'myphpsite_user'@'localhost';

Step 4: Test the Configuration

Test your Nginx configuration for syntax errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 5: Access Your PHP Website

Open your web browser and navigate to You should see the PHP information page.


Configuring Nginx to host a PHP website with a database involves creating a server block, configuring PHP-FPM, and setting up the necessary database. Following these steps will help you efficiently host your PHP applications with Nginx, providing a secure and performant web hosting solution.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.