Running PHP in Detached Mode

You are here:
< Back

What’s PHP Detached Mode?

PHP Detached Mode is new in OpenLiteSpeed v1.5.6.

In previous versions, all PHP processes were attached to OpenLiteSpeed processes. As such, when OpenLiteSpeed restarted, so would the PHP processes. In PHP Detached Mode, the PHP processes will continue running independently, even when OpenLiteSpeed restarts.

When you run in PHP SuEXEC ProcessGroup Mode, PHP process groups are still started by the openlitespeed process. When the first request for a specific PHP process group comes in, if PHP is not running it, it will be started by OpenLiteSpeed. pstree will show that PHP process is child of the openlitespeed process. That’s normal. What’s new is that, as of 1.5.6, once the openlitespeed process exits, the PHP process won’t quit. It will become independent.

Benefits of PHP Detached Mode

PHP Detached Mode provides an advantage, especially when hundreds of accounts are hosted on the same shared hosting server, with new accounts being rapidly added to it. In cases like this, there is no need to restart all PHP processes during the web server restart. Also, when a user is heavily relying on opcode to reduce the server load, PHP Detached Mode avoids an opcode cache reset during the server restart.

How to Enable PHP Detached Mode

If you run lsphp from the server level as nobody:nobody, Detached Mode may have been enabled by default when you installed/upgraded OLS to v1.5.6 or above. For each virtual host running as PHP SuEXEC, you may need to explicitly configure an external application, and then set Run on Startup to PHP Detached Mode.

Restart DetachedPHP Processes

In the past, if there were any php.ini changes, you would probably want to restart OLS to apply the changes, since OLS would also restart PHP processes. Under Detached Mode, this is no longer an option.

If you want to make php.ini changes effective immediately, there are a few ways to restart PHP processes.

For a User

To restart detached PHP processes for the account (vhost) level, you can touch a .lsphp_restart.txt file under the user’s home directory, for example /home/USER1/.

touch /home/USER1/.lsphp_restart.txt

Once .lsphp_restart.txt is created, the user’s PHP will be restarted when the next request comes in. The file .lsphp_restart.txt won’t be removed. OLS will check the timestamp of the file to decide if the user’s detached PHP needs to be restarted or not. You can manually remove it if you want to, but it’s not necessary. Every time you want to restart that user’s detached PHP, you can just touch the file again, whether it already exists or not, in order to refresh the timestamp.

For the Server

To restart detached PHP processes for the server level, you can touch a .lsphp_restart.txt file under the <lsws_server_root>/admin/tmp/ directory. Usually, that is /usr/local/lsws/admin/tmp/.

touch /usr/local/lsws/admin/tmp/.lsphp_restart.txt

All running detached PHP processes will be restarted, but not immediately. Instead, they will restart as soon as the server needs to use that PHP handler.

Stop All Immediately

To stop all lsphp processes immediately, you can manually kill them from the command line:
killall -9 lsphp

PHP Processes Won’t Run Forever

PHP Detached Mode doesn’t mean that PHP will run forever. It will still follow the “Max Idle Time” setting in the external app. If you want to make PHP live longer, just increase Max Idle Time. Never set it to -1 to indicate “unlimited” since, in process group mode, values under 30 will be automatically converted to the default of 30 seconds. If you want PHP to be running longer, try to enter a large number, such as 3600.

Verify Detached Mode

To verify that PHP is running in Detached Mode, you can check the lsphp processes. When OLS restarts, if existing lsphps are still running instead of exiting, then PHP is running detached.

test/# ps -ef | grep lsphp
john     24432 24406  0 14:28 ?        00:00:00 lsphp
john     24433 24432  0 14:28 ?        00:00:00 lsphp
root     24457 24435  0 14:29 pts/0    00:00:00 grep --color=auto lsphp

test/# service lsws restart

test/# ps -ef | grep lsphp
john     24432     1  0 14:28 ?        00:00:00 lsphp
john     24433 24432  0 14:28 ?        00:00:00 lsphp
nobody   24482 24480  0 14:29 ?        00:00:00 lsphpnew
root     24485 24435  0 14:29 pts/0    00:00:00 grep --color=auto lsphp