Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:php_run_without_timeout [2014/12/04 19:07]
Michael Armstrong
litespeed_wiki:php_run_without_timeout [2015/07/28 18:51]
Michael Alegre removed
Line 1: Line 1:
 ====== Run PHP without Timeouts ====== ====== Run PHP without Timeouts ======
  
-This wiki covers how to set up LiteSpeed Web Server so that a long-running PHP script will never timeout+This wiki covers how to set up LiteSpeed Web Server so that a long-running PHP script will not be interrupted before it has finished
  
-===== Reason =====+====== Reason ​======
  
 Some PHP scripts need to run for long periods of time without interruption. Examples include WordPress modules such as BackupBuddy,​ ImportBuddy,​ or any other module that relies on a WordPress built-in cron job. Whenever a PHP application rebuilds MySQL indexes, the process may run for a long time. Some PHP scripts need to run for long periods of time without interruption. Examples include WordPress modules such as BackupBuddy,​ ImportBuddy,​ or any other module that relies on a WordPress built-in cron job. Whenever a PHP application rebuilds MySQL indexes, the process may run for a long time.
 +====== How-To'​s ======
  
-In Apache mod_phpthe ignore_user_abort variable allows ​user to trigger ​long-running process ​and then close the browser or navigate away from the page without killing the PHP/​MySQL ​process. ​This environment variable is not supported by LSWS.+Generallyallowing ​PHP script ​to run forever is not desirable. Thus there are number of features (in LSWS and built into PHP) that may prevent a PHP process ​from running long enough to finishYou may need to set up more than one of the following configurations to ensure your application works correctly.
  
-===== How-To'​s ​=====+===== Turn off aborting for a broken connection ​=====
  
-Generally, allowing ​PHP script to run forever is not desirable. Thus there are number of features ​that may prevent a PHP process ​from running as long as possibleYou may need to set up more than one of the following configurations to ensure your application works correctly.+When user closes ​connection (by closing a window, for example), LSWS will abort processing ​that PHP script by killing the PHP process. ​This is to avoid wasting system resources and prevent certain types of DoS attacks.
  
-==== Turn off aborting for a broken connection ==== +In some casesthoughit is preferable to not to abort the PHP script ​regardless of whether the connection has been closed. For example, Wordpress built-in cron jobs start a background job by sending a request to ''​wp-cron.php''​ then immediately closing ​the connection without waiting for responseIn order for the cron job to complete, though, the web server must keep the PHP engine running without interruption.
- +
-When a user closes a connection (by closing a windowfor example)LSWS will abort processing that PHP script by killing ​the PHP processThis is to avoid wasting system resources and prevent certain types of DoS attacks.+
  
-In some casesthough, it is preferable to not to abort the PHP script regardless of whether the connection is still open. For example, Wordpress built-in cron jobs start background job by sending ​request to ''​wp-cron.php'' ​then immediately closing ​the connection ​without ​waiting for response. In order for the cron job to complete, though, the web server must keep the PHP engine running without interruption.+With Apache mod_php, the ignore_user_abort setting allows ​user to trigger ​long-running process and then close the browser or navigate away from the page without ​killing ​the PHP/MySQL process. This setting is not supported by LSWS (or any Apache suEXEC setup).
  
-Aborting for a broken connection can be turned off at the server level in LSWS's WebAdmin console or by using the "​noabort"​ environment variable.+Aborting for a broken connection can instead ​be turned off at the server level in LSWS's WebAdmin console or by using LiteSpeed'​s ​"​noabort"​ environment variable.
  
-=== Globally via the WebAdmin ===+==== Globally via the WebAdmin ​====
  
 WebAdmin console > Configuration > Server > General > External Application Abort WebAdmin console > Configuration > Server > General > External Application Abort
Line 27: Line 26:
 Set External Application Abort to "No Abort" to stop all applications from aborting even when a connection has been broken. Set External Application Abort to "No Abort" to stop all applications from aborting even when a connection has been broken.
  
-=== Through the "​noabort"​ environment variable ===+==== Through the "​noabort"​ environment variable ​====
  
 Aborting for a broken connection can be turned off by using the request-level "​noabort"​ environment variable. This can be done in a rewrite rule or using the SetEnv/​SetEnvIf directives. "​noabort"​ is a LiteSpeed-specifc environment variable. Aborting for a broken connection can be turned off by using the request-level "​noabort"​ environment variable. This can be done in a rewrite rule or using the SetEnv/​SetEnvIf directives. "​noabort"​ is a LiteSpeed-specifc environment variable.
  
-The ''​[E=noabort:​1]''​ flag can be added to any rewrite rule. The rewrite rule can be in an Apache .htaccess file or vhost-level configuration file. The rewrite flag should usually be used in ONE account only. If you need to do a server-level configuration that will apply to all accounts (though perhaps only for certain scripts), you should use the SetEnvIf directive. ​+The ''​[E=noabort:​1]''​ flag can be added to any rewrite rule. The rewrite rule can be in an Apache .htaccess file or vhost-level configuration file. The rewrite flag should usually be used for a single ​account only. If you need to do a server-level configuration that will apply to all accounts (though perhaps only for certain scripts), you should use the SetEnvIf directive. ​
  
-== Rewrite rule examples: ==+=== Rewrite rule examples: ​===
  
   * For all requests.   * For all requests.
Line 46: Line 45:
   ​   ​
  
-== SetEnv/​SetEnvIf directive examples: ==+=== SetEnv/​SetEnvIf directive examples: ​===
  
   * For all requests.   * For all requests.
  
-  SetEnv ​ noabort 1+  SetEnv noabort 1
  
-  * For certain ​URLs (i.e. ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​).+  * For certain ​URIs (i.e. ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​).
  
   SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noabort   SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noabort
  
