Wiki
 

PHP run without timeouts

Summary:

In some cases, we want PHP script to run as long as possible without any timeouts.

Reason:

For example, when a php application rebuilds MySQL indexes, the process may run for a long time. In Apache mod_php, the ignore_user_abort variable allows the user to trigger this long-running process and then close the browser or navigate away from the page without killing the PHP/MySQL process.

How-To's

There are a number of methods for keeping a PHP process running. Some may be more suitable for different situations.

Turn off aborting for a broken connection

WebAdmin console > Configuration > Server > General > External Application Abort

Set this to “No Abort” to stop applications from aborting even when a connection has been broken.

This can also be set in an Apache .htaccess file:

RewriteEngine On
RewriteRule .* - [E=noabort:1]

LiteSpeed connection timeout override

    Admin CP => Configuration => Server => Tuning => Connection Timeout (secs)

PHP execution time in php.ini

  
    max_execution_time=36000

LSAPI_MAX_PROCESS_TIME (set this environment variable in lsphp5)

    Admin CP => Configuration => Server(or Vhost) => External App => lsphp5

In Self Managed Mode, LSAPI_MAX_PROCESS_TIME (default 300 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

One of our customers has had success with 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();
?>

Note: You need to turn off keepalive for this request, which can be done with a rewrite rule.

 
litespeed_wiki/php_run_without_timeout.txt · Last modified: 2014/07/01 11:18 by Michael Armstrong