Install and Configure PHP¶
This guide will show you how to get started with LSPHP (PHP with LiteSpeed API) on your linux machine.
We will cover:
- Installing PHP
- Configuring LSPHP
Installation¶
Install from LiteSpeed Repository¶
The easiest way to get up and running with PHP is to use the LiteSpeed Repository. The LiteSpeed Repository comes with prebuilt PHP packages with LiteSpeed support built in.
Here is a list of some of the features you get when you use our LiteSpeed Repository, instead of compiling PHP manually:
- PHP versions 5.3*, 5.4*, 5.5*, 5.6*, 7.0*, 7.1*, 7.2*, 7.3*, 7.4*, 8.0*, 8.1, 8.2 and 8.3.
- CentOS* 7 (EOL June 30, 2024), 8, 9
- Debian 10 (EOL June 30, 2024), 11, 12
- Ubuntu 20, 22, 24
- The most up-to-date versions of LSAPI. (You don't have to wait for a new PHP version to be released.)
- Easy installation of multiple versions of PHP (by default installed to different locations).
- All frequently used PHP packages.
- Multiple possible MySQL support packages (via native driver or client library).
- Multiple opcode caching options: APCu, Xcache, Zend Opcache.
Footnote
* These PHP versions are dependent on the OS. Newer OS's might not support the older end-of-life (EOL) versions.
Prerequisites¶
Before you can install LSPHP there are a couple of prerequisite packages and repositories that need to be installed via the operating system's package manager. This can be accomplished by running the following command in a terminal:
$ sudo wget -O - https://repo.litespeed.sh | sudo bash
Packages¶
It is not a requirement to search through the LSPHP packages and extensions we offer before installing, but it does come in handy to know what you can install and then have the option to install everything at the same time. To get a list of all the LSPHP packages and extensions we offer you can run the following:
$ sudo apt-cache search lsphp
$ sudo dnf search lsphp
$ sudo dnf search lsphp
$ sudo yum search lsphp
Install¶
Now that you have installed the prerequisites you can go ahead with the actual installation process.
If you searched our repository then you can use the names of the packages and extensions you found to install what you need.
For example, to install LSPHP82 and LSPHP82-mysql via the command line you would run the following:
$ sudo apt-get install lsphp82 lsphp82-common lsphp82-mysql
$ sudo dnf install lsphp82 lsphp82-common lsphp82-mysqlnd
$ sudo dnf install lsphp82 lsphp82-common lsphp82-mysqlnd
$ sudo dnf install lsphp82 lsphp82-common lsphp82-mysqlnd
This will install lsphp82
and lsphp82-mysql
into the following location: /usr/local/lsws/lsphp82/bin/lsphp
.
Once all of the packages and extensions you need have been installed, you can continue to Configuration.
Install from Source¶
Here we will take a look at how to compile PHP + LSAPI from source. This can be useful for custom setups and for platforms where our LiteSpeed Repo is not available.
Prerequisites¶
PHP has many dependencies that are needed when compiling and these often change depending on what extensions and functions you compile PHP with. So before you can compile you will need to install some default packages that are needed for a reasonably feature rich PHP.
From the command line, run the following:
$ sudo apt-get build-essential pkg-config openssl libssl-dev openssl-blacklist openssl-blacklist-extra bison autoconf automake libtool re2c flex libxml2-dev libssl-dev libbz2-dev libcurl4-openssl-dev libdb5.1-dev libjpeg-dev libpng12-dev libXpm-dev libfreetype6-dev libt1-dev libgmp3-dev libc-client2007e-dev libldap2-dev libmcrypt-dev libmhash-dev freetds-dev zlib1g-dev libmysqlclient-dev libncurses5-dev libpcre3-dev unixODBC-dev libsqlite0-dev libaspell-dev libreadline6-dev librecode-dev libsnmp-dev libtidy-dev libxslt-dev libt1-dev
$ sudo dnf install patch gcc glibc libstdc++ binutils libtool autoconf make bison pam-devel libcap-devel openssl-devel tcp_wrappers-devel bzip2-devel curl-devel db4-devel gmp-devel httpd-devel libstdc++-devel sqlite-devel sqlite2-devel liyuybedit-devel pcre-devel libtool gcc-c++ libtool-ltdl-devel libevent-devel libc-client-devel cyrus-sasl-devel openldap-devel mysql-devel postgresql-devel unixODBC-devel libxml2-devel net-snmp-devel libxslt-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel libXpm-devel t1lib-devel libmcrypt-devel libtidy-devel freetds-devel aspell-devel recode-devel enchant-devel firebird-devel gdbm-devel tokyocabinet-devel
$ sudo dnf install patch gcc glibc libstdc++ binutils libtool autoconf make bison pam-devel libcap-devel openssl-devel tcp_wrappers-devel bzip2-devel curl-devel db4-devel gmp-devel httpd-devel libstdc++-devel sqlite-devel sqlite2-devel liyuybedit-devel pcre-devel libtool gcc-c++ libtool-ltdl-devel libevent-devel libc-client-devel cyrus-sasl-devel openldap-devel mysql-devel postgresql-devel unixODBC-devel libxml2-devel net-snmp-devel libxslt-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel libXpm-devel t1lib-devel libmcrypt-devel libtidy-devel freetds-devel aspell-devel recode-devel enchant-devel firebird-devel gdbm-devel tokyocabinet-devel
$ sudo yum install patch gcc glibc libstdc++ binutils libtool autoconf make bison pam-devel libcap-devel openssl-devel tcp_wrappers-devel bzip2-devel curl-devel db4-devel gmp-devel httpd-devel libstdc++-devel sqlite-devel sqlite2-devel liyuybedit-devel pcre-devel libtool gcc-c++ libtool-ltdl-devel libevent-devel libc-client-devel cyrus-sasl-devel openldap-devel mysql-devel postgresql-devel unixODBC-devel libxml2-devel net-snmp-devel libxslt-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel libXpm-devel t1lib-devel libmcrypt-devel libtidy-devel freetds-devel aspell-devel recode-devel enchant-devel firebird-devel gdbm-devel tokyocabinet-devel
This will install the basic dependencies for the PHP that you will compile in the next section.
Compile¶
Now that the dependencies have been installed you can work on compiling PHP.
Info
You can change 8.2.17
in the following commands with the version of PHP you wish to compile.
First, you will need to download and extract PHP. This can be done by running the following in a command line:
$ sudo cd /tmp
$ sudo wget https://www.php.net/distributions/php-8.2.17.tar.gz
$ sudo tar -xf php-8.2.17.tar.gz
$ sudo cd php-8.2.17
Now that PHP has been download and extracted, in the source code folder run ./configure
with --with-litespeed
and other configuration options. For example, you can run the following:
$ sudo ./configure '--prefix=/usr/local/lsws/lsphp82' '--with-libdir=lib64' '--with-zlib' '--with-gd' '--enable-shmop' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--with-curl' '--with-openssl' '--with-gettext' '--with-mcrypt' '--enable-mbstring=all' '--enable-mbregex' '--with-png-dir=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--enable-ftp' '--with-imap=/usr' '--with-imap-ssl' '--with-mysql=/usr' '--with-mysqli=/usr/bin/mysql_config' '--enable-pcntl' '--with-freetype-dir=/usr' '--with-pdo-mysql=/usr' '--with-litespeed'
Danger
It is absolutely required to run ./configure
with --with-litespeed
if you want to build PHP with LSAPI. The other options are not required but we do recommend setting a --prefix
so PHP does not install to the default location and break any system PHPs you may already have installed. This --prefix
can be changed to whatever you want the path to be.
Now that PHP is configured, you can compile it. This is accomplished by running these commands in a terminal:
$ sudo make
$ sudo make install
Info
During the make
process it is possible to run into dependency issues depending on the configuration options you have set. We recommend searching Stack Overflow to install these missing dependencies as this is a very common and operating system specific issue that others have probably already solved.
Once make install
is finished then LSPHP is all set to go. You can continue to Configuration.
Configuration¶
This section will show you how to manually set up the PHP section at the server level so that it can work with your system's binaries to serve PHP files.
GUI¶
Server Level¶
To set up the PHP section via our WebAdmin Console GUI, navigate to External Application > Add, and choose LSAPI
Type. Set the following example values, and press Save:
- Name:
lsphp
- Address:
uds://tmp/lshttpd/lsphp.sock
- Max Connections:
10
- Environment:
PHP_LSAPI_CHILDREN=10 LSAPI_AVOID_FORK=200M
- Initial Request Timeout (secs):
60
- Retry Timeout (secs):
0
- Command:
lsphp82/bin/lsphp
- Instances:
1
Navigate to Server Configuration > Add Script Handler. Set the following example values, and press Save:
- Suffixes:
php
- Handler Type:
LiteSpeed SAPI
- Handler Name [Server Level]:
lsphp
Restart the web server and PHP.
That is it! PHP is now fully set up, and pages ending in the suffixes you added to the PHP Handler will use the PHP Handlers > Command to process the page.
Notes
- The most important setting here is the Command setting. It must be set to the location of the PHP build you wish to use. (All copies of PHP are automatically copied to
$SERVER_HOME/fcgi-bin/
if created using OpenLiteSpeed’s PHP Build utility. - Max Connections and PHP_LSAPI_CHILDREN values need to match. In this example both are set to
10
.
Virtual Host Level¶
If you want to use different settings for any of your virtual hosts, you can set up the same external applications and script handlers at the virtual host level, which will override any server-level script handler settings.
Navigate to Server Configuration > Virtual Hosts > Example2 > Script Handlers > Add, and update the new script handler with appropriate settings, using the following example as a guide: - Suffixes: php
- Handler Type: LiteSpeed SAPI
- Handler Name: lsphp82
Command Line¶
It is possible to configure PHP manually via a terminal. Open /usr/local/lsws/conf/httpd_config.conf
, like so:
$ sudo vi /usr/local/lsws/conf/httpd_config.conf
Add the following extprocessor
block to the file:
extprocessor lsphp {
type lsapi
address uds://tmp/lshttpd/lsphp.sock
maxConns 10
env PHP_LSAPI_CHILDREN=10
env LSAPI_AVOID_FORK=200M
initTimeout 60
retryTimeout 0
persistConn 1
respBuffer 0
autoStart 2
path lsphp82/bin/lsphp
backlog 100
instances 1
priority 0
memSoftLimit 0
memHardLimit 0
procSoftLimit 0
procHardLimit 0
}
Add the following scripthandler
block to the file:
scripthandler {
add lsapi:lsphp php
}
Save the file. The changes won't take effect until you restart OpenLiteSpeed. To do so, run the following commands:
$ sudo touch /usr/local/lsws/admin/tmp/.lsphp_restart.txt
$ sudo systemctl restart lsws
More PHP Settings¶
You can further configure PHP using the following settings.
PHP Handlers¶
Title | Value | Description |
---|---|---|
Suffixes | php | Extra file "suffixes" to be handled by this handler. |
Handler Type | LiteSpeed LSAPI | Specifies the type of external application that processes these script files. |
Handler Name | LiteSpeed lsphp | Specifies the name of the external application that processes the script files when the handler type is FastCGI, Web Server, LSAPI, Load Balancer, or Servlet Engine. |
PHP External App Defaults¶
Title | Value | Description |
---|---|---|
Name | lsphp | A unique name for this external application. You will refer to it by this name when you use it in other parts of the configuration. |
Address | uds://tmp/lshttpd/lsphp.sock | A unique socket address used by the external application. |
Max Connections | 10 | Specifies the maximum number of concurrent connections that can be established between the server and an external application. |
Environment | PHP_LSAPI_CHILDREN=10 LSAPI_AVOID_FORK=200M | Specifies extra environment variables for the external application. |
Initial Request Timeout | 60 | Specifies the maximum time in seconds the server will wait for the external application to respond to the first request over a new established connection. |
Retry Timeout | Specifies the period of time that the server waits before retrying an external application that had a prior communication problem. | |
Connection Keepalive Timeout | Specifies the maximum time in seconds to keep an idle persistent connection open. | |
Response Buffering | No | Specifies whether to buffer responses received from external applications. |
Start By Server | Yes (Through CGI Daemon) | Specifies whether you want the web server to start the application automatically.. |
Command | /usr/local/lsws/lsphp82/bin/lsphp | Specifies the full command line including parameters to execute the external application. |
Back Log | 100 | Specifies the backlog of the listening socket. Required if Start By Server is enabled. |
Instances | 1 | Specifies the maximum instances of the external application the server will create. |
Run On Startup | Yes (Detached mode) | Specifies whether to start the external application at server start up. |
Max Idle Time | -1 | Specifies the maximum idle time before an external application is stopped by the server, freeing idle resources. |
Priority | Specifies priority of the external application process. | |
Memory Soft Limit | Specifies the memory consumption limit in bytes for an external application process or an external application started by the server. | |
Memory Hard Limit | Much The same as Memory Soft Limit (bytes), except the soft limit can be raised up to the hard limit from within a user process. | |
Process Soft Limit | Limits The total number of processes that can be created on behalf of a user. | |
Process Hard Limit | Much The same as Process Soft Limit, except the soft limit can be raised up to the hard limit from within a user process. |