Nginx and PHP-FPM Setup

Setting up an Nginx web server with PHP-FPM can be a bit daunting for those who are new to web server administration. In this blog post, I will provide a step-by-step guide on how to set up an Nginx web server with PHP-FPM on a Linux-based system (such as Ubuntu or CentOS).

Before we begin, it’s important to note that the process of setting up an Nginx web server with PHP-FPM will vary depending on the specific Linux distribution you are using. For the purpose of this tutorial, we will be using Ubuntu 20.04 LTS as an example.

Step 1: Install Nginx

The first step in setting up an Nginx web server with PHP-FPM is to install Nginx itself. To do this, we will use the apt package manager.

First, update the apt package manager’s local package index:

Copy codesudo apt update

Next, install Nginx by running the following command:

Copy codesudo apt install nginx

Once the installation is complete, you can verify that Nginx has been installed successfully by running the following command:

Copy codenginx -v

This should print the version number of Nginx that was installed.

Step 2: Install PHP and PHP-FPM

Next, we will install PHP and PHP-FPM. This can be done using the apt package manager as well.

To install PHP and PHP-FPM, run the following command:

Copy codesudo apt install php-fpm php-cli

This will install PHP and PHP-FPM, along with some other common PHP modules.

Once the installation is complete, you can verify that PHP and PHP-FPM have been installed successfully by running the following command:

Copy codephp --version

This should print the version number of PHP that was installed.

Step 3: Configure Nginx to Use PHP-FPM

Once Nginx and PHP-FPM have been installed, we need to configure Nginx to use PHP-FPM to process PHP requests.

To do this, open the Nginx configuration file in a text editor:

Copy codesudo nano /etc/nginx/nginx.conf

In this file, we need to make the following changes:

  • In the http block, add the following lines:
Copy codeindex index.php index.html;

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
  • In the server block, add the following line:
Copy codelocation / {
    try_files $uri $uri/ =404;
}

These changes will tell Nginx to use PHP-FPM to process PHP requests and to look for PHP files in the document root of your website.

Once you have made these changes, save the file and exit the text editor.

Step 4: Restart Nginx and PHP-FPM

Once you have made the necessary changes to the Nginx configuration file, you need to restart Nginx and PHP-FPM for the changes to take effect.

To restart Nginx, run the following command:

sudo system

You may also like...

Leave a Reply

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