PHP

Maximum execution time exceeded: what PHP is really timing

The timer behind this error does not count what most people think it counts, and it is only one of several timeouts between the visitor and your code. Here is how to read it, find the slow part, and fix the work rather than the clock.

Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/shop/import.php on line 214. An import that worked last month now stops halfway, and the line in the message is a loop that has already run a few thousand times without complaint. Before you raise anything, it helps to know what that timer was measuring.

What does "maximum execution time exceeded" mean?

It means PHP stopped the script because it had been running longer than max_execution_time, a php.ini setting that defaults to 30 seconds for web requests. The error is fatal: the script ends on the spot, the visitor gets an error page or a half-finished response, and nothing after that line runs.

The file and line are simply where the script happened to be when the timer ran out. Like the memory limit error, the line named in the message is often innocent. It is the moment the clock fired, not necessarily the slow part.

What does the timer actually count?

On Linux, the timer counts time spent executing PHP, not time spent waiting. The PHP manual is explicit: database queries, stream operations, sleep() and system calls made through functions like system() are not included. On Windows the measured time is real wall-clock time.

That one detail changes the diagnosis. A page that hangs for two minutes waiting on a slow API will usually never show this error on a Linux server, because waiting does not run the clock. Something else ends that request first, as the next section shows. So when you do see "maximum execution time exceeded", PHP itself was busy: a loop over far more rows than anyone planned for, image resizing, a regular expression on a huge string, or recursion that went deeper than expected.

Which other timeouts sit in front of it?

Between the visitor and your script there are usually three or four timers, and whichever runs out first decides what the visitor sees. On a typical stack:

  • Cloudflare, if the site sits behind it, waits about 100 seconds for your server to answer, then shows its own error 524.
  • nginx waits fastcgi_read_timeout for PHP-FPM, 60 seconds by default, then returns a 504 Gateway Time-out and logs upstream timed out ... while reading response header from upstream.
  • Apache has its own Timeout directive, also 60 seconds by default.
  • PHP-FPM can kill a worker after request_terminate_timeout. It is off by default, and when a host sets it, the FPM log says the script's execution timed out and that the child was terminated.
  • PHP stops the script after max_execution_time, counting only its own work.
Diagram: the timeouts a slow PHP request meets, from the outside in. Cloudflare gives up after 100 seconds and shows error 524. nginx gives up after fastcgi_read_timeout, 60 seconds by default, and returns a 504. PHP-FPM can kill the worker after request_terminate_timeout, which is off by default. PHP itself stops the script after max_execution_time, 30 seconds by default, with the fatal error "Maximum execution time exceeded", but on Linux that timer only counts time spent running PHP code, not time spent waiting on a database or an API.
The first timer to run out decides which error the visitor sees.

This is why raising max_execution_time to 300 so often seems to do nothing. If nginx gives up at 60 seconds, the visitor gets a 504 at the one-minute mark while the script keeps running in the background with nobody waiting for its answer.

How do you find the slow part?

Measure it instead of guessing. PHP-FPM has a built-in slow log that writes a stack trace for any request still running after a threshold you choose. Turn it on for a pool on staging:

; in the pool config, e.g. /etc/php/8.3/fpm/pool.d/www.conf
request_slowlog_timeout = 5s
slowlog = /var/log/php8.3-fpm-slow.log

Reload FPM, reproduce the slow page, and read the log. Each entry shows the call stack at the moment the threshold passed, which is usually enough to point at the loop or the function doing the damage. Without access to FPM, a few timing markers do the same job more crudely:

$t = microtime(true);
// ... step one ...
error_log(sprintf('step one: %.2fs', microtime(true) - $t));

On staging, a profiler such as Xdebug's profiling mode gives the full picture. Read it for one thing: which function's own time is largest, and how many times it was called. A cheap function called two hundred thousand times is the classic culprit.

When is raising the limit the right fix?

Raise it when the work is legitimate, bounded and rare: a one-off import on a small site, an admin export that runs once a month. Raise it for that script only, with set_time_limit() at the start of the job, rather than for every request on the server. Calling set_time_limit() restarts the counter from zero, so it can also be used to give each batch of a long job a fresh allowance.

For everything else, change the work. The usual options, roughly in order:

  • Batch it. Process 500 rows per request and resume from where the last batch stopped, so no single request runs long.
  • Move it out of the request. Queue the job and let a worker process it: Laravel queues, WooCommerce's Action Scheduler, or a plain cron entry that runs a PHP script from the command line, where no time limit applies by default.
  • Fix the algorithm. Nested loops over two large lists, or one database query per row inside a loop, turn a seconds-long job into a minutes-long one. Loading the lookup data once into an array keyed by ID often removes most of the time.

If the job grows with your data, it will hit any limit eventually. Raising the ceiling only tells you how long you have before the same error comes back.

The developers page explains how a drafted fix is checked before it is written to disk. For the PHP error that tends to arrive alongside this one, see allowed memory size exhausted.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required