If you have ever encountered errors like the ones below, the following tweaks may prove useful for you.
This article assumes that your configurations are located in:
Nginx: /etc/nginx/nginx.conf
PHP-FPM: /etc/php/{version}/fpm/pool.d/www.conf
Please adjust according to your system. Additionally, all values are relative to your system resources, so you need to test which values are right for you.
Tweak Nginx
To check the limits of Nginx, follow these steps:
Tweak PHP-FPM
To determine the average memory usage of a process, execute the following command:
Please note that you may need to match php-fpm with your specific process name, which could be different, such as php-fpm8.2. In case your process name varies, adapt the command accordingly:
In the output, pay attention to the RSS column, which indicates the average memory usage in kilobytes for each process. As illustrated in the example above, the average memory usage is approximately 56540 kilobytes or around 60 megabytes per process.
Or alternatively, use this command to get the summary:
This command provides a single output, representing the average memory usage of the specified process (php-fpm8.1 in this case). The result, such as 63M, indicates the average memory consumption in megabytes.
Tweak the OS (Ubuntu)
Then run sysctl -p for changes to take effect. After that, you can also restart Nginx and PHP-FPM services.
To make these changes take effect, restart the server.
Enable PHP-FPM Status Page
Uncomment the pm.status_path line from PHP-FPM config. The default value is /status. Adjust as needed or add a prefix if you have multiple pools.
Then, edit Nginx per-site config, add this inside the server block
Restart Nginx and PHP-FPM services for changes to take effect. Now you can access the status page via http://your-site.com/php-fpm-status. All available options are below:
/php-fpm-status
/php-fpm-status?json
/php-fpm-status?html
/php-fpm-status?xml
/php-fpm-status?full
/php-fpm-status?json&full
/php-fpm-status?html&full
/php-fpm-status?xml&full
Enable Nginx Status Page
First check if your installed Nginx has ngx_http_stub_status_module with this command:
Edit Nginx per-site config, add this inside the server block:
Restart Nginx service for changes to take effect. Now you can access the status page via http://your-site.com/nginx-status.
It’s worth mentioning, always check your Nginx & PHP-FPM config after editing and before restarting the service.
UPDATE 2024-01-12
Troubleshoot PHP-FPM
These commands are useful for obtaining detailed information about the scripts being executed:
To trace a specific process, use the following command, replacing [PID] with the process ID you want to monitor. You can identify the process ID using commands such as htop or top:
To view the trace file, open /tmp/strace.out.[PID]. Dumping the output into a temporary file makes it easier to analyze compared to streaming it in real-time.
Here’s an example of the output:
To identify the syscalls taking the longest time, use the following command:
Wait for a few seconds, then press Ctrl+C. The output looks like this: