nginx php php-fpm config

Configuring Nginx with PHP & PHP-FPM on Ubuntu

Before going ahead with this post, make sure that you have SSH access to your server and you've properly configured the system users and firewall to secure the server. It's best practice to use SSH keys for authentication instead of password, and to not use root user. 
Besides the configurations in this tutorial, you'll also have to update your domain name's settings from domain registrar's website to add an A record and point it to your server's IP address. You also need a basic knowledge of nano or vim editor to create & edit the config files on the server.

A Ubuntu server configured with Nginx and PHP is my first pick when setting up a dev or live environment for a web application. I’ve used this to run Larevel, Magento, WordPress, and custom PHP applications.

To, get started make sure you have Ubuntu 18.04 or later. For the purpose of this tutorial I’ll be using a DigitalOcean droplet and these configurations can vary slightly for other cloud platforms and may also require you to allow traffic from certain ports by updating network settings from that platform’s dashboard.

Next, the first thing you need to do is to make sure that Ubuntu’s advanced package tool(apt) is up to date. For that we’ll run the following command

sudo apt update -y

This can take some time to update the package index. Once this is done, we’ll install the Nginx server.

sudo apt install nginx

Now, check the ufw status and if it is “active” then allow traffic for Nginx.

sudo ufw status
sudo ufw allow 'Nginx HTTP'

At this point Nginx should be up and running and you should be able to view the default Nginx page if you visit your server’s IP address in a web browser. The IP address is the same that you used for the SSH connection. So, visiting http://youripaddress should show a “Welcome to Nginx” page.

Next, let’s install php-fpm which will tell Nginx to use PHP to process requests for .php pages.

sudo apt install php-fpm

To configure php-fpm with Nginx, we’ll edit the Nginx config file. The path of the Nginx config file can vary based on how Nginx was installed. For instance on Mac OS if Nginx was installed using homebrew the config file is located in “/opt/homebrew/etc/nginx/nginx.conf”. If you are following this tutorial then you can use following command to edit the config file.

sudo nano /etc/nginx/sites-available/your-hostname

“your-hostname” is the file name to identify which host is this configuration for. It will help to find the right config file when you have multiple hosts configured on a server. After running the above command, you’ll need the add the following configurations to that file. Update “your_domain” to the domain name that you’ll be using for this application (e.g mygreatapp.com). To make these updates I assume you have a basic understanding of using nano or vim.

server {
        listen 80;
        root /var/www/yourdomain/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name your_domain;

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

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

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

Once you have created this configuration, we can enable it by running the following command, which essentially builds a symbolic link to the sites-enabled directory.

sudo ln -s /etc/nginx/sites-available/your-hostname /etc/nginx/sites-enabled/

And disable default config file

sudo unlink /etc/nginx/sites-enabled/default

Test if the new configs are valid.

sudo nginx -t

If there are new issues in the config, go ahead and reload the Nginx configs with following command

sudo systemctl reload nginx

That is it, now PHP is fully configured with Nginx. To test it out you can create a php file in root directory for your host(/var/www/yourdomain/html) and then view that file in the browser by visiting the hostname.

Good luck and happy hacking.

Leave a Reply

Your email address will not be published. Required fields are marked *