Blog / Linux/ Optimizing PHP-fpm Performance on a 1GB Memory VPS

Optimizing PHP-fpm Performance on a 1GB Memory VPS

1G内存VPS或云主机,PHP-fpm.conf该怎么设置提高性能?

PHP-fpm Configuration File Path and Editing

The main PHP-fpm configuration file is typically located at /usr/local/php/etc/php-fpm.conf. To modify settings, edit it with a text editor (like vi) and then reload or restart the PHP-fpm service.

# Edit the config file
vi /usr/local/php/etc/php-fpm.conf
# Reload configuration (graceful restart)
service php-fpm reload
# Or restart the service completely
service php-fpm restart

Key Performance Configuration Options

These are the most impactful settings for performance tuning.

Process Manager Mode (pm)

The pm parameter defines how PHP-fpm manages child processes. The main options are static and dynamic.

  • static: A fixed number of child processes. Only pm.max_children is used.
  • dynamic: The number of child processes adjusts dynamically based on load. pm.max_children sets the absolute limit, while pm.start_servers, pm.min_spare_servers, and pm.max_spare_servers control the scaling.

Process Control Parameters

  • pm.max_children: Maximum number of child processes allowed (the hard limit for both modes).
  • pm.start_servers: Number of child processes created on startup (dynamic mode).
  • pm.min_spare_servers: Minimum number of idle processes (dynamic mode).
  • pm.max_spare_servers: Maximum number of idle processes (dynamic mode).

Request Timeout and Process Recycling

  • request_terminate_timeout: Maximum execution time for a single PHP request (seconds). The process is terminated if exceeded. Example: request_terminate_timeout = 30.
  • pm.max_requests: Number of requests a child process serves before it is automatically restarted. This helps mitigate memory leaks. Example: pm.max_requests = 2000.

Memory Optimization and Configuration for 1GB VPS

For a server with only 1GB of RAM, use dynamic mode. It's more efficient as it scales processes down during low traffic, conserving precious memory.

Recommended Configuration (1GB RAM):

pm = dynamic
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 1000

This setup ensures a minimum of 5 idle processes for quick response, scales up to handle load, and has a maximum limit of 30 to prevent memory exhaustion. The pm.max_requests setting helps recycle processes periodically.

Estimating PHP-fpm Process Memory Usage

Use this command to check the average memory consumption (in MB) of your PHP-fpm processes:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%sn", sum/NR/1024,"M") }'

Once you have the average, calculate a safe pm.max_children value: (Total Available RAM * 0.7) / Average Process Memory. The 0.7 factor reserves ~30% of RAM for the OS, Nginx, MySQL, etc.

Coordinating with php.ini memory_limit

The memory_limit in php.ini sets the maximum memory a single PHP script can use. This value should be less than the memory allocated per PHP-fpm process in your calculations. If a script exceeds this limit, the process is killed, potentially causing a 502 Bad Gateway error from Nginx.

Post a Comment

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