If the exception with the message “Maximum execution time of 30 seconds exceeded” occurs in your application, probably your application is misdesigned. By default, PHP has an execution time limit of 30 seconds.
So, all tasks that can take more than a few seconds should be handled in the background using Laravel queues. Although, you may want to increase this limit. In this article, I will show you how to do it.
What You Should Know About It?
You will most likely see the FatalErrorException with this error message in Laravel. But this error is not thrown by Laravel itself. The framework catches and converts to the exceptions regular PHP error messages such as fatal, warning, and others. This behavior makes error handling in Laravel easier. And this mechanism is being worked out in this case. The message from PHP says that the script has been executed too long and will be terminated immediately.
The second point you should consider is that the exception occurs only in PHP scripts executed on a web server. For example, Apache or PHP-FPM. There is no execution time limit for all scripts executed in the terminal, even if the in the php.ini file, the limit is specified.
The third point is that the mechanism for calculating the time for applying the limitation is unexpected. PHP counts only its execution time and skips all syscalls and input wait. For example, if you want to use the sleep() method to test it, it will not work.
So that you can ensure that the limitation is not applied to the command line scripts using the following script:
loop.php<?php
$i = 0;
$secondsToWait = 65;
$start = hrtime()[0];
while (hrtime()[0] - $start < $secondsToWait) {
$i++;
}
php loop.php
The script will be executed with no errors despite requiring 120 seconds to complete. Now, let’s have a look at what happens when you place this code in a controller and execute it on a web server:
How to Fix the Error?
Firstly, you can set the limitation using the set_time_limit() directly in the PHP script. This function works not only for web-served scripts but also for SAPI. For example. let’s make timeout in 90 seconds:
app/Http/Controllers/TaskController.phpset_time_limit(90);
Or disable it at all:
app/Http/Controllers/TaskController.phpset_time_limit(0);
If you want to configure max execution time globally on a web server, firstly, you should find out where its php.ini is located. Make the phpinfo.php script with the following contents and open it in the browser:
public/phpinfo.php<?php phpinfo(); ?>
After this, you can find the path to its configuration file in the Loaded Configuration File line. In this case it is /etc/php/8.1/apache2/php.ini. Now open it, find max_execution_time parameter in the PHP section, and set a desired value:
/etc/php/8.1/apache2/php.inimax_execution_time=0
After this, you can re-open the page in the browser and ensure that everything works. That’s it. If you have troubles with timeout in Laravel Jobs, read article How to Fix Job Timed Out in Laravel.
Great goods from you, man. I have understand your stuff previous to and you
are just extremely excellent. I actually like what you’ve acquired here, really like
what you are stating and the way in which you say it. You make
it enjoyable and you still take care of to keep it
wise. I can’t wait to read much more from you. This is really a tremendous site.
I am really loving the theme/design of your weblog.
Do you ever run into any browser compatibility issues? A couple of my blog visitors
have complained about my blog not operating correctly in Explorer but
looks great in Firefox. Do you have any suggestions to help fix this problem?
This site have a few issues with colors in Internet Explorer. The developers of the template I’m using don’t want to adapt it for IE, and they are right because Microsoft ended support for this browser in 2022 for most editions of Windows.