How to Increase PHP Max Execution Time

How to configure the max execution time for your server

Avatar

Configuring the maximum execution time is a crucial task for optimizing the performance and reliability of software and web applications. The max execution time setting determines the maximum duration a script or process can run before it is terminated. This is particularly important for preventing resource-intensive scripts from causing server overloads or timeouts. To configure the max execution time, you can typically find a setting in your server’s configuration or in your scripting language’s configuration files, such as PHP.

In PHP, you can adjust this setting using the “max_execution_time” directive in the php.ini file. By setting an appropriate value, you can ensure that your scripts have sufficient time to complete their tasks without causing performance issues. This simple configuration adjustment plays a significant role in maintaining the stability and responsiveness of your applications, making it an essential aspect of software development and web hosting.

 

1. Modify max_execution_time in php.ini

The php.ini configuration file contains the default settings for your PHP installation. You can increase the max execution time by updating the max_execution_time directive in this file.

  • Locate the php.ini file on your server.
  • Open the file and search for the max_execution_time directive.
  • Change its value to the desired number of seconds (e.g., 300 seconds for 5 minutes):

  • Save the file and restart your web server for the changes to take effect.

 

2. Modify max_execution_time in .htaccess (for Apache web server)

If your website is hosted on an Apache web server, you can modify the .htaccess file in your website’s root directory to increase the max execution time.

  • Create or open the .htaccess file in your website’s root directory.
  • Add the following line to set the timeout value to 300 seconds:
  • Save the file.

 

3. Use ini_set() in your PHP script

You can also set the max execution time programmatically within your PHP script using the ini_set() function. Add the following line at the beginning of your script:

 

4. Use set_time_limit() in your PHP script

The set_time_limit() function allows you to set the max execution time for a particular script.

This function accepts an integer parameter, which represents the number of seconds the script should be allowed to run. For example, to set the timeout value to 300 seconds:

 

According to the PHP manual, the set_time_limit() function restarts the timeout counter from zero. In other words, if the default max_execution_time is 30 seconds, and you call set_time_limit(300) at the beginning of your script, your script will now have a total of 330 seconds to run.

 

5. Modify php.ini settings via a custom configuration file

This works only if you are using a hosting that allow to create a custom php.ini file to override the default settings.

  • Create a new file named php.ini or .user.ini in your website’s root directory. The file name depends on your host’s configuration, please refer to its documentation to know wich to use.
  • Add the following line to set the max execution time to 300 seconds:

 

  • Save the file.
  • This may be the only line in the new file. All other directives will be loaded from the original php.ini, and the corresponding directives in your newly created file will override their counterparts.

 

Leave a Reply

Your email address will not be published. Required fields are marked *