The maximum execution time for your PHP
scripts and applications are defined by the max_execution_time
directive. PHP
will timeout and exit with a fatal error once it reaches the time limit.
Fatal error: Maximum execution time of 30 seconds exceeded in yourscript.php
If not configured, the default maximum execution time for a PHP
script is 30 seconds.
You can set the timeout for PHP
so that your script won't reach the time limit and exit with error.
Methods to increase maximum execution time limit for PHP:
This approach will affect all the PHP
scripts running in your system. It's the best solution if you're running content management system or framework like Wordpress
or Drupal
as you don't need to configure the option manually to all the affected scripts.
Set the max_execution_time
in your PHP
configuration file to the number of seconds that want to allow your PHP
scripts to execute.
php.ini
file using your favourite text editor. $ sudo vi /etc/php/7.2/apache2/php.ini
max_execution_time
directive. ; Maximum execution time of each script, in seconds ; http://php.net/max-execution-time ; Note: This directive is hardcoded to 0 for the CLI SAPI max_execution_time = 30
max_execution_time
in seconds. max_execution_time = 300
Setting it to 0
will impose no time limit whatsoever to the execution of your PHP
scripts.
It's not recommended to set the value to 0
at this level as this method's system-wide change will cause any misbehaved PHP
scripts to unnecessarily consume precious CPU
time.
$ sudo systemctl restart apache2
This is basically the same as the previous solution, but it's actually a function that you call from your PHP
script. It's a general function to override any configuration options set in your PHP
's configuration file and will only affect the execution of the scripts that call the function.
When placed at the start of your PHP
script, the following function call will allow it to run for 300 seconds (5 minutes).
ini_set('max_execution_time', 300)
For CMS and frameworks, it should be placed in shared files such as the configuration files. For Wordpress
it's wp-settings.php
.
This is PHP
's built-in function specifically to set the maximum execution time limit of your PHP
scripts. It's to be called from your PHP
script as in the previous method and the following example is to also set the limit to 300 seconds (5 minutes).
set_time_limit ( 300 )
This is the best option if you don't have administrator access to your system but still want to set the option to all your scripts in your project folder.
Add the following lines to .htaccess
file within your PHP
project folder and this will be applied to all the scripts within that folder.
<IfModule mod_php7.c> php_value max_execution_time 300 </IfModule>
Use mod_php5.c
if you're hosting on PHP 5.
htaccess
is an Apache
-specific solution.
The option is readily available in cPanel
's dashboard and can be used to change PHP
's maximum execution time limit.
Comment anonymously. Login not required.