-**Note:** Rewrite rules cannot be easily inherited. Thus, if you want to turn off aborting for a broken connection for a certain URL in all accounts, the SetEnvIf directive should be used. +**Note:** Rewrite rules cannot be easily inherited. Thus, if you want to affect ​all accounts ​with one setting, the SetEnvIf directive should be used. 
  
-==== LiteSpeed connection timeout override ====+===== LiteSpeed connection timeout override ​=====
  
-If a script does not send back anything for long time, server may close the client connection based on connection timeout, it means that the request could be aborted by the server ​or the client ​cannot see the result even though ​the script ​may continue ​to run till it finishes, depends on whether ​"​noabort"​ has been set or notYou may want to prevent ​connection ​from being timed out some times+If a script does not send back anything for long time, this can trigger a connection timeout ​and the server ​will close the client ​connection. This is done to prevent poorly written PHP scripts from tying up the server. To get desired functionality from your web applications, ​though, you may need to prevent the connection from being timed out. (If the "​noabort" ​environment variable above has been set, the script will continue to run even though the connection has been brokenYour application,​ though, ​may require the connection ​to stay open for correct functionality.)
  
-=== Globally ​via server configuration ===+Connection timeout can be prevented by either increasing the global connection timeout setting (via the WebAdmin console) or using LiteSpeed'​s "​noconntimeout"​ environment variable. ​
  
-  Admin CP => Configuration ​=> Server ​=> Tuning ​=> Connection Timeout (secs)+==== Globally via the WebAdmin ====
  
-=== Through "​noconntimeout"​ environment variable ===+WebAdmin CP > Configuration > Server > Tuning > Connection Timeout (secs)
  
-Similar ​to "​noabort"​ environment,​ you can add "​noconntimeout"​ environment variable via rewrite rule or SetEnv/​SetEnvIf to turn off connection timeout+This setting can be increased to allow scripts to run longer (though they will still time out after the specified time has elapsed). 
 + 
 +==== Through "​noconntimeout"​ environment variable ==== 
 + 
 +Similarly ​to the "​noabort"​ environment ​variable, you can add the "​noconntimeout"​ environment variable via rewrite rule or using the SetEnv/​SetEnvIf ​directives. (The rewrite flag is preferred for controlling a single account. The SetEnv/​SetEnvIf directives are preferred for rules that will apply to all accounts.) "​noconntimeout"​ is a LiteSpeed-specifc environment variable. 
 + 
 +=== Rewrite rule examples: === 
 + 
 +  * For all requests.
  
   RewriteEngine On   RewriteEngine On
   RewriteRule .* - [E=noconntimeout:​1]   RewriteRule .* - [E=noconntimeout:​1]
 +
 +  * For ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​ only.
  
   RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noconntimeout:​1]   RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noconntimeout:​1]
  
-  ​SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noconntimeout +  ​* Combined ​with the "​noabort"​ environment variable.
-   +
- +
-It can be combined ​with "​noabort"​ environment variable.+
  
   RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noabort:​1,​ E=noconntimeout:​1]   RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noabort:​1,​ E=noconntimeout:​1]
  
-equivalent ​SetEnvIf directive ​is+  SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noconntimeout 
 +   
 +=== SetEnv/SetEnvIf directive ​examples: === 
 + 
 +  * For certain URLs (i.e. ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​).
  
   SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noabort noconntimeout   SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noabort noconntimeout
  
 +===== "​LSAPI_MAX_PROCESS_TIME"​ environment variable =====
  
 +In ProcessGroup mode, the [[http://​www.litespeedtech.com/​docs/​litespeed-sapi/​php-lsapi/​configuration|"​LSAPI_MAX_PROCESS_TIME"​ environment variable]] (default 3600 seconds) controls the maximum processing time allowed when processing a request. If a child process cannot finish processing the request in the given time period, it will be killed by the parent process. This option can get rid of a dead or a runaway child process.
  
-==== PHP execution time in php.ini ====+Set the environment variable ​in your external application settings (WebAdmin > Configuration > Server(or Vhost) > External App > your external application > Environments).
  
-      max_execution_time=36000+===== PHP execution time in php.ini =====
  
-This is the time that spent in the PHP process itself (user time), does not include time spent in system calls or network I/O. Usually it takes more time to have it triggered+The max_execution_time setting sets the maximum ​time in seconds a PHP script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. This time does not include time spent in system calls or network I/O (unlike the "​LSAPI_MAX_PROCESS_TIME"​ environment variable above)Thus a process will usually trigger "​LSAPI_MAX_PROCESS_TIME"​ before triggering a max_execution_time setting of a similar length. The default setting is 30.
  
-==== LSAPI_MAX_PROCESS_TIME ​(set this environment variable ​in lsphp5====+Example setting ​(in a php.ini file):
  
-   Admin CP => Configuration => Server(or Vhost) => External App => lsphp5 => Environments +      max_execution_time=36000
- +
-In Self Managed Mode, LSAPI_MAX_PROCESS_TIME (default 3600 seconds) controls the maximum processing time allowed when processing a request. If a child process cannot finish processing the request in the given time period, it will be killed by the parent process. This option can get rid of a dead or a runaway child process.+
  
-==== PHP coding ====+===== PHP coding ​=====
  
-One of our customers ​has had success ​with the following PHP code+One of our customers had success ​making sure a PHP script did not time out by adding ​the following PHP code:
-Code:+
 <​code>​ <​code>​
 <?php <?php
Line 131: Line 140:
 </​code>​ </​code>​
  
-**Note:​** ​ +**Note:** You need to turn off keepalive ​connections ​for this request. This can be done with a rewrite rule.
-You need to turn off keepalive for this request, which can be done with a rewrite rule.+