How to increase PHP timeout values in Apache and NGNIX

There comes a time when you really need to increase the amount of time it takes before your server stops a particular PHP process from continuing. This is called PHP session “timeout”.

Under normal conditions you want to keep these values low so your connections are as efficient as possible. These limits are also in place to prevent poorly written scripts from monopolizing too many resources on your server.

However, there’s plenty of reasons you would want to increase these values – usually temporarily.

Reasons may include:

  • long import processes (e.g. one-time or initial loads of data)
  • lengthy upgrade processes that need to finish or doom your application to a broken status
  • ridiculous AJAX functions with limited or no error handling
  • processing of feeds or other data where the scripts are too dumb to stop and take a break
  • you crave instability in your life

Regardless of your need, there’s a hierarchy of places where you have to make these adjustments.

Changing PHP Timeouts in APACHE DSO

Starting with Apache, you first have to determine which PHP handler you are working with. Under DSO – your server PHP.ini file is going to be where you can manipulate this value.

Find the following values:

max_input_time = 120

&

max_execution_time = 120

The value “max_input_time” controls the total time a PHP script will spend on parsing input data. If this value is not present, then PHP should default to 60 seconds. You can increase this value as much as you want – however – understand that exceedingly high values will tie up resources like you wouldn’t believe.

“max_input_time” is the time spent prior to starting the “max_execution_time” on a PHP script.

“max_execution_time” is the total amount of time that a PHP script is allowed to process. When you set this higher, it allows all scripts to run as long as the value in seconds you place here.

If you can not set additional limits in your application itself – this should be the only place you need to make changes to PHP.ini under DSO.

How to find the path to your PHP.ini file

Finding the path is not always as easy as you would think. Some servers wind up a bit messy and multiple php.ini files exist. To find yours in the fastest way possible open up an SSH prompt and type the following:

php -i

This command outputs out of the PHP configuration values and information much like the phpinfo(); command would produce. You can find the path to your php.ini file buried in this output. You may want to just grep the information with the following command:

php -i | grep php.ini

You should see an output that looks something like this:

Configuration File (php.ini) Path => /usr/local/lib/php.ini

Changing PHP Timeouts under suPHP / FastCGI

suPHP is a PHP handler that forces each process to run under the account owner of that process. It is done for better security across virtual hosts and is the most common configuration in any multi-domain or shared hosting environment. FastCGI is a similiar PHP handler and handles changes to php.ini in the same manner.

What is unique about suPHP is that each individual domain or vhost can have its own PHP.ini files that further refines changes and adjustments in your PHP configuration. Note – you only have to add you changes to this file that you want overridden or changed. The server will default to the server’s PHP.ini for all functions not specified in the PHP.ini file.

Changing PHP Timeouts in NGNIX

Changes to PHP values in NGNIX are handled a little different. You first find your php.ini file and make the same changes to input and execution times as described above.

Changes in PHP-FPM

If you are using PHP-FPM (Fast Process Manager) you need to navigate to :

 

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

Find and edit:

request_terminate_timeout = 300

Changes in your NGINX Config

One last place to change.. and you need to do this for each site you want to make the increase on.

Find the lines for each domain in your site-available file.

e.g.

/etc/nginx/sites-available/some-domain-you-want-to-change.com
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
}

To increase the timeout values for all sites globally you need to edit your main nginx.conf file.

Open: /etc/nginx/nginx.conf

Find the values between the http{..} brackets

http {
#…
fastcgi_read_timeout 300;
#…
}

You then need to restart/reload PHP-FPM & Nginx to make the changes active.

service nginx reload
service php5-fpm reload

Changes in your Apache Config

If you are using Apache, you need to go into your httpd.conf file and locate:

Timeout 600 (or whatever the value is currently set at) and increase this as much as you desire.

That’s it ..

 

Want to discuss a project?

  • This field is for validation purposes and should be left unchanged.