PHP and Laravel

Maximum execution time of 30 seconds exceeded: causes and fix

Fix the PHP and Laravel error 'Maximum execution time of 30 seconds exceeded' (or 120 seconds): find the slow script, raise the limit safely, verify.

PHP Fatal error: Maximum execution time of 30 seconds exceeded

In short

PHP stops a script and raises this fatal error when it runs longer than the max_execution_time setting, which is 30 seconds by default for web requests. PHP itself throws it, not nginx, Apache or Laravel. Laravel shows it on the error page and writes it to storage/logs/laravel.log. The number is the limit in force, so 120 seconds means someone raised the limit to 120 and the script still ran past it.

Likely causes

  • •A web request is doing too much work. Imports, exports, report builds and bulk updates run inside one request. The stack trace in storage/logs/laravel.log points at a controller or a large loop, and the error appears only with bigger data sets.
  • •Loading or looping over too many rows. Calls such as Model::all(), or queries inside a foreach (the N+1 pattern), make PHP hydrate and process thousands of models. The trace ends in Eloquent or a collection method, and a query debugger shows hundreds of queries for one request.
  • •An infinite loop or runaway recursion after a code change. The error started right after a deploy, hits the same file and line every time, and happens even with a small amount of data. Raising the limit only delays the failure.
  • •Slow work done synchronously in the request. Image resizing, PDF generation, and calls to slow third-party APIs all run while the user waits. The trace shows an image library, a PDF library or an HTTP client call.
  • •The limit is too low for a legitimate long task. Migrations, backups or one-off maintenance run through a web route need more than 30 seconds. The same task finishes fine from the terminal, because the PHP CLI has no time limit by default.
  • •You changed the wrong php.ini. You raised the limit but the web still fails at 30 seconds. The CLI and PHP-FPM read different files, so php -i may show your new value while PHP-FPM still uses the old one.

How to fix it

  1. 1. Find the script and line that timed out

    Read the stack trace in the Laravel log. The first frame under the error shows the file and line where PHP was stopped. That tells you which of the causes applies. Run this from your Laravel project root.

    grep -A 8 "Maximum execution time" storage/logs/laravel.log | tail -n 30
  2. 2. Check which limit the web process really uses

    The CLI and PHP-FPM have separate settings. Replace 8.3 with your PHP version. On RHEL-family systems the binary is usually php-fpm with no version suffix. Look for the max_execution_time line in the output.

    php -r 'echo ini_get("max_execution_time"), PHP_EOL;'
    php-fpm8.3 -i | grep max_execution_time   # Ubuntu/Debian
    php-fpm -i | grep max_execution_time      # RHEL, Rocky, Alma
  3. 3. Fix the slow work first

    Process large tables with chunkById() or cursor() instead of all(). Use eager loading (with()) to remove N+1 queries. Move long tasks such as exports and image processing into a queued job so the request returns at once. If the trace shows a loop that never ends, fix the code. A bigger limit will not help.

    php artisan make:job ProcessExport
  4. 4. Raise the limit for one script only

    If one task legitimately needs more time, call set_time_limit(120) at the start of that controller method or command. This resets the timer and affects only that request, so the rest of the site keeps the safe default. Avoid set_time_limit(0) on web requests, because a stuck request then holds a PHP-FPM worker forever.

  5. 5. Raise the limit in php.ini for PHP-FPM (reloads PHP-FPM)

    Use this only if many requests need it. The sed command keeps a .bak backup. A reload is graceful, but run it at a quiet moment. On Ubuntu/Debian the file is /etc/php/8.3/fpm/php.ini. On RHEL-family systems it is /etc/php.ini. For Apache with mod_php, edit /etc/php/8.3/apache2/php.ini and reload apache2 instead.

    sudo sed -i.bak 's/^max_execution_time = .*/max_execution_time = 120/' /etc/php/8.3/fpm/php.ini
    sudo systemctl reload php8.3-fpm   # RHEL: sudo systemctl reload php-fpm
  6. 6. Match the web server and pool timeouts (reloads nginx)

    If PHP now runs for 120 seconds but nginx gives up after its fastcgi_read_timeout (60 seconds by default), users get a 504 instead. Set fastcgi_read_timeout 120s; in the PHP location block of your site config. Also check request_terminate_timeout in your PHP-FPM pool file (/etc/php/8.3/fpm/pool.d/www.conf, or /etc/php-fpm.d/www.conf on RHEL), because it can kill a request regardless of max_execution_time. Test the config before reloading.

    grep -rn "fastcgi_read_timeout" /etc/nginx/
    sudo nginx -t && sudo systemctl reload nginx
  7. 7. Set the timeout for queue workers and long artisan commands

    Queue workers run on the CLI, so they use the --timeout flag, not max_execution_time. Keep retry_after in config/queue.php larger than the timeout, or jobs will be picked up twice. Restart the worker (or Supervisor program) after the change, which interrupts the job it is running.

    php artisan queue:work --timeout=300
    php -d max_execution_time=0 artisan your:command

Confirm it is fixed

Confirm PHP-FPM reports the new limit, then repeat the request that failed. The good output shows your new value in both columns, and no new 'Maximum execution time' lines appear in storage/logs/laravel.log.

php-fpm8.3 -i | grep max_execution_time

How Opservo catches it early

The Opservo agent tails nginx, Apache, Laravel, Docker, PM2 and systemd journal logs and groups repeated lines into normalised patterns, so raw log lines are never stored. When this error keeps coming back, it raises a "recurring error" finding with a plain-English AI summary.

Last updated 2026-11-30.

FAQ

Related questions

What if the message says 120 seconds instead of 30?+

The cause and fix are the same. The number is the limit that was in force when PHP stopped the script, so someone already raised it to 120. Find the slow code with the log trace and fix that, or move the work into a queued job.

Should I set max_execution_time to 0?+

Not for web requests. Zero means no limit, so a stuck request can hold a PHP-FPM worker indefinitely and starve the site. Use it only for CLI jobs you trust, and prefer a queue with a sensible --timeout.

Why does the script finish in the terminal but fail in the browser?+

The PHP CLI has no time limit by default, while PHP-FPM and mod_php use the 30 second default from php.ini. They also read different php.ini files, so check the limit with php-fpm -i and not just php -i.

Will raising the limit stop the error for good?+

Only if the task is truly long but healthy. If the script is slow because of N+1 queries, huge result sets or a loop that never ends, a higher limit just makes users wait longer before the same failure.

Maximum execution time of 30 seconds exceeded: fix · Opservo