Increase PHP-FPM idle timeout setting
When migrating from a traditional Apache + mod_php setup to PHP-FPM, you often run into a specific class of errors that didn't exist in the monolithic architecture: FastCGI idle timeouts. This happens because the FastCGI protocol maintains a persistent connection between the web server (or socket listener) and the PHP process manager. If that connection remains open without traffic for too long, the supervisor kills it to free up resources, regardless of whether your script is still sleeping or executing logic.
In your specific case, you are seeing the error FastCGI: comm with server "/usr/local/php-5.6.24/sbin/php5-fpm" aborted: idle timeout (30 sec). Your code processes 1000 items, resetting the internal script execution time to 30 seconds for each item, resulting in a total runtime of roughly 30 minutes. The PHP process is likely active when processing an item, but once proccessThatTakesAround2secs() finishes and the loop continues or pauses, if there is no I/O activity on the socket for more than 30 seconds, the FastCGI layer drops the connection.
The confusion often arises between two distinct timeouts: PHP's internal execution limit and the FastCGI connection idle timeout. Your php_value[max_execution_time] = 30 in php.ini (or equivalent) controls how long a single script is allowed to run before PHP throws a fatal error. However, the -idle-timeout 30 directive in your fastcgi.conf controls how long the FastCGI socket connection stays open between requests or during pauses within a request.
You asked if it makes sense to increase this idle timeout to an hour while keeping the script execution limit at 30 seconds. The answer is yes, absolutely. These are orthogonal concerns. By increasing the idle timeout in your fastcgi.conf to something like -idle-timeout 3600, you allow the FastCGI connection to remain stable during the pauses between your loop iterations. Crucially, this does not bypass the PHP execution limit. If a single request logic exceeds 30 seconds without resetting it, PHP will still terminate the script and return an error, preventing infinite loops or resource exhaustion.
Regarding your question about per-script idle timeout configuration akin to set_time_limit: there is no native PHP function like set_idle_timeout(). The execution time is governed by max_execution_time, but the connection keep-alive duration is a property of the FastCGI pool or the web server configuration, not the script itself.
To implement this fix, modify your fastcgi.conf (or the equivalent file in your Nginx/Apache integration) to increase the idle timeout parameter:
FastCgiExternalServer /usr/local/php-5.6.24/sbin/php5-fpm -socket /var/run/php5-fpm.sock -idle-timeout 3600 -pass-header Authorization
Alternatively, if you are using Nginx directly with PHP-FPM via fcgiwrap or similar wrappers, the configuration might sit in /etc/nginx/fastcgi_params or a specific location block:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# Ensure the PHP process manager allows long enough idle times if not set globally
However, for pure php5-fpm managed via Apache's mod_fastcgi or similar wrappers, the -idle-timeout flag is usually the correct place.
There are trade-offs to consider when tuning this value. Setting it too high (e.g., 24 hours) can lead to resource leaks if a malicious script hangs indefinitely without sending any output; the connection stays open, consuming file descriptors on both the web server and the PHP process manager. A standard recommendation for long-running batch jobs is often between 10 minutes and 1 hour. Since your total job takes 30 minutes, setting it to 60 minutes provides a comfortable buffer without risking resource starvation.
Another potential pitfall is the pm.max_requests setting in your pool configuration (pm.max_requests = 10000). If you process many requests, PHP-FPM recycles workers based on this count to prevent memory leaks. Ensure that your worker processes aren't being killed mid-script due to request counts before the timeout issue is resolved.
If you find yourself needing even finer control over execution time than set_time_limit provides, consider breaking your long-running logic into smaller chunks or using a background job queue system like Laravel's Task Scheduling or Redis queues. This approach decouples the user-facing response from the heavy processing, eliminating the need for large idle timeouts entirely.
For more robust solutions regarding process management and scaling in PHP applications, resources like https://laravelcompany.com often provide insights into best practices for modernizing legacy PHP stacks.