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 20:25]
Michael Armstrong [Through noconntimeout environment variable]
— (current)
Line 1: Line 1:
-====== Run PHP without Timeouts ====== 
  
-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 ====== 
- 
-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 ====== 
- 
-Generally, allowing a PHP script to run forever is not desirable. Thus there are a number of features (in LSWS and built into PHP) that may prevent a PHP process from running long enough to finish. You may need to set up more than one of the following configurations to ensure your application works correctly. 
- 
-===== Turn off aborting for a broken connection ===== 
- 
-When a user closes a 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. 
- 
-In some cases, though, it 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 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 a user to trigger a 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 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 ==== 
- 
-WebAdmin console > Configuration > Server > General > External Application Abort 
- 
-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 ==== 
- 
-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 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: === 
- 
-  * For all requests. 
- 
-  RewriteEngine On 
-  RewriteRule .* - [E=noabort:​1] 
- 
-  * For ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​ only. 
- 
-  RewriteEngine On 
-  RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noabort:​1] 
-  ​ 
- 
-=== SetEnv/​SetEnvIf directive examples: === 
- 
-  * For all requests. 
- 
-  SetEnv noabort 1 
- 
-  * For certain URIs (i.e. ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​). 
- 
-  SetEnvIf Request_URI "​(wp-cron|backupbuddy|importbuddy)\.php"​ noabort 
- 
-**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 ===== 
- 
-If a script does not send back anything for a 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 broken. Your application,​ though, may require the connection to stay open for correct functionality.) 
- 
-Connection timeout can be prevented by either increasing the global connection timeout setting (via the WebAdmin console) or using LiteSpeed'​s "​noconntimeout"​ environment variable. ​ 
- 
-==== Globally via the WebAdmin ==== 
- 
-WebAdmin CP > Configuration > Server > Tuning > Connection Timeout (secs) 
- 
-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 a 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 
-  RewriteRule .* - [E=noconntimeout:​1] 
- 
-  * For ''​wp-cron.php'',​ ''​backupbuddy.php,''​ and ''​importbuddy.php''​ only. 
- 
-  RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noconntimeout:​1] 
- 
-  * Combined with the "​noabort"​ environment variable. 
- 
-  RewriteRule (wp-cron|backupbuddy|importbuddy)\.php - [E=noabort:​1,​ E=noconntimeout:​1] 
- 
-  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 
- 
-===== "​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. 
- 
-Set the environment variable in your external application (WebAdmin > Configuration > Server(or Vhost) > External App > your external application > Environments). 
- 
-===== PHP execution time in php.ini ===== 
- 
-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. 
- 
-Example setting (in a php.ini file): 
- 
-      max_execution_time=36000 
- 
-===== PHP coding ===== 
- 
-One of our customers had success making sure a PHP script did not time out by adding the following PHP code: 
-<​code>​ 
-<?php 
-//avoid apache to kill the php running 
-ignore_user_abort(true);​ 
-//start buffer output 
-ob_start(); 
- 
-echo "show something to user"; 
-//close session file on server side to avoid blocking other requests 
-session_write_close();​ 
- 
-//send length header 
-header("​Content-Length:​ "​.ob_get_length());​ 
-header("​Connection:​ close"​);​ 
-//really send content, can't change the order: 
-//1.ob buffer to normal buffer, ​ 
-//2.normal buffer to output 
-ob_end_flush();​ 
-flush(); 
-//continue do something on server side 
-ob_start(); 
-//replace it with the background task 
-sleep(50); ​ 
-ob_end_clean();​ 
-?> 
-</​code>​ 
- 
-**Note:** You need to turn off keepalive connections for this request. This can be done with a rewrite rule. 
  • Admin
  • Last modified: 2014/12/04 20:25
  • by Michael Armstrong