How to install Nginx and PHP-FPM on a Linux server?

To install Nginx and PHP-FPM on a Linux server, you can follow the steps below:

  1. Update system packages:

    sudo apt update
  2. Install Nginx:

    sudo apt install nginx
  3. Start Nginx service:

    sudo systemctl start nginx
  4. Verify the installation by accessing your server's IP address in a browser. You should see the default Nginx welcome page.

  5. Install PHP-FPM:

    sudo apt install php-fpm
  6. Configure PHP-FPM:

    • Open the PHP-FPM configuration file in a text editor:

      sudo nano /etc/php/VERSION/fpm/php.ini

      Replace VERSION with the installed PHP version (e.g., 7.4), or find the appropriate path to the php.ini file.

    • Uncomment the cgi.fix_pathinfo line and set its value to 0:

      cgi.fix_pathinfo=0
    • Save and close the file.

  7. Restart PHP-FPM service:

    sudo systemctl restart phpVERSION-fpm

    Replace VERSION with the installed PHP version (e.g., 7.4).

  8. Configure Nginx to use PHP-FPM:

    • Open the Nginx default server block configuration file:

      sudo nano /etc/nginx/sites-available/default
    • Add the following location block inside the existing server block:

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

      Replace VERSION with the installed PHP version (e.g., 7.4).

    • Save and close the file.

  9. Check Nginx configuration syntax:

    sudo nginx -t
  10. If the syntax is OK, restart Nginx service:

    sudo systemctl restart nginx

Now, Nginx and PHP-FPM should be installed and configured on your Linux server. You can test PHP-FPM by creating an index.php file in the default Nginx document root directory (/var/www/html on Ubuntu) and accessing it in a browser.