Php таймаут php ini

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
Читайте также:  Python модуль json методы

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:

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:

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 :

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.

/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.

Find the values between the http brackets

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 ..

Источник

set_time_limit

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini .

When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.

Parameters

The maximum execution time, in seconds. If set to zero, no time limit is imposed.

Return Values

Returns true on success, or false on failure.

Notes

Note:

The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system() , stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.

See Also

User Contributed Notes 25 notes

Both set_time_limit(. ) and ini_set(‘max_execution_time’. ); won’t count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:

my_exec.php:
function my_background_exec ( $function_name , $params , $str_requires , $timeout = 600 )
< $map =array( '"' =>‘\»‘ , ‘$’ => ‘\$’ , ‘`’ => ‘\`’ , ‘\\’ => ‘\\\\’ , ‘!’ => ‘\!’ );
$str_requires = strtr ( $str_requires , $map );
$path_run = dirname ( $_SERVER [ ‘SCRIPT_FILENAME’ ]);
$my_target_exec = «/usr/bin/php -r \»chdir(‘ < $path_run >‘); < $str_requires >\\\$params=json_decode(file_get_contents(‘php://stdin’),true);call_user_func_array(‘ < $function_name >‘, \\\$params);\»» ;
$my_target_exec = strtr ( strtr ( $my_target_exec , $map ), $map );
$my_background_exec = «(/usr/bin/php -r \»chdir(‘ < $path_run >‘); < $str_requires >my_timeout_exec(\\\» < $my_target_exec >\\\», file_get_contents(‘php://stdin’), < $timeout >);\» my_timeout_exec ( $my_background_exec , json_encode ( $params ), 2 );
>

function my_timeout_exec ( $cmd , $stdin = » , $timeout )
< $start = time ();
$stdout = » ;
$stderr = » ;
//file_put_contents(‘debug.txt’, time().’:cmd:’.$cmd.»\n», FILE_APPEND);
//file_put_contents(‘debug.txt’, time().’:stdin:’.$stdin.»\n», FILE_APPEND);

$process = proc_open ( $cmd , [[ ‘pipe’ , ‘r’ ], [ ‘pipe’ , ‘w’ ], [ ‘pipe’ , ‘w’ ]], $pipes );
if (! is_resource ( $process ))
‘1’ , ‘stdout’ => $stdout , ‘stderr’ => $stderr );
>
$status = proc_get_status ( $process );
posix_setpgid ( $status [ ‘pid’ ], $status [ ‘pid’ ]); //seperate pgid(process group id) from parent’s pgid

stream_set_blocking ( $pipes [ 0 ], 0 );
stream_set_blocking ( $pipes [ 1 ], 0 );
stream_set_blocking ( $pipes [ 2 ], 0 );
fwrite ( $pipes [ 0 ], $stdin );
fclose ( $pipes [ 0 ]);

if ( time ()- $start > $timeout )
< //proc_terminate($process, 9); //only terminate subprocess, won't terminate sub-subprocess
posix_kill (- $status [ ‘pid’ ], 9 ); //sends SIGKILL to all processes inside group(negative means GPID, all subprocesses share the top process group, except nested my_timeout_exec)
//file_put_contents(‘debug.txt’, time().»:kill group \n», FILE_APPEND);
return array( ‘return’ => ‘1’ , ‘stdout’ => $stdout , ‘stderr’ => $stderr );
>

$status = proc_get_status ( $process );
//file_put_contents(‘debug.txt’, time().’:status:’.var_export($status, true).»\n»;
if (! $status [ ‘running’ ])
< fclose ( $pipes [ 1 ]);
fclose ( $pipes [ 2 ]);
proc_close ( $process );
return $status [ ‘exitcode’ ];
>

usleep ( 100000 );
>
>
?>

a_class.php:
class A
static function jack ( $a , $b )
< sleep ( 4 );
file_put_contents ( ‘debug.txt’ , time (). «:A::jack:» . $a . ‘ ‘ . $b . «\n» , FILE_APPEND );
sleep ( 15 );
>
>
?>

test.php:
require ‘my_exec.php’ ;

my_background_exec ( ‘A::jack’ , array( ‘hello’ , ‘jack’ ), ‘require «my_exec.php»;require «a_class.php»;’ , 8 );
?>

Источник

Overriding php.ini timeout values with your PHP script’s execution time limit

We’ve enhanced the appearance of our portal and we’re working on updating screenshots. Things might look different, but the functionality remains the same.

When working with PHP scripts, let’s learn what the maximum execution time setting is — and how to override the php.ini timeout value that is set with the max_execution_time variable in the php.ini settings file.

By default, the maximum execution time setting is derived from the max_execution_time variable within the php.ini timeout set of definitions, which are found in the php.ini file. But you can change the maximum allowed execution time for your individual PHP scripts. This article will show you how.

What is the PHP limit time?

One important control involved with PHP scripts is that they will have a maximum amount of time set that they can take to execute. The time limit (as known as the php.ini timeout) varies depending on the hosting company and how the maximum execution time variable has been configured globally for all PHP scripts — or individually per PHP script.

How do I fix a PHP timeout?

You can fix a PHP timeout by following the guidance in this article. You can work our Nexcess support staff to increase your php,ini timeout or example. Also, there are user.ini file settings that can be used — per directory on in the webroot.

Increasing the timeout in php.ini or user.ini file by adding a line that increases the maximum execution time is a standard method. However, you can also increase the timeout in your script itself my taking the appropriate steps documented in this article.

How to set maximum execution time

Overriding the default php.ini timeout values with your own PHP script’s execution time limit is often useful and necessary step for a website admin to take. We need to learn how to set the maximum execution time using your individual PHP script’s execution time limit — overriding the php.ini timeout set with the max_execution_time variable.

PHP scripts: maximum execution time definition

Maximum execution time refers to the default maximum amount of time a PHP script will run before PHP terminates the script and reports an error.

PHP scripts: how to change maximum execution time

You may use one of three possible methods to alter your maximum execution time. Those three methods are described below.

Method #1: Modify the php.ini file (via the php.ini timeout settings)

We do not permit our clients with managed hosting plans to modify their php.ini files or the php.ini timeout settings in them, but our support team may implement changes on your behalf.

Method #2: Set maximum execution time for individual PHP scripts (by overriding the default php.ini timeout settings)

You may adjust maximum execution time via PHP’s set_time_limit function. This value replaces the max_execution_time variable within the php.ini file.

For example, to change the execution time limit for a particular PHP script to ten minutes, convert the time limit to seconds add the following line at the beginning of the script:

Method #3: Set maximum execution time for PHP scripts from the CLI (by overriding the default php.ini timeout settings)

You may set the maximum execution time manually from the command line interface (CLI) by using the -d flag with the PHP command as follows:

The above command would run script.php with a maximum execution time of 300 seconds regardless of the globally set max_execution_time value.

We hope that after reading this article you learned more about the maximum execution time setting with regard to PHP scripts and how to override the php.ini timeout value that is set with the max_execution_time variable in the php.ini setting file. You can change the maximum allowed execution time for your PHP scripts as this article outlined.

If you need this change made globally, contact our support team by email at support@nexcess.net for assistance.

Nexcess, the premium hosting provider for WordPress, WooCommerce, and Magento, is optimized for your hosting needs. Nexcess provides a managed hosting infrastructure, curated tools, and a team of experts that make it easy to build, manage, and grow your business online. Serving SMBs and the designers, developers, and agencies who create for them, Nexcess has provided fully managed, high-performance cloud solutions for more than 22 years.

Источник

Оцените статью