How to enable and configure PHP-FPM pools in Nginx?

To enable and configure PHP-FPM pools in Nginx, follow these steps:

  1. Install PHP-FPM: Ensure that PHP-FPM is installed on your server. You can install it using your package manager. For example, on Ubuntu, you can use the command sudo apt install php-fpm.

  2. Enable PHP-FPM: Once PHP-FPM is installed, it is generally enabled and started automatically. However, you can use the following command to ensure it is running: sudo systemctl enable php-fpm (on systemd-based systems).

  3. Configure PHP-FPM Pools: PHP-FPM allows you to create separate pools for different websites or applications. Each pool has its own configuration file. By default, PHP-FPM is configured with a "www" pool. You can edit the pool's configuration file located in the /etc/php/{version}/fpm/pool.d/ directory. For example, the default www pool's config file is /etc/php/{version}/fpm/pool.d/www.conf.

  4. Configure the Nginx Server Block: Now, you need to configure the Nginx server block to use PHP-FPM. Open your website's server block configuration file located in the /etc/nginx/sites-available/ directory. For example, sudo nano /etc/nginx/sites-available/example.com.

  5. Add the necessary directives to the server block:

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

    Replace {version} with your PHP version (e.g., 7.4).

  6. Save the changes and exit the file.

  7. Enable the Server Block: Create a symbolic link to your server block's configuration file in the /etc/nginx/sites-enabled/ directory using the following command: sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

  8. Test and Reload Nginx Configuration: Ensure that your Nginx configuration is valid by running sudo nginx -t. If the test is successful, reload Nginx configuration using sudo systemctl reload nginx (on systemd-based systems).

That's it! Your PHP-FPM pools are now enabled and configured in Nginx. Repeat steps 4-8 for each website or application that needs its own PHP-FPM pool.