Blog / Linux/ PHP-FPM Configuration and Optimization Guide for LNMP Environments (Latest Version)

PHP-FPM Configuration and Optimization Guide for LNMP Environments (Latest Version)

LNMP 环境中 PHP-FPM 配置优化指南( 最新版)

PHP-FPM Configuration File Location and Editing

The main PHP-FPM configuration file is typically located in one of the following paths, depending on your installation method and operating system:

  • Source compilation installation: /usr/local/php/etc/php-fpm.conf or /usr/local/php/etc/php-fpm.d/www.conf
  • Package manager installation (e.g., apt/yum): /etc/php/{version}/fpm/php-fpm.conf and /etc/php/{version}/fpm/pool.d/www.conf

You can edit it with any text editor, for example:

# Edit the main config file with vi
vi /usr/local/php/etc/php-fpm.conf
# Or edit the pool config file (more common)
vi /usr/local/php/etc/php-fpm.d/www.conf

Core Configuration Parameters and Optimization Guide

Below are explanations and optimization suggestions for key settings. Adjust them based on your server hardware (CPU, memory) and application load.

Process Manager (pm) Mode

This is the most critical setting affecting performance and resource usage.

  • pm = dynamic (Recommended): Dynamic process management. The number of child processes fluctuates between minimum and maximum values.
  • pm = static: Static process management. A fixed number of child processes, suitable for high-traffic servers with ample memory.
  • pm = ondemand: Processes start on demand. Suitable for low-traffic or memory-constrained environments, but introduces startup latency for incoming requests.

Process Number Settings for Dynamic Mode

For pm = dynamic, configure these parameters:

pm.max_children = 50        # Maximum child processes. Hard limit.
pm.start_servers = 5        # Number of child processes created on start.
pm.min_spare_servers = 2    # Minimum idle processes. New ones are created if below.
pm.max_spare_servers = 8    # Maximum idle processes. Excess ones are terminated.

Calculation advice: The value for pm.max_children is primarily limited by server memory. A simple estimation formula is:

pm.max_children ≈ Server Available Memory / Average Memory Usage per PHP Process

For example, on a 1GB server with a PHP process using ~50MB, pm.max_children could theoretically be 20. However, reserve memory for the system and other services (like Nginx, MySQL), so set a lower practical value.

Request and Timeout Settings

pm.max_requests = 500        # Restart each child after this many requests to prevent memory leaks.
request_terminate_timeout = 30s  # Maximum execution time per request. Increase (e.g., 60s) if 502 errors occur.
request_slowlog_timeout = 10s    # Slow request log threshold.
slowlog = /var/log/php-fpm/$pool-slow.log  # Slow log path.

Listening and Connection Settings

# Method 1: Unix Socket (Recommended for better performance when Nginx and PHP-FPM are on the same host)
listen = /run/php/php8.2-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660

# Method 2: TCP Socket (For when PHP-FPM and Nginx are on different hosts)
# listen = 127.0.0.1:9000
# listen.allowed_clients = 127.0.0.1  # Allowed IPs for TCP connections.

Logging and Status Monitoring

access.log = /var/log/php-fpm/$pool-access.log  # Access log
slowlog = /var/log/php-fpm/$pool-slow.log       # Slow log
pm.status_path = /status  # Enable status page
ping.path = /ping         # Health check endpoint
ping.response = pong

Configuration Example: 1 vCPU, 1GB Cloud Server

For a low-resource cloud server (e.g., 1 vCPU, 1GB RAM), here is a conservative, stable configuration suitable for small to medium WordPress or Laravel applications.

; Process Management
pm = dynamic
pm.max_children = 20          ; Based on memory estimate
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 500         ; Prevent memory leaks

; Listening (Unix Socket for performance)
listen = /run/php/php-fpm.sock
listen.owner = www
listen.group = www
listen.mode = 0660

; User and Group
user = www
group = www

; Timeouts and Logging
request_terminate_timeout = 30s  ; Prevent long-running scripts
request_slowlog_timeout = 5s     ; Log slow requests >5s
slowlog = /var/log/php-fpm/slow.log

; Resource Limits
rlimit_files = 4096              ; Increase open file limit
rlimit_core = 0                  ; Disable core dumps

Post-Optimization Verification and Debugging

  1. Syntax Check: After modifying config, run php-fpm -t to test syntax.
  2. Restart Service: systemctl restart php-fpm or service php-fpm restart.
  3. Check Status: Use systemctl status php-fpm or visit the configured pm.status_path page.
  4. Monitor Resources: Use top, htop, or glances to monitor memory and CPU, ensuring pm.max_children does not cause memory exhaustion.

These configurations and optimizations can significantly improve PHP-FPM stability and performance in an LNMP environment. Fine-tune based on your actual load.

Post a Comment

Your email will not be published. Required fields are marked with *.