Increase max execution time for php

Increase max execution time for php

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

Stefan Bogdanescu

Founder & Senior Architect

set_time_limit Is Not a Panacea: Understanding PHP Execution Time Limits

You've added set_time_limit(0); to your script, yet it still halts after roughly two minutes. This is a classic case of hitting a wall that set_time_limit cannot breach because the function only controls one specific clock: the web server's execution timer. When you run a long-running process like a link search, relying solely on this directive inside your PHP code often fails to account for the broader infrastructure constraints surrounding your application.

The set_time_limit() function modifies the max_execution_time directive for the current script. Setting it to 0 effectively disables the limit, allowing the script to run until it crashes due to memory exhaustion or an explicit stop command. However, this behavior assumes that nothing else is cutting the power cord. In a shared hosting environment or a tightly tuned production server, several other mechanisms can terminate your process before set_time_limit(0) takes full effect.

The Hidden Culprits: Memory and Server Configuration

Even if you successfully disable the execution time limit, your script may still die due to memory constraints. PHP has its own memory ceiling defined by the memory_limit directive in your php.ini file or a .user.ini configuration. If your link search algorithm is resource-intensive, accumulating data structures without clearing them will eventually cause a fatal Allowed memory size exhausted error, regardless of how much time you allow it to run.

Furthermore, many hosting providers and load balers enforce a maximum request duration at the Apache or Nginx level, independent of PHP's internal timer. For instance, an Apache worker might be configured with LimitRequestTime 120, which hard-cuts off any request taking longer than two minutes. Similarly, Nginx can have a proxy_read_timeout or fastcgi_read_timeout that terminates the connection if the backend PHP process takes too long to respond. These are server-side firewalls designed to prevent your application from holding up other requests on a busy server.

The Child Process Trap: FPM and CLI Differences

If you are running PHP via FastCGI Process Manager (FPM), the situation becomes more complex. FPM workers are spawned as child processes. If a script runs for too long, it ties up an entire worker process. Depending on your configuration (pm.max_children in php-fpm.conf), the system might kill the process to free up resources, or the web server might mark the request as timed out before PHP even gets a chance to execute its logic.

Additionally, there is often confusion between CLI and Web contexts. The set_time_limit() function behaves differently depending on how you invoke PHP. In a Command Line Interface (CLI) environment, you can pass the time limit directly via the command line argument:

php -d max_execution_time=0 your_script.php

This approach often works better because it bypasses certain web server timeouts entirely, provided the CLI process itself isn't restricted by ulimit. However, in a web context (which seems to be your case given you are searching links from a site), you must ensure that your web server configuration aligns with your PHP settings.

Why You Might Still Hit the Two-Minute Wall

The specific behavior of stopping at 2-3 minutes strongly suggests an external timeout mechanism is active. If your php.ini allows unlimited time but the request dies quickly, check your Apache or Nginx config files for directives like:

# In Apache (.htaccess or vhost)
LimitRequestTime 120

or

# In Nginx
fastcgi_read_timeout 120s;
proxy_read_timeout 120s;

These settings override PHP's internal limits. To resolve this, you must adjust these server-level directives or refactor your application to handle long tasks asynchronously.

The Better Approach: Background Processing

For operations that inherently take minutes or hours, such as crawling a site for links, blocking the main request thread is generally not a sustainable architecture. Instead of trying to force the web server to wait indefinitely, consider offloading the task to a background queue system like Laravel Queues or Redis.

By pushing the link search logic into a queued job, your web server can respond immediately with a "task started" confirmation. The actual heavy lifting happens in a decoupled worker process where you have full control over memory limits and execution time without worrying about holding up HTTP connections. This pattern is standard practice for resource-heavy tasks.

If you are building a custom solution using the Laravel framework, integrating a queue job allows you to scale horizontally rather than fighting against server timeouts. For more details on managing long-running tasks in PHP ecosystems, resources from LaravelCompany offer insights into optimizing application performance and handling background jobs effectively.

Ultimately, set_time_limit(0) is just one piece of the puzzle. You must inspect memory limits, server timeouts, and process management strategies to ensure your long-running scripts survive beyond the two-minute mark.

Our network