Error & Fix PHP Development PHP 8.3

How to Fix 'Maximum Execution Time of 30 Seconds Exceeded' Error in PHP

PHP killed your script because it ran longer than the time limit โ€” 30 seconds by default. Before you raise the limit, find out which clock actually stopped the request, because the real fix is usually in the code, not the setting.

RH Raja HunainAuthor 23 Nov 2026 2 min read 8 views
On this page
Share

Explanation

You click a button, wait, and instead of a result you get a blank page โ€” or this fatal error in the log:

PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/html/import.php on line 88

PHP stopped your script because it ran longer than max_execution_time allows. The default limit is 30 seconds on most web servers. The tempting fix is to raise the limit and move on โ€” but most of the time that only hides the real problem, because a script that should finish in two seconds and takes thirty is slow for a reason. This guide finds that reason first, fixes it, and only then changes the limit.

What the timer actually measures

Two details about this error confuse almost everyone who meets it for the first time.

First, on Linux, max_execution_time only counts the time PHP itself spends executing your code. Time spent waiting does not count: not database queries, not calls to an external API, not sleep(), and not the file upload itself. So a script that dies at exactly 30 seconds was doing 30 seconds of actual PHP work โ€” usually a loop, or far too much work crammed into one request.

Second, the file and line number in the message are where PHP happened to be when the timer ran out โ€” not necessarily where the slowness lives. Treat the line as a clue, not a verdict. And on the command line, PHP normally has no limit at all (max_execution_time is 0 there), so a cron script or an Artisan command never shows this error unless the code calls set_time_limit() itself.

Step 1: Read the message and reproduce the error

Confirm the exact limit in effect first. The number in the error message is whatever limit applied at that moment โ€” hosts often set 60, 120, or 300 instead of the 30-second default, so do not assume. Check it from a web request, not the CLI, because the two can read different php.ini files:

<?php echo ini_get('max_execution_time');

Delete that test file afterwards.

Then reproduce the failure on demand. On the CLI you can force the same fatal in two seconds:

php -d max_execution_time=2 import.php
# PHP Fatal error: Maximum execution time of 2 seconds exceeded in import.php on line 3

If the error appears at exactly the configured limit every time, even with tiny input, you are looking at a loop that never ends or work that grows without bound. If it only appears with large input โ€” a big import file, a report over a whole year โ€” the work is finite but too large for one request. That distinction decides the fix.

Step 2: Work out which clock actually stopped the request

A long PHP request passes through several timeouts, and the shortest one wins. Raising max_execution_time does nothing if a different layer gave up first, so match what you saw against this table:

What you saw Which layer stopped it Where to look
PHP fatal error at exactly your max_execution_time PHP itself The PHP or Laravel log, with file and line
504 Gateway Time-out, often at exactly 60 seconds nginx stopped waiting nginx error log: upstream timed out
502 Bad Gateway PHP-FPM killed the worker FPM log: execution timed out ... terminating
A queue job marked failed, worker exiting with code 137 The Laravel worker's own timeout The worker's --timeout value

Each row has a different fix. A 504 at 60 seconds means nginx gave up while PHP was still working โ€” raising the PHP limit alone changes nothing, and you need fastcgi_read_timeout instead (Step 5).

Step 3: Fix the code first โ€” the three real causes

Raising the limit is a stopgap. The durable fix is in the workload: bound it, speed it up, or move it out of the request.

Cause 1: a loop that never ends. A while condition that never becomes false, a retry that never gives up, or recursion without a working base case will burn the full 30 seconds on any input at all. Add a temporary iteration counter and log it โ€” if the count climbs without bound, read the loop condition for a variable that is never updated. Fix the exit condition; no limit change helps a loop with no exit.

Cause 2: too much work in one request. This is the most common cause in real apps: generating a report over millions of rows, resizing a folder of images, or importing a huge CSV synchronously. In Laravel, the usual culprit is the N+1 pattern โ€” one query per row instead of one query for all rows โ€” combined with loading everything into memory at once:

// Slow: one query per invoice line
foreach (Invoice::all() as $invoice) {
    $invoice->lines; // a new query on every iteration
}

// Fast: eager-load and chunk
Invoice::with('lines')->chunk(500, function ($invoices) {
    foreach ($invoices as $invoice) {
        // process 500 at a time, memory stays flat
    }
});

Eager loading plus chunk() has taken exports from minutes to seconds in real projects. The same idea applies outside Laravel: paginate the work, process it in batches, and never load a whole table to handle part of it.

Cause 3: a job that does not belong in a web request. Imports, backups, bulk emails, and large reports that genuinely take minutes do not belong behind a button click โ€” each one holds a PHP process while the visitor waits. Move the work to a queue job or a cron script, where the web time limit does not apply, and show the user a progress page instead.

Step 4: Raise the limit โ€” the right way, only where it belongs

Sometimes the work is genuinely large and the limit is genuinely too low โ€” a big import on a small host, for example. Then raising it is legitimate, but do it narrowly, for the script that needs it, not for the whole site:

<?php
set_time_limit(300); // restarts the timer: 300 seconds from this line

set_time_limit() restarts the counter from zero at the moment you call it, so place it at the top of the one script that needs the extra time. For a site-wide change, edit php.ini:

max_execution_time = 120

Then reload PHP-FPM so the change takes effect (sudo systemctl reload php8.3-fpm on Ubuntu). On shared hosting, where you cannot edit the main php.ini, a .user.ini file in your site's root with the same line often works under PHP-FPM, and cPanel offers the same setting under Select PHP Version โ†’ Options or MultiPHP INI Editor.

A warning: raising the limit for the whole site is the lazy fix, and it makes the next failure worse. A script that should finish in two seconds and now gets 300 will hold a PHP worker for five minutes instead of thirty when it breaks again. Raise it for the one script, and only after Step 3 has shown the code is not the problem.

Step 5: Do not forget the other timers

PHP is rarely the only clock. If you raise PHP to 300 seconds but the web server gives up at 60, the browser sees a gateway error:

  • nginx: add fastcgi_read_timeout 300s; in the PHP location block, otherwise you get a 504 at 60 seconds.
  • PHP-FPM: the pool setting request_terminate_timeout kills a worker regardless of PHP's own timer.
  • Apache with mod_php or proxy_fcgi: the Timeout and ProxyTimeout directives play the same role.
  • Laravel queue workers: a job that runs long needs --timeout on the worker (php artisan queue:work --timeout=300).

Set the timers as a stack, with each layer slightly longer than the one below it.

Frequently Asked Questions

Why does the error still say 30 seconds when I already raised the limit? The change probably went to the wrong php.ini โ€” the CLI and the web server read different files โ€” or the app sets its own limit with set_time_limit(). Check ini_get('max_execution_time') from a web request.

Is it safe to set max_execution_time to 0? That removes the limit entirely, so a runaway script can hold a worker forever. Use it only on the CLI or for one specific script with set_time_limit(0), never site-wide on the web server.

Conclusion

The "maximum execution time exceeded" error is PHP telling you a request did more work than its time budget allows โ€” and the budget is rarely the problem. First, work out which layer stopped the request: a PHP fatal at exactly the limit, a 504 from nginx, a 502 from PHP-FPM, or a failed queue job each point at a different fix. Then fix the code: close the runaway loop, eager-load and chunk the oversized work, or move the long job to a queue. Only when the work is genuinely large should you raise the limit โ€” narrowly, for that one script. Fix the workload and the limit stops mattering.

Was this helpful? 0 likes Login to like & bookmark
RH

Written by

Raja Hunain

Full Stack Developer at Raja Dev Solutions โ€” writes about real development work, the errors he faced and exactly how he fixed them, plus practical Laravel, PHP, JavaScript and MySQL tutorials.

More about the author

Comments (0)

No comments yet. Be the first to comment.

Leave a comment

Comments are moderated and appear after approval.

Keep reading

Related Errors & Articles

Error & Fix PHP Development 22 Nov 2026 ยท 2 min read

How to Fix "Headers Already Sent" Error in PHP

PHP says "Cannot modify header information - headers already sent" when output reaches the browser before your header() call. Here is how to read the message, find the exact line that sent output, and fix it for good.