A dedicated PHP LiteSpeed SAPI has been developed to push LiteSpeed PHP performance to next level by taking advantage the performance advantage of LiteSpeed SAPI. PHP LiteSpeed SAPI is the best PHP server API with the combination of best performance, best security and best flexibilities.
- Best Performance
LiteSpeed PHP delivers up to 30% better performance than FastCGI PHP and 50% better performance than Apache mod_php. LiteSpeed PHP process is persistent and works well with PHP opcode cache and loader, such as APC, eAccelerator, XCache, ionCube Loader, Zend Optimizer, etc.
- Best Security
LiteSpeed PHP can be started in suEXEC mode, it is a lot easier to secure the file system in a shared hosting environment. Jail PHP process in a chroot is possible.
- Best Flexibilities
Same as mod_php, LiteSpeed PHP support changing PHP configurtion via web server configuration and .htacess file.
PHP processes are dynamically spawned based on current load, server resources has been used more efficiently.
PHP4 and PHP5 can be setup in the similar way on one server, switching between PHP4 and PHP5 are just a simple directive in .htaccess
addType application/x-httpd-php5 php
to run *.php as PHP5 script instead of PHP4.
The new SAPI code has been contributed to PHP project, hopefully, it will be distributed along with PHP source code package soon. In the mean time, you need to apply a small patch to build PHP with LiteSpeed SAPI module. And 'autoconf' and 'automake' are required. Detail installation instruction is available at
PHP 4/5 with LSAPI Compile/Install How-To
Latest PHP LiteSpeed SAPI module can be downloaded here
Start PHP from command line
Usually, lsphp is managed by LiteSpeed web server in a single server installation. lsphp can be used in clustered environment with one LiteSpeed web server at the front, load balancing lsphp processes running on multiple backend servers. In such environment, lsphp can be start manually from command with option "-b ", socket address can be IPv4, IPv6 or Unix Domain Socket address.
./lsphp -b [::]:3000
have lsphp bind to port 3000 on all IPv4 and IPv6 address,
./lsphp -b *:3000
have lsphp bind to port 300 on all IPv4 address.
./lsphp -b 192.168.0.2:3000
have lsphp bind to address 192.168.0.2:3000.
./lsphp -b /tmp/lsphp_manual.sock
have lsphp accept request on Unix domain socket "/tmp/lsphp_manual.sock"
Using LiteSpeed PHP with LiteSpeed Web Server
Usually, PHP support has been configured out of box for PHP4, if you want to use PHP5, you can either replace lsphp exectuable with a PHP5 binary, or add configuration for PHP5 if you want to use both PHP4 and PHP5.
Brief instructions are as follow:
- Login to web administration interface, go to 'Server'->'Ext App' tab, add an external application of type "LSAPI app", "Command" should be set to a shell command that executes the PHP binary you just built. "Instances" should be set to "1". Add "LSAPI_CHILDREN" environment variable to match the value of "Max Connections". Do not set it to a large value, '20' is a good value to start with. More tunable environment variable described below can be added.
- Go to 'Server'->'Script Handler' tab, add a script handler configuration: set 'suffix' to 'php' or 'php5', 'Handler Type' to 'LiteSpeed API', 'Handler Name' should be the name of external application just defined.
- Click 'Apply Changes' link on the top left of the page, then click 'graceful restart'. Now PHP is running with LiteSpeed SAPI.
Generate formated PHP source code in html
Since 4.3 release of LiteSpeed PHP SAPI module, generating of formated PHP source code in html can be turned on for a process with a command line option
If you want to have LiteSpeed generate PHP source code for files with extention like "phps" or be set stting MIME type to "x-httpd-phps", you should add a dedicate external application and a script handler for "phps". The "Command" configuration for that external application should be set to include the "-s" command line option.
There are a few environment variables that can be tweaked to control the behavior of LSAPI application.
- LSAPI_CHILDREN or PHP_LSAPI_CHILDREN (default: 0)
There are two ways to let PHP handle multiple requests concurrently, Server Managed Mode and Self Managed Mode. In Server Managed Mode, LiteSpeed web server dynamically spawn/stop PHP processes, in this mode "Instances" should match "Max Connections" configuration for PHP external application. To start PHP in Self Managed Mode, "Instances" should be set to "1", while "LSAPI_CHILDREN" environment variable should be set to match the value of "Max Connections" and >1. Web Server will start one PHP process, this process will start/stop children PHP processes dynamically based on on demand. If "LSAPI_CHILDREN" <=1, PHP will be started in server managed mode.
Self Managed Mode is preferred because all PHP processes can share one shared memory block for the opcode cache.
Usually, there is no need to set value of LSAPI_CHILDREN over 100 in most server environment.
- LSAPI_AVOID_FORK (default: 0)
LSAPI_AVOID_FORK specifies the policy of the internal process manager in "Self Managed Mode". When set to 0, the internal process manager will stop and start children process on demand to save system resource. This is preferred in a shared hosting environment. When set to 1, the internal process manager will try to avoid freqently stopping and starting children process. This might be preferred in a dedicate hosting environment.
- LSAPI_EXTRA_CHILDREN (default: 1/3 of LSAPI_CHILDREN or 0)
LSAPI_EXTRA_CHILDREN controls the maximum number of extra children processes can be started when some or all existing children processes are in malfunctioning state. Total number of children processes will be reduced to LSAPI_CHILDREN level as soon as service is back to normal. When LSAPI_AVOID_FORK is set to 0, the default value is 1/3 of LSAPI_CHIDLREN, When LSAPI_AVOID_FORK is set to 1, the default value is 0.
- LSAPI_MAX_REQS or PHP_LSAPI_MAX_REQUESTS (default value: 10000)
This controls how many requests each child process will handle before it exits automatically. Several PHP functions have been identified having memory leaks. This parameter can help reducing memory usage of leaky PHP functions.
- LSAPI_MAX_IDLE (default value: 300 seconds)
In Self Managed Mode, LSAPI_MAX_IDLE controls how long a idle child process will wait for a new request before it exits. This option help releasing system resources taken by idle processes.
- LSAPI_MAX_IDLE_CHILDREN (default value: 1/3 of LSAPI_CHILDREN or LSAPI_CHILDREN)
In Self Managed Mode, LSAI_MAX_IDLE_CHILDREN controls how many idle children processes are allowed. Excessive idle children processes will be killed by the parent process immediately. When LSAPI_AVOID_FORK is set to 0, the default value is 1/3 of LSAPI_CHIDLREN, When LSAPI_AVOID_FORK is set to 1, the default value is LSAPI_CHILDREN.
- LSAPI_MAX_PROCESS_TIME (default value: 300 seconds)
In Self Managed Mode, LSAPI_MAX_PROCESS_TIME controls the maximum processing time allowed when processing a request. If a child process can not finish processing of a request in the given time period, it will be killed by the parent process. This option can help getting rid of dead or runaway child process.
- LSAPI_PGRP_MAX_IDLE (default value: FOREVER )
In Self Managed Mode, LSAPI_PGRP_MAX_IDLE controls how long the parent process will wait before exiting when there is no child process. This option help releasing system resources taken by an idle parent process.
By default a LSAPI application check the existence of its parent process and exits automatically if the parent process died. This is to reduce orphan process when web server is restarted. However, it is desireable to disable this feature, such as when a LSAPI process was started manually from command line. LSAPI_PPID_NO_CHECK should be set when you want to disable the checking of existence of parent process. When PHP started by "-b" option, it is disabled automatically.
By default a LSAPI application will not leave a core dump file when crashed. If you want to have LSAPI PHP dump a core file, you should set this environment variable. If set, regardless the value has been set to, core files will be created under the directory that the PHP script in.
By default a LSAPI application will send back a notification packet whenever a request has been received. Since PHP LSAPI 5.0 release, it can be changed to only notify server for newly established connection by setting this option to '1'. It is recommended for LiteSpeed Web Server 4.1 release and later to gain more performance.
By setting it to a non-zero number, LiteSpeed web server will log requests into standard error log file if a request takes longer than the specified milliseconds. It can help identify the script that slows down your server.