Skip to main content

PHP-FPM Configuration and Syntax

This document contains examples and syntax for setting up PHP-FPM.

Basic PHP-FPM Pool Configuration

PHP-FPM pool files are typically located in /etc/php/7.x/fpm/pool.d/. Here is an example pool configuration:

[example]
user = www-data
group = www-data

listen = /run/php/php8.4-fpm-example.sock

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

chdir = /

Common PHP-FPM Configuration Directives

Listening on a Specific Port

listen = 127.0.0.1:9000

Listening on a Unix Socket

listen = /run/php/php8.4-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Process Manager Settings

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

pm Options:

  • static: A fixed number of child processes.
  • dynamic: Spawns child processes based on demand.
  • ondemand: Starts child processes only when requested.

Logging Settings

error_log = /var/log/php8.4-fpm.log
access.log = /var/log/php8.4-fpm.access.log

Security Settings

security.limit_extensions = .php .php3 .php4 .php5 .php7

Environment Variables

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

Nginx and PHP-FPM Integration

Here is how to configure Nginx to work with PHP-FPM:

server {
listen 80;
server_name example.com;

root /var/www/example.com/html;
index index.php index.html;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param PHP_VALUE "
error_log=/home/testphpsite/logs/php/error.log;
memory_limit=512M;
max_execution_time=60;
max_input_time=60;
max_input_vars=10000;
post_max_size=64M;
upload_max_filesize=64M;
date.timezone=UTC;
display_errors=off;";

}
}

Apache2 and PHP-FPM Integration

This document provides step-by-step instructions for integrating PHP-FPM with Apache2, including configuration examples and required commands.


Apache2 Virtual Host Configuration

To integrate PHP-FPM with Apache2, you need to configure a VirtualHost file.

Example Configuration:

<VirtualHost *:80>
ServerName example.com

DocumentRoot /var/www/example.com/html
DirectoryIndex index.php index.html

<Directory /var/www/example.com/html>
AllowOverride All
Require all granted
</Directory>

<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.4-fpm.sock|fcgi://localhost/"
ProxyErrorOverride On
ProxySet timeout=60
</FilesMatch>

ErrorLog /home/testphpsite/logs/apache2/error.log
CustomLog /home/testphpsite/logs/apache2/access.log combined
</VirtualHost>

Required Commands

Follow these commands to set up and enable PHP-FPM with Apache2.

sudo a2enmod proxy_fcgi setenvif

1. Enable PHP-FPM Configuration

Enable the PHP-FPM configuration for Apache2:

sudo a2enconf php8.4-fpm

2. Restart Apache2

Restart Apache2 to apply the changes:

sudo systemctl restart apache2

3. Verify Configuration

Check Apache2's configuration for syntax errors:

sudo apachectl configtest
  • Output should be Syntax OK.

PHP-FPM Status Page (Recommeded for development)

Enable the status page for PHP-FPM by adding the following to your pool configuration:

pm.status_path = /status

Then, in Nginx:

location ~ ^/(status|ping)$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.4-fpm.sock;
allow 127.0.0.1;
deny all;
}

Access the status page at http://yourdomain.com/status.

Advanced Configuration: Limits and Timeouts

Maximum Execution Time

request_terminate_timeout = 30s

Maximum Input Size

php_admin_value[memory_limit] = 128M
php_admin_value[post_max_size] = 16M
php_admin_value[upload_max_filesize] = 8M

This document covers the essential PHP-FPM configuration settings. Adjust them based on your specific application and server requirements.