Dump the code
Best ressources to improve your Python Skills

Configuring PHP-FPM pools

Created 10 months ago
Posted By admin
4min read
PHP-FPM pools are a group of PHP processes or workers that are managed collectively. Each pool operates independently, and multiple pools can coexist on the same server. PHP-FPM pools allow for better resource management, isolation, and flexibility in handling PHP requests.

Configuring PHP-FPM pools involves creating or modifying pool configuration files. Each pool can have its own set of configuration parameters, allowing you to customize the behavior of PHP-FPM for different applications or websites. These pool configuration files are typically stored in the /etc/php/{version}/fpm/pool.d/ directory.

Let's go through the steps to configure a PHP-FPM pool:

1. Create a pool configuration file:
Start by creating a new pool configuration file. The convention is to create a file with a .conf extension in the pool.d directory. For example, if you're creating a pool for a website named "example," you might create a file named example.conf.

sudo nano /etc/php/8.2/fpm/pool.d/example.conf
Adjust the version number based on your PHP version.

2. Define pool configuration:
Inside the configuration file, define the pool-specific settings. Here's a basic example:

[example]
user = www-data
group = www-data
listen = /run/php/php8.2-fpm-example.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.start_servers = 2
pm.max_children = 5
pm.min_spare_servers = 1
pm.max_spare_servers = 3
  • [example] is the name of the pool. You can replace it with a name that makes sense for your application.
  • user and group specify the UNIX user and group under which the PHP processes will run.
  • listen defines the socket or IP address and port on which PHP-FPM will listen. PHP-FPM supports communication via Unix sockets or TCP/IP. Sockets are generally more efficient due to lower overhead. Choose the appropriate option based on your infrastructure.In this case, it's using a Unix socket.
  • pm (process manager) directives control the dynamic process management settings.
  • pm.start_servers defines the number of processes to start initially.
  • pm.max_children sets the maximum number of child processes.
  • pm.min_spare_servers and pm.max_spare_servers determine the minimum and maximum number of idle processes.
Adjust these settings based on your application's requirements and server resources.

3. Save the configuration file:
Save the changes and exit the text editor.
Additionally, you can inspect the status and settings of the pool by using the php-fpm command-line tool:

sudo php-fpm8.2 -t

4. Restart PHP-FPM
After creating or modifying a pool configuration file, you need to restart PHP-FPM to apply the changes.

sudo systemctl restart php8.2-fpm
 
5. Verify the pool configuration:
You can check the PHP-FPM status to see if the new pool is active and there are no syntax errors in the configuration.

sudo systemctl status php8.2-fpm

6. Test the configuration with NGINX:
Modify the fastcgi_pass with the new listen created in the pool.
listen = /run/php/php8.2-fpm-example.sock

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm-example.sock;  # Adjust the version number
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

Save the Nginx configuration file and test for syntax errors:

sudo nginx -t

If there are no errors, restart Nginx to apply the changes:

sudo systemctl restart nginx

Now create a simple PHP file in your web server's document root and access it through your web browser to ensure that PHP-FPM is processing PHP scripts correctly.

For example, create a file named info.php with the following content:

   <?php
   phpinfo();
Access this file in your browser (e.g., http://your-server/info.php) to see the PHP information page.

Remember to tailor the pool configuration settings to the specific requirements of your application, and consider creating separate pools for different websites or applications to isolate their environments and resources.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles