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
:
<?php
phpinfo();
?>
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;
server_name myphpsite.com www.myphpsite.com;
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';
FLUSH PRIVILEGES;
EXIT;
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 http://myphpsite.com
. You should see the PHP information page.
Conclusion
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.