How to configure Nginx to work with PHP-FPM?

To configure Nginx to work with PHP-FPM, follow these steps:

  1. Install Nginx and PHP-FPM on your server.

    • On Ubuntu, you can use the following commands: sudo apt update sudo apt install nginx php-fpm
  2. Configure PHP-FPM settings:

    • Open the PHP-FPM configuration file using a text editor. On Ubuntu, the file is located at /etc/php/{PHP_VERSION}/fpm/pool.d/www.conf.
    • Find the listen directive and make sure it is set to the correct socket or IP address and port. By default, it may be set to listen = /run/php/php{PHP_VERSION}-fpm.sock. If you are using a different setup, you may need to adjust this value.
    • Save the changes and exit the editor.
  3. Configure Nginx to use PHP-FPM:

    • Open the Nginx configuration file for the site you want to enable PHP processing on. The file is typically located at /etc/nginx/sites-available/{SITE_NAME}.
    • Inside the server block, add the following location block to handle PHP files: location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php{PHP_VERSION}-fpm.sock; } Replace {PHP_VERSION} with the actual PHP version you are using.
    • Save the changes and exit the editor.
  4. Test the configuration and restart the services:

    • Check the Nginx configuration for any syntax errors: sudo nginx -t
    • If the configuration test is successful, restart Nginx and PHP-FPM services to apply the changes: sudo systemctl restart nginx sudo systemctl restart php{PHP_VERSION}-fpm Again, replace {PHP_VERSION} with the actual PHP version you are using.

Now Nginx should be configured to work with PHP-FPM. You can place your PHP files in the appropriate directory defined in the Nginx configuration file and access them via your domain or IP address.