DEV Community

Raja Hunain
Raja Hunain

Posted on

PHP "Maximum Execution Time Exceeded"? Check These 4 Clocks Before You Touch php.ini

Your PHP script died at 30 seconds, so you raised max_execution_time to 300 — and the error came back anyway. That happens more often than people admit, because the PHP time limit is usually not the clock that stopped the request.

A long PHP request passes through several timeouts, and the shortest one wins. Here is how to tell which one actually killed yours.

1. PHP itself — the fatal at exactly the limit

If the log says Maximum execution time of 30 seconds exceeded at exactly your configured limit, PHP stopped the script. One detail most people miss: on Linux, max_execution_time only counts time PHP spends executing code. Database queries, API calls, and sleep() do not count. So a script dying at exactly 30 seconds was doing 30 seconds of real PHP work — usually a loop that never ends, or far too much work in one request.

Reproduce it fast on the CLI:

php -d max_execution_time=2 import.php
Enter fullscreen mode Exit fullscreen mode

2. nginx — the 504 at 60 seconds

If the browser shows 504 Gateway Time-out at exactly 60 seconds, nginx gave up while PHP was still working. Raising the PHP limit changes nothing here — the fix is fastcgi_read_timeout 300s; in the PHP location block. Check the nginx error log for upstream timed out to confirm.

3. PHP-FPM — the 502

A 502 Bad Gateway means PHP-FPM killed the worker. The FPM log says execution timed out ... terminating, and the setting behind it is the pool's request_terminate_timeout — a limit that fires regardless of what max_execution_time says.

4. The Laravel worker — the failed queue job

A queue job marked failed with the worker exiting code 137 was killed by the worker's own --timeout, not by PHP. Raise it on the worker (php artisan queue:work --timeout=300) instead of touching php.ini.

Fix the code before the limit

Once you know which clock fired, fix the workload first. A runaway loop needs its exit condition fixed, not a bigger limit. An export that runs one query per row needs eager loading and chunking — that alone has taken real exports from minutes to seconds. And a job that genuinely takes minutes (imports, backups, bulk email) belongs in a queue or a cron script, not behind a button click.

Raise the limit only for the one script that needs it, with set_time_limit(300) at the top of that script. Raising it site-wide just means the next failure holds a PHP worker for five minutes instead of thirty.

I wrote the full walkthrough — how to reproduce the error, diagnose each layer, fix the three real causes in code, and set every timer correctly — here:

https://rajadevsolutions.online/errors/how-to-fix-maximum-execution-time-exceeded-error-in-php

Top comments (0)