Optimize PHP-FPM performance with memory tuning and connection pooling for high-traffic websites

Beginner 25 min Apr 03, 2026 17 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Configure PHP-FPM with optimized memory pools, process management, and connection limits to handle high-traffic websites efficiently. Learn production-grade tuning strategies for maximum performance.

Prerequisites

  • Root or sudo access
  • Basic knowledge of web server concepts
  • 1GB+ available RAM

What this solves

PHP-FPM (FastCGI Process Manager) is the recommended way to run PHP applications in production, but default configurations often perform poorly under high traffic. This tutorial shows you how to optimize PHP-FPM memory usage, configure process pools effectively, and implement connection limits to prevent server overload while maintaining fast response times.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest PHP-FPM version with security patches.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install PHP-FPM and essential extensions

Install PHP-FPM with commonly needed extensions for web applications. This includes OpCache for bytecode caching and essential database drivers.

sudo apt install -y php8.3-fpm php8.3-opcache php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring php8.3-xml php8.3-zip
sudo dnf install -y php-fpm php-opcache php-mysqlnd php-curl php-gd php-mbstring php-xml php-zip

Configure PHP memory limits and execution settings

Optimize PHP's core memory settings in php.ini. These settings prevent memory exhaustion while allowing sufficient resources for complex applications.

memory_limit = 256M
max_execution_time = 60
max_input_time = 60
post_max_size = 64M
upload_max_filesize = 32M
max_file_uploads = 20
date.timezone = UTC
memory_limit = 256M
max_execution_time = 60
max_input_time = 60
post_max_size = 64M
upload_max_filesize = 32M
max_file_uploads = 20
date.timezone = UTC

Enable and optimize OpCache for bytecode caching

OpCache dramatically improves PHP performance by storing precompiled script bytecode in memory, eliminating the need to load and parse PHP files on each request.

opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.save_comments=1
opcache.enable_file_override=1
opcache.enable=1
opcache.memory_consumption=256
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=2
opcache.save_comments=1
opcache.enable_file_override=1

Configure PHP-FPM process pool settings

The process manager controls how PHP-FPM spawns and manages worker processes. Dynamic mode provides the best balance of performance and resource efficiency for high-traffic sites.

[www]
user = www-data
group = www-data
listen = /run/php/php8.3-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 1000
[www]
user = nginx
group = nginx
listen = /run/php-fpm/www.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 1000

Configure connection limits and timeouts

Set appropriate timeouts and connection limits to prevent hanging processes and ensure responsive behavior under load. These settings help PHP-FPM handle traffic spikes gracefully.

request_terminate_timeout = 120
rlimit_files = 4096
rlimit_core = 0

catch_workers_output = yes
decorate_workers_output = no
clear_env = no

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
request_terminate_timeout = 120
rlimit_files = 4096
rlimit_core = 0

catch_workers_output = yes
decorate_workers_output = no
clear_env = no

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

Configure PHP-FPM global settings

Set global PHP-FPM parameters that control logging, emergency restart behavior, and process limits. These settings ensure stability under high load conditions.

pid = /run/php/php8.3-fpm.pid
error_log = /var/log/php8.3-fpm.log
log_level = warning
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s
daemonize = yes
rlimit_files = 4096
rlimit_core = 0
events.mechanism = epoll
pid = /run/php-fpm/php-fpm.pid
error_log = /var/log/php-fpm/error.log
log_level = warning
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s
daemonize = yes
rlimit_files = 4096
rlimit_core = 0
events.mechanism = epoll

Create PHP-FPM status monitoring endpoint

Enable the PHP-FPM status page to monitor pool performance, active connections, and memory usage. This is essential for troubleshooting performance issues.

pm.status_path = /php-fpm-status
ping.path = /php-fpm-ping
ping.response = pong
pm.status_listen = 127.0.0.1:9001
pm.status_path = /php-fpm-status
ping.path = /php-fpm-ping
ping.response = pong
pm.status_listen = 127.0.0.1:9001

Optimize system limits for PHP-FPM

Configure system-level limits to ensure PHP-FPM can handle the configured number of processes and file descriptors without hitting OS limits.

www-data soft nofile 4096
www-data hard nofile 8192
www-data soft nproc 2048
www-data hard nproc 4096

For RHEL-based systems use 'nginx' instead of 'www-data'

nginx soft nofile 4096 nginx hard nofile 8192 nginx soft nproc 2048 nginx hard nproc 4096

Configure systemd service limits

Create systemd overrides to ensure PHP-FPM can use the configured resource limits. This prevents systemd from restricting PHP-FPM processes.

sudo mkdir -p /etc/systemd/system/php8.3-fpm.service.d
[Service]
LimitNOFILE=8192
LimitNPROC=4096
TasksMax=4096
[Service]
LimitNOFILE=8192
LimitNPROC=4096
TasksMax=4096

Enable and start PHP-FPM service

Reload systemd configuration and start PHP-FPM with the optimized settings. Enable the service to start automatically on system boot.

sudo systemctl daemon-reload
sudo systemctl enable --now php8.3-fpm
sudo systemctl status php8.3-fpm
sudo systemctl enable --now php-fpm
sudo systemctl status php-fpm

Verify your setup

Check that PHP-FPM is running correctly and verify the optimized configuration is active.

sudo systemctl status php8.3-fpm
sudo php-fpm8.3 -t
php -m | grep -i opcache
curl -s http://127.0.0.1:9001/php-fpm-status
sudo systemctl status php-fpm
sudo php-fpm -t
php -m | grep -i opcache
curl -s http://127.0.0.1:9001/php-fpm-status

Check PHP-FPM process information and memory usage:

ps aux | grep php-fpm
sudo ss -tuln | grep 9001
free -h

Performance monitoring and troubleshooting

Monitor PHP-FPM performance using the status endpoint and system tools. You can integrate this with Netdata for real-time monitoring or htop for process analysis.

# Monitor pool status
curl -s http://127.0.0.1:9001/php-fpm-status?full

Watch process spawning

watch -n 2 'ps aux | grep php-fpm | wc -l'

Monitor slow requests

tail -f /var/log/php*fpm.log

Check OpCache status

php -r "print_r(opcache_get_status());"

Monitor memory usage per process

ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | grep php-fpm
Note: For high-traffic sites with consistent load, consider using pm = static with pm.max_children set to your server's optimal process count. This eliminates the overhead of spawning/killing processes.

Common issues

SymptomCauseFix
502 Bad Gateway errorsSocket permission issuesCheck listen.owner/group matches web server user
Slow response timesToo few PHP-FPM processesIncrease pm.max_children and pm.start_servers
High memory usageMemory leaks or too many processesSet pm.max_requests lower (500-1000) to restart workers
Connection timeoutsLong-running scriptsIncrease request_terminate_timeout and max_execution_time
PHP-FPM won't startConfiguration syntax errorsRun sudo php-fpm -t to test configuration
OpCache not workingExtension not loadedVerify opcache.enable=1 in correct php.ini file

Next steps

Automated install script

Run this to automate the entire setup

#php-fpm #performance #memory-tuning #connection-pooling #web-server

Need help?

Don't want to manage this yourself?

We handle infrastructure for businesses that depend on uptime. From initial setup to ongoing operations.

Talk to an engineer