Using PHP-FPM with user pools involves configuring separate PHP-FPM pools for different users or websites, allowing for isolation and tailored resource management. This is particularly useful in shared hosting environments or when running multiple applications with distinct requirements.
Steps to Configure PHP-FPM with User Pools:
Create Dedicated Users (if needed).
For each application or user requiring a separate PHP-FPM pool, ensure a dedicated system user account exists. This user will own the PHP-FPM processes and the application's files.
Code
sudo adduser application_user
Create PHP-FPM Pool Configuration Files.
Navigate to the PHP-FPM pool configuration directory (e.g., /etc/php/X.X/fpm/pool.d/, where X.X is your PHP version). Create a new configuration file for each pool (e.g., application_name.conf).
Code
sudo cp /etc/php/X.X/fpm/pool.d/www.conf /etc/php/X.X/fpm/pool.d/application_name.conf
sudo nano /etc/php/X.X/fpm/pool.d/application_name.conf
Configure the PHP-FPM Pool.
Edit the new pool configuration file, making the following key changes:
Pool Name:
Change the pool name from [www] to a unique name, e.g., [application_name].
User and Group:
Set the user and group directives to the dedicated user and group for this application.
Code
; In application_name.conf
[application_name]
user = application_user
group = application_user
Listen Address/Socket: Configure the listen directive to specify how the web server will communicate with this pool. This can be a Unix socket (recommended for local communication) or a TCP address/port.
Code
; For Unix socket
listen = /run/php/phpX.X-fpm.application_name.sock
; For TCP socket (if needed)
; listen = 127.0.0.1:9001
Process Management (Optional but Recommended).
Adjust pm (process manager), pm.max_children, pm.start_servers, pm.min_spare_servers, pm.max_spare_servers, pm.process_idle_timeout, and pm.max_requests according to the application's needs.
Configure Your Web Server (Nginx or Apache):
Nginx: In your Nginx virtual host configuration, direct requests for the specific application to its corresponding PHP-FPM pool using the fastcgi_pass directive.
Code
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/phpX.X-fpm.application_name.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Apache (with mod_proxy_fcgi): In your Apache virtual host configuration, use ProxyPassMatch to forward PHP requests to the correct PHP-FPM pool.
Code
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php/phpX.X-fpm.application_name.sock|fcgi://localhost/"
</FilesMatch>
Restart PHP-FPM and Web Server.
After making changes, restart both the PHP-FPM service and your web server to apply the new configurations.
Code
sudo systemctl restart phpX.X-fpm
sudo systemctl restart nginx # or apache2
By following these steps, you can effectively isolate PHP applications and manage their resources independently using PHP-FPM user pools.