The max_execution_time directive in PHP limits how long a script can run before it stops. By default, this is set to 30 seconds. If a script runs longer, PHP will terminate it and display a fatal error.

Fatal error: Maximum execution time of 30 seconds exceeded in yourscript.php

You may need to increase this time limit for scripts that require more processing time. This can be done by adjusting the max_execution_time setting in php.ini, using the ini_set() or set_time_limit() functions in your script, or modifying the .htaccess file.

Different methods are available depending on your server setup or access level. For example, you can configure max_execution_time directly from the cPanel interface if you use a hosting service.

Change PHP maximum execution time limit in php.ini

The php.ini file is the primary configuration file for PHP. Adjusting the max_execution_time directive here will apply the change to all PHP scripts on the server. This method is ideal for users with administrative access to their server, as it allows a system-wide configuration change.

  1. Open the php.ini file using a text editor.
    $ sudo vi /etc/php/7.2/apache2/php.ini
  2. Find the 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
  3. Set the value to the desired number of 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.

  4. Save the changes and exit the editor.
  5. Restart the web server to apply the changes.
    $ sudo systemctl restart apache2

Change PHP maximum execution time limit via ini_set() function

The ini_set() function allows you to override the max_execution_time setting within a specific PHP script. This method is useful when you need to increase the execution time for a single script without affecting the entire server.

Add the ini_set() function to the top of your PHP script to set the desired execution time limit. This change will only apply to the script where it is included, allowing the rest of the server to maintain the default configuration. An example of the function call is as follows:

ini_set('max_execution_time', 300);

The function call should be placed in shared files such as the configuration files for CMS and frameworks. For WordPress it's wp-settings.php.

Change PHP maximum execution time limit via set_time_limit() function

The set_time_limit() function is specifically designed to control the execution time of a script. It resets the script's execution time limit whenever it's called, making it effective for handling longer processes within a PHP script.

In your PHP script, you can use the set_time_limit() function to set the maximum execution time. This function can be called multiple times within the script, allowing you to reset the execution timer during long processes. An example usage is:

set_time_limit(300);

Change PHP maximum execution time limit in .htaccess

The .htaccess file provides a way to configure the server for a specific directory. This method is beneficial for users without administrative access, allowing them to change the max_execution_time for all scripts within a particular directory.

To increase the maximum execution time for all scripts within a specific directory, edit the .htaccess file in that directory. Add the following line to set the desired time limit:

php_value max_execution_time 300

This method is especially useful when you do not have access to the server's php.ini file or need to apply the change to a specific project folder.

htaccess is an Apache-specific solution.

Change PHP maximum execution time limit for cPanel hosting

cPanel provides a web-based interface for managing your hosting environment. It includes an option to adjust the max_execution_time for PHP scripts. This method is ideal for users who prefer a graphical interface over editing configuration files manually.

Discuss the article:

Comment anonymously. Login not required.