PHP stops the script after 30 seconds
Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/app/import.php on line 42Short answer
PHP ends a script that runs past max_execution_time (30 seconds by default for web requests). Raise it for the one script that needs it, make sure you edited the php.ini that PHP-FPM really reads, and raise the FPM and web server limits as well. Work that can take minutes belongs in a queue, not in the request.
Fix it fast
1Find out which cause you have
Read the number in the message. It is the limit PHP actually applied, so it tells you whether your change reached this process. Then see which limits are set for command line PHP; the web value is read through a request in the next causes.
php -r 'echo ini_get("max_execution_time"), PHP_EOL;' php --ini2Match what you see, then jump to its fix
- 1
The message shows 30 (or the number you set) and the same page always fails at the same step, such as an import, a report or an image batchThe script really needs more than the default 30 secondsFix - 2
You raised the value, but the error still prints the old numberYou edited a php.ini that the web server does not readFix - 3
The request ends with an error page from the web server, and PHP prints no 'Maximum execution time' messagePHP-FPM kills the worker with request_terminate_timeoutFix - 4
504 Gateway Time-out after about 60 seconds, nothing about execution time in the PHP logThe web server gives up before PHP doesFix - 5
Run time grows with the amount of data, and every limit you raise fails again laterThe work belongs in a queue, not in a web requestFix
- 1
What does the error look like?
Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/app/import.php on line 42Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded504 Gateway Time-out (no PHP error in the log)
Cause 1 of 5
The script really needs more than the default 30 seconds
1Check
On Linux the default counter measures the time PHP itself spends running code, not time spent in system calls, streams or database queries, so a script that only waits usually does not trip it. Reaching the limit means heavy PHP work in a loop, or a build of PHP that counts elapsed time (ZTS builds since 8.3 use real-time timers).
php -r 'echo PHP_ZTS ? "ZTS" : "NTS", PHP_EOL;'
php -r 'echo ini_get("max_execution_time"), PHP_EOL;'2Fix
Make the loop cheaper first (batches, fewer queries, streaming). If one script still needs longer, raise the limit for it, either in the script or in php.ini. set_time_limit() restarts the counter each time it is called, and 0 means no limit at all, which is risky on a web request.
<?php
set_time_limit(120); // this script may run 120 more seconds
; php.ini or a pool file, for every script
max_execution_time = 603Verify
The script finishes, or the message now shows the new number.
php -r 'set_time_limit(120); echo ini_get("max_execution_time"), PHP_EOL;'Confirmed by php.net
Cause 2 of 5
You edited a php.ini that the web server does not read
1Check
Command line PHP and PHP-FPM each load their own php.ini, so a change for one does not reach the other. php --ini shows the CLI file. For the web side, run a tiny page through the real server and delete it afterwards.
php --ini | grep 'Loaded Configuration File'
echo '<?php echo php_ini_loaded_file(), " ", ini_get("max_execution_time");' | sudo tee /var/www/html/ini-probe.php
curl -s http://127.0.0.1/ini-probe.php; sudo rm /var/www/html/ini-probe.php2Fix
Change the value in the file the probe printed (on Debian and Ubuntu packages that is usually /etc/php/8.3/fpm/php.ini), or in the pool with php_admin_value, then restart PHP-FPM. Values set with php_admin_value cannot be overridden by ini_set() in the app.
; /etc/php/8.3/fpm/pool.d/www.conf
php_admin_value[max_execution_time] = 120
sudo systemctl restart php8.3-fpm3Verify
The probe now prints the new value.
curl -s http://127.0.0.1/ini-probe.phpConfirmed by php.net
Cause 3 of 5
PHP-FPM kills the worker with request_terminate_timeout
1Check
request_terminate_timeout is a pool setting that kills the whole worker process after a set time. The PHP manual says to use it when max_execution_time does not stop a script for some reason. It is off (0) by default, so look for a value somebody set.
sudo grep -rn 'request_terminate_timeout' /etc/php/8.3/fpm/2Fix
Give it a value above max_execution_time, or leave it at 0 when you do not need the safety net. Use a slow log to see where long requests spend time.
; /etc/php/8.3/fpm/pool.d/www.conf
request_terminate_timeout = 150s
request_slowlog_timeout = 10s
slowlog = /var/log/php8.3-fpm.slow.log3Verify
Reload PHP-FPM and repeat the slow request.
sudo systemctl reload php8.3-fpmConfirmed by php.net
Cause 4 of 5
The web server gives up before PHP does
1Check
nginx waits for PHP-FPM with fastcgi_read_timeout (60 seconds by default), counted between two reads, not for the whole request. Apache has its own Timeout directive, which the PHP manual lists as 300 seconds by default.
sudo nginx -T 2>/dev/null | grep -n fastcgi_read_timeout2Fix
Raise it only for the location that serves the long script, and keep it above the PHP limits. A bigger limit is a stopgap: if the request can take minutes, use the queue below.
location = /admin/export.php {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 180s;
}3Verify
Test the config, reload, and repeat the request.
sudo nginx -t && sudo systemctl reload nginxConfirmed by nginx.org
Cause 5 of 5
The work belongs in a queue, not in a web request
1Check
Command line PHP has no execution time limit by default (the manual gives 0), so a queue worker is not cut off by max_execution_time. Laravel instead limits each job with its own timeout, 60 seconds by default.
php artisan queue:work --help | grep -i timeout2Fix
Dispatch a job from the request and answer at once. Give the job its own timeout, and keep that timeout lower than the connection's retry_after, otherwise the job can be started a second time while it is still running. The pcntl PHP extension is required for job timeouts.
// controller
ExportReport::dispatch($report);
return response()->json(['status' => 'queued'], 202);
// app/Jobs/ExportReport.php
class ExportReport implements ShouldQueue
{
public $timeout = 300; // keep below retry_after in config/queue.php
}
php artisan queue:work --timeout=3003Verify
The request answers quickly, and the worker log shows the job finishing.
php artisan queue:work --onceConfirmed by laravel.com
How do I stop it coming back?
- Treat any web request that takes longer than a few seconds as a design problem: move it to a queue, a scheduled command or a streamed response.
- Set outbound timeouts on HTTP and database calls so a stuck dependency fails fast instead of eating the whole limit.
- Keep the limits in order: PHP script limit, then PHP-FPM terminate timeout, then web server timeout, then any load balancer, each a little larger than the one inside it.
- Run php --ini and a web probe after every PHP upgrade, since the package may install a fresh php.ini.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Servers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixBack end
Laravel file upload returns 413 behind nginx
413 Content Too Large
A 413 means a limit refused the request body before Laravel could use it.
3 causes, each with a fixBack end
Laravel routes return 404 or throw NotFoundHttpException
Symfony\Component\HttpKernel\Exception\NotFoundHttpException
A 404 means Laravel found no route for that URL, or the request never reached Laravel at all.
3 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
Why a Filament panel feels slow on a Laravel server
The panel is fast on a small demo and slow once there are many resources, pages and widgets
A slow Filament panel on a server is most often running without the production caches.
3 causes, each with a fixBack end
Intervention Image will not save the file in a Laravel app
Can't write image data to path (/var/www/app/public/uploads/photo.jpg)
Intervention Image writes straight to a file path.
3 causes, each with a fix