php timeout - set_time_limit(0); - don't work

php timeout - set_time_limit(0); - don't work

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

set_time_limit(0) Fails: When PHP Still Enforces a 30-Second Hard Limit

You've added set_time_limit(0); at the very top of your script, yet the application still dies after exactly 30 seconds with a "Maximum execution time exceeded" fatal error. This happens frequently because developers assume that disabling the limit in code is an absolute guarantee. In reality, PHP's execution time is governed by a hierarchy of checks where configuration files and web server settings can override your inline directives.

The log entry PHP Fatal error: Maximum execution time of 30 seconds exceeded confirms that the PHP interpreter itself has hit its wall. However, the presence of set_time_limit(0) suggests you believe you have disabled this check. The discrepancy usually stems from one of three specific architectural constraints: the web server's own timeout settings, the FastCGI process manager limits, or a misunderstanding of how set_time_limit interacts with safe mode (which your logs indicate is off, but which still leaves other doors open).

The Web Server Hard Limit

Even if PHP ignores its internal timer, the HTTP protocol layer often enforces its own. When a request hangs beyond a certain threshold, the web server itself terminates the connection to prevent resource exhaustion. This behavior manifests as a 500 Internal Server Error or a blank screen before the PHP error log is even written.

In Nginx, for instance, the fastcgi_read_timeout directive defaults to 60 seconds in many configurations but can be explicitly set lower. If your web server configuration contains:

fastcgi_read_timeout 30s;

Nginx will kill the connection after 30 seconds regardless of what PHP thinks. Similarly, Apache's mod_proxy or ProxyTimeout directives can impose identical restrictions. Before touching your PHP code, inspect your web server configuration. Look for directives like proxy_read_timeout, fastcgi_send_timeout, or client_body_timeout. If these are set to 30 seconds, they act as a hard cap that set_time_limit(0) cannot bypass.

FastCGI Process Manager Constraints

If you are running PHP via FPM (FastCGI Process Manager), the process manager itself may impose strict limits independent of the web server or the PHP script. The php-fpm configuration file often contains a directive like:

request_terminate_timeout = 30s

This setting is common in production environments to ensure that runaway scripts do not hold up the entire pool of worker processes. When this limit is reached, FPM kills the script immediately. The PHP error log might show the timeout message, but the actual termination signal comes from the process manager daemon, not the PHP interpreter. In this scenario, increasing request_terminate_timeout in your php-fpm.conf or a custom pool configuration file is necessary.

Configuration Precedence and Safe Mode

Your logs mention that safe mode is off, which is good; however, the directive set_time_limit(0) only affects scripts running in CGI/FPM modes if the limit hasn't already been enforced by a parent process. If you are running PHP as an Apache module (mod_php), the behavior can differ slightly, but the principle remains: configuration files loaded before script execution take precedence.

If you have a .user.ini file in your project directory or a global php.ini with max_execution_time = 30, that setting applies to every request unless explicitly overridden. While set_time_limit(0) should technically disable the limit, some hosting environments restrict this capability entirely for security reasons. If you are on a shared host, you might not have permission to modify php.ini or user.ini. In such cases, the only viable path is to contact the host provider and request an increase in the max_execution_time directive via the control panel or support ticket.

Architectural Alternatives

If you cannot change the server configuration or if the timeout persists due to external hard limits, relying on set_time_limit(0) as a workaround is fragile. A more robust approach involves restructuring the logic that causes the delay. If a specific operation—such as an image generation task or a large data export—takes longer than 30 seconds, consider breaking it into smaller chunks and processing them asynchronously using a queue system like Laravel Queues or Redis.

By offloading heavy tasks to background workers, you ensure that the initial HTTP request completes quickly, returning a success status to the user while the actual work finishes in the background. This pattern decouples the response time from the processing duration, effectively bypassing the need for extended timeouts on the web server side. For projects built on Laravel, utilizing job queues allows you to handle long-running processes without worrying about set_time_limit or web server timeouts interfering with the user experience.

Ultimately, set_time_limit(0) is not a magic switch that overrides every possible timeout mechanism in the request stack. It only disables PHP's internal watchdog. If the web server, FPM, or hosting provider has installed their own guardrails, you must adjust those specific settings or change your application architecture to fit within them.

Our network