Ffile2fix
Sign in Get started

Fix "Maximum execution time of 30 seconds exceeded" in PHP

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

PHP stopped the script because it ran longer than max_execution_time allows, which is 30 seconds by default for web requests. You can raise the limit, but the better fix is usually to find the slow database query, remote HTTP call or loop and make it faster or move it to a background job.

Also appears as: Fatal error: Maximum execution time of 60 seconds exceeded Β· Fatal error: Maximum execution time of 300 seconds exceeded in /wp-includes/class-wp-http-curl.php Β· PHP Fatal error: Maximum execution time of 120 seconds exceeded in vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php

Common causes

  • A slow or unindexed database query
  • Polling or retrying a remote HTTP or API call in a loop without its own timeout
  • Large imports, exports, backups or image processing run inside a web request
  • An infinite or very long loop
  • max_execution_time set too low for a legitimate admin task such as a plugin update

How to fix it

  1. Check the file and line. The path in the message shows where PHP was when time ran out, which is usually inside the slow operation. A line in a cURL or database class points to a slow remote call or query.
  2. Raise max_execution_time. Set max_execution_time = 120 in the active php.ini (find it with php --ini) and restart PHP-FPM. On Apache with mod_php you can use php_value max_execution_time 120 in .htaccess; on shared hosting use .user.ini or the control panel.
  3. Raise web server timeouts to match. If PHP now runs longer, Nginx may return 504 first. Raise fastcgi_read_timeout 120s; in Nginx, and request_terminate_timeout in the PHP-FPM pool if it is set lower.
  4. Extend the limit for one script. Call set_time_limit(300); at the top of a known long-running script. Each call restarts the timer, so calling it inside a batch loop keeps the job alive between batches.
  5. Run long jobs from the CLI. The PHP CLI has no execution time limit by default (max_execution_time = 0). Move imports, exports and bulk updates to a cron job or queue worker, for example php artisan queue:work or wp cli commands.
  6. Fix the slow part. Add indexes for slow queries (check with EXPLAIN), set timeouts on HTTP clients such as CURLOPT_TIMEOUT, and process large datasets in batches.

php.ini and Nginx

; php.ini
max_execution_time = 120

# Nginx location ~ \.php$ block
fastcgi_read_timeout 120s;

// one script only
set_time_limit(300);

How to stop it happening again

  • Set explicit timeouts on every outbound HTTP and API call
  • Run imports, backups and reports from cron or a queue, not a browser request
  • Index columns used in WHERE, JOIN and ORDER BY clauses
  • Log slow requests with the PHP-FPM slowlog to catch problems early

Frequently asked questions

Does max_execution_time include time waiting on the database?

On Linux, max_execution_time counts only CPU time spent running the script, so time waiting on database queries, sleep() or network calls usually does not count. On Windows it is wall-clock time. Web server and PHP-FPM timeouts are always wall-clock.

Why does set_time_limit() not work?

Some hosts disable it in disable_functions, or the web server or PHP-FPM request_terminate_timeout cuts the request first. Check both and run the job from the CLI if needed.

Is setting max_execution_time to 0 safe?

Zero means no limit. It is the CLI default and fine there, but on web requests it lets stuck scripts tie up PHP workers until the server runs out of them.