Increase script execution time. Increased execution time of a PHP script with Nginx. Why measurements in programming?

If you have a large project or a server with limited resources, then you will often see the “504 Gateway Time-out” error.

You can follow the steps below to increase the timeout value. In PHP the default is 30 seconds.

Changes in php.ini

If you want to change the maximum execution time for PHP scripts from 30 seconds (default) to 300 seconds.

Nano /etc/php5/fpm/php.ini

Install...

Max_execution_time = 300

In Apache PHP runs as a module, these changes will be enough. But in our case, we need to make changes in several more places.

Changes in PHP-FPM

This is necessary if you uncommented the request_terminate_timeout parameter earlier. By default it is commented out and takes the max_execution_time found value from php.ini

Opening...

Nano /etc/php5/fpm/pool.d/www.conf

Installing...

Request_terminate_timeout = 300

Changes in Nginx Config

Increasing the time limit for one domain example.com

Nano /etc/nginx/sites-available/example.com location ~ \.php$ ( include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_read_timeout 300; }

If you want to increase the time-limit for all domains on your server, you can edit the main nginx.conf configuration file:

Nano /etc/nginx/nginx.conf

Add the following parameter to the http(..) section

Http(#... fastcgi_read_timeout 300; #... }

Have you ever received an error Fatal Error: Maximum execution time of 30 seconds exceeded in WordPress? This error means that the running PHP script required more time to complete than the limit set and the script was stopped due to this. This guide will help you understand the question of how to increase script execution time in PHP and thereby solve the error that has arisen.

Before starting this guide, check that you have:

  • Access to the Administrative Console of your WordPress site (English)

Step 1 – Install the WP Maximum Execution Time Exceeded Plugin

WordPress has a huge community, so there's a handy plugin or several for every functionality you need. Our situation is no exception; to increase the script execution time in PHP, you can use the WP Maximum Execution Time Exceeded plugin and fix the error that has arisen. Detailed instructions on how to install the WordPress plugin can be found.

This plugin makes changes to the .htaccess file and increases the script execution time limits. After installing the plugin, you don't need to do anything else, everything is already done by the plugin. The plugin does not have a settings page, so if you want to return the previous settings, simply delete the plugin and .htaccess will be returned to its original form.

Step 2 – Editing the .htaccess file manually

If you already have a lot of plugins installed and you don’t want to use an additional one or it just didn’t help you get rid of the error, then you can make changes to the .htaccess file manually to increase the maximum execution time limit. The easiest way to use File manager located in your Control Panel. If you don't know how to find the .htaccess file, please refer to the detailed instructions. You will need to enter the following code in the .htaccess file at the bottom and save it by clicking Save:

Php_value max_execution_time 60

This will increase the value of the max_execution_time parameter and, for PHP, the script execution time to 60 seconds (1 minute). If you still get this error, then try increasing it even more - up to 120/180 seconds and so on.

Step 3 – Changing the max_execution_time value in cPanel

If you are using cPanel, then you can increase the max_execution_time parameter directly through cPanel. Go to section Select PHP Version (Select PHP version).

Increase the value you see in the line max_execution_time. Usually, it is set to 30 seconds by default, in our example we increased it to 60 seconds. Click Apply(Apply) to save the changes.

That's all, you have successfully increased the limit parameter value max_execution_time for your website.

Conclusion

By following this guide, you have learned 3 ways to increase max_execution_time for WordPress, to change the script execution time in PHP and get rid of the error Fatal Error: Maximum execution time exceeded. If you are using shared hosting and none of the solutions work for you, then contact your hosting provider to check what your actual restrictions are and whether they can be changed at all.

If you are developing a large project, then it may take a lot of time to process some data. Different hosting sites have their own limits on such actions, and if the need to increase this time brought you here, then I will tell you in detail how this can be done.

So that you understand what we are talking about, I will give a small example. If you go beyond the set time frame, you may receive the following error:

Fatal error: Maximum execution time of 30 seconds exceeded in site.php on line 19

I'll tell you about 4 ways to get rid of it.

But it’s worth starting with the fact that in each individual case, all of the listed methods may not work, since some hosting providers prohibit changing this parameter themselves.

Increasing the execution time of a PHP script through the php.ini configuration file

As the first way to increase the execution time of the script, we will use the configuration file php.ini.

To find out exactly where this file is located, read.

Open it in a way convenient for you and insert at the very bottom:

Max_execution_time = 300;

Where " 300 "means 300 seconds (change to yours). This file, unfortunately, is not available for editing by users on all hosting sites.

Increasing the execution time of a PHP script through the built-in “ini_set” function

The second method is based on using the function " ini_set" You insert it directly into the script file itself, preferably at the very top. Example:

Here the meaning is " 300 "You also change to what you need in seconds. note that when using PHP in safe mode, this function will not be available.

Increasing the execution time of a PHP script through the built-in function “set_time_limit”

The third method, probably one of the most popular, is to use the “ set_time_limit» to change the execution time of the script. Also used directly in the PHP file itself. Example:

Here " 300 "you also change to the value you need. It is advisable to insert the code at the very top.

Increasing the execution time of a PHP script through the .htaccess file

The last option is to edit the file .htaccess, which is located at the root of your site (if it doesn’t exist, create it).

At the very top of this file paste:

Php_value max_execution_time 300

Meaning " 300 "You also change it to yours.

note, that in all cases the script execution time is indicated in seconds. In addition, the parameter value in all methods can take " 0 ", which means unlimited execution time.

How can I find out how much time is allocated to execute a PHP script?

After you have tried to change the execution time of the script using one of the methods, you need to find out whether you really succeeded.

To do this, create a PHP file in the root of your site, where you will output the value of the set time:

If the time is equal to what you indicated, congratulations, you have achieved your desired goal. Otherwise, read the article again, write in the comments about your problem or forward it to your hosting provider.

If you are developing a large project, then it may take a lot of time to process some data. Different hosting sites have their own limits on such actions, and if the need to increase this time brought you here, then I will tell you in detail how this can be done.

So that you understand what we are talking about, I will give a small example. If you go beyond the set time frame, you may receive the following error:

Fatal error: Maximum execution time of 30 seconds exceeded in pandoge_com.php on line 19

I'll tell you about 4 ways to get rid of it.

But it’s worth starting with the fact that in each individual case, all of the listed methods may not work, since some hosting providers prohibit changing this parameter themselves.

Increasing the execution time of a PHP script through the php.ini configuration file

As the first way to increase the execution time of the script, we will use the configuration file php.ini.

To find out exactly where this file is located, read.

Open it in a way convenient for you and insert at the very bottom:

Max_execution_time = 300;

Where " 300 "means 300 seconds (change to yours). This file, unfortunately, is not available for editing by users on all hosting sites.

Increasing the execution time of a PHP script through the built-in “ini_set” function

The second method is based on using the function " ini_set" You insert it directly into the script file itself, preferably at the very top. Example:

Here the meaning is " 300 "You also change to what you need in seconds. note that when using PHP in safe mode, this function will not be available.

Increasing the execution time of a PHP script through the built-in function “set_time_limit”

The third method, probably one of the most popular, is to use the “ set_time_limit» to change the execution time of the script. Also used directly in the PHP file itself. Example:

Here " 300 "you also change to the value you need. It is advisable to insert the code at the very top.

Increasing the execution time of a PHP script through the .htaccess file

The last option is to edit the file .htaccess, which is located at the root of your site (if it doesn’t exist, create it).

At the very top of this file paste:

Php_value max_execution_time 300

Meaning " 300 "You also change it to yours.

note, that in all cases the script execution time is indicated in seconds. In addition, the parameter value in all methods can take " 0 ", which means unlimited execution time.

How can I find out how much time is allocated to execute a PHP script?

After you have tried to change the execution time of the script using one of the methods, you need to find out whether you really succeeded.

To do this, create a PHP file in the root of your site, where you will output the value of the set time:

If the time is equal to what you indicated, congratulations, you have achieved your desired goal. Otherwise, read the article again, write in the comments about your problem or forward it to your hosting provider.

From the author: my friend’s son became so interested in chemistry that he began conducting various “tests” at home. After the chandelier in the kitchen became their “victim,” his father asked him to change his preferences. As a result, the child became interested in statistics and decided to calculate the average burning time of one match. The result was that the entire apartment almost caught fire. I advised the would-be scientist to start programming and set the execution time of the PHP script.

Why measurements in programming?

First of all, it is safer than chemistry and statistics. But in fact, the execution time of program code is an important parameter that affects the performance of the entire resource and its individual modules. This parameter is more in demand during optimization and testing.

Let's imagine a little. Let’s imagine that there is some kind of “home-written” engine, which the developer “uploaded” to a good hosting and launched (based on it) his website. But for some reason, loading the resource on the user side is very slow.

In such a situation, initially all the bumps will fall on the hoster. Most likely, an angry programmer will file a complaint with the hosting support service. They say that the terms of the contract are not being fulfilled, the site is barely “alive”...

But in fact, the problem of slow loading pages of a dynamic resource may be hidden in the engine itself. And in order not to look like a “dummie” later, an experienced developer always measures the execution time of a PHP script before delivering a project.

PHP Features

I have always called it one of the most convenient programming languages. To track the running time of the code, the developer does not need to write long “talmuds” to intercept each line of the script. This technology implements several specialized tools. The main one is the microtime() function. It returns the time (in milliseconds) that has passed since the beginning of 1970. The so-called “beginning of the Unix era.” If you pass the get_as_float parameter to the function with the value true, then it will return the time period in the form of whole seconds and fractional milliseconds.

The principle of using microtime() is as follows: the function is called at the beginning of the script and at the end of it. Then the initial value is subtracted from the last value. Here's how to calculate the execution time of a script:

"; $last=microtime(true); $value=$last-$first; echo "The script is executed in: ".$value; ?>

Changing the running time of the code

In PHP, the script running time value can be limited programmatically (decrease or increase). To do this, several functions are used, each of which overwrites a parameter set in the configuration file (php.ini).
First, let's modify our code and increase the number of loop iterations by 20 times to 200 million. If everything is fine, then after running the script you should receive a fatal error message.

This means that in the settings the max_execution_time parameter is set to the default value (30 seconds). Let's increase the maximum execution time of a script in PHP by 3 times.

set_time_limit(90);

Now we don't receive any error messages.