Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP stops the script after 30 seconds

ErrorFatal error: Maximum execution time of 30 seconds exceeded in /var/www/app/import.php on line 42

Short 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.

  • Applies to PHP 8.3, PHP-FPM 8.3, nginx 1.28, Laravel 12, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 --ini
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2You raised the value, but the error still prints the old numberYou edited a php.ini that the web server does not readFix
    3. 3The 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. 4504 Gateway Time-out after about 60 seconds, nothing about execution time in the PHP logThe web server gives up before PHP doesFix
    5. 5Run 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

What does the error look like?

  • Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/app/import.php on line 42
  • Symfony\Component\ErrorHandler\Error\FatalError: Maximum execution time of 60 seconds exceeded
  • 504 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 = 60

3Verify

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

Did this fix it?

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.php

2Fix

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-fpm

3Verify

The probe now prints the new value.

curl -s http://127.0.0.1/ini-probe.php

Confirmed by php.net

Did this fix it?

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.log

3Verify

Reload PHP-FPM and repeat the slow request.

sudo systemctl reload php8.3-fpm

Confirmed by php.net

Did this fix it?

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_timeout

2Fix

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 nginx

Confirmed by nginx.org

Did this fix it?

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 timeout

2Fix

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=300

3Verify

The request answers quickly, and the worker log shows the job finishing.

php artisan queue:work --once

Confirmed by laravel.com

Did this fix it?

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:

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.