Course lectures : 1 of 1

Nginx: increase php execution time

Last updated: 8th October 2015

nginxmax_execution_time

Today let's have a look at how we can increase the execution time on the nginx server, which by default is set to something around 1440 seconds = 24 minutes.

I'm working on the Ubuntu 14.04.3 LTS with nginx/1.8.0.

There are 4 steps we need to follow and you have to be logged in as a root user in order to be able to follow them.

Changes to the php.ini

First we will increase values associated with max_execution_time and gc_maxlifetime.
Use the following command to find the line for max_execution_time in php.ini (replacing the path to it if it varies).

grep -n 'max_execution_time' /etc/php5/fpm/php.ini

In my case it returned line 372

372:max_execution_time = 30

Open the file on this line using your favourite editor - I'm going to use nano:

nano +372 /etc/php5/fpm/php.ini

If the line is preceded with a semi-colon ; - remove it. Change 30 to as many seconds as you need - I'm going to increase it to 5 minutes (300 seconds).

max_execution_time = 300

Once you've updated the value, save and exit your editor (Ctrl + x, y, Return).

Next let's find out on which line the gc_maxlifetime is

grep -n 'gc_maxlifetime' /etc/php5/fpm/php.ini

In my case it returned

1455:session.gc_maxlifetime = 1440
1462:; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):

Which indicates that the variable can be found on line 1455. The second instance is a comment so we can ignore it.
Let's open it again on that line.

nano +1455 /etc/php5/fpm/php.ini

Change the value to 18000, which in seconds will equal to 5 hours. Save the file and exit the editor using the same combination as before (or the one right for your chosen editor).

Changes in www.conf

To identify the line that contains the string request_terminate_timeout inside of the /etc/php5/fpm/pool.d/www.conf use the similar command to the one used before:

grep -n 'request_terminate_timeout' /etc/php5/fpm/pool.d/www.conf

In my case it returned

328:request_terminate_timeout = 30

Open file on that line (in my case 328) and change the associated value to 300

nano +328 /etc/php5/fpm/pool.d/www.conf
request_terminate_timeout = 300

Save and close the file.

Changes in the nginx config file

We can increase the time limit for either a single site or globally.
To increase it for a single site open the site's config file (replacing example.com with the actual site's name)

nano /etc/nginx/sites-available/example.com

Navigate to the block which starts with

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    ...
}

and add the line that says

fastcgi_read_timeout 300;

Your block should now look something like

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_read_timeout 300;
    ...
}

Save and close the file.

To set it globally - open /etc/nginx/nginx.conf

nano /etc/nginx/nginx.conf

And somewhere in between the http {...} block add the same as above

fastcgi_read_timeout 300;

giving you something like

http {
    ...
    fastcgi_read_timeout 300;
    ...
}

Last thing to do is to restart php-fpm and nginx

service php5-fpm reload
service nginx reload

COMMENTS

If you'd like to include code example please wrap your code within <pre><code> tags.


<pre>
<code>
function fullName(firstName, lastName)
{
    return firstName + ' ' + lastName;
}
</code>
</pre>