Configure by File¶
Besides configuring PHP by virtual host, OpenLiteSpeed also has ability to set up PHP by directory. This means that you can have different directories running different PHP versions to further customize your server environment.
In this guide, we will set up four directories, and make them each run a different PHP version.
As shown in the screenshot above, we have created the following directories for our example.com
domain: test
, test70
, test71
, and test72
. By default, they are all running the default version of PHP, which in this case is 7.3.7 We will make the last three listed directories run PHP 7.0, PHP 7.1, and PHP 7.2 respectively.
For each PHP version running in test70
, test71
, and test72
we will need to create an external app, script handler, and static context.
Set up External Apps¶
In your WebAdmin Console, navigate to Server Configuration > External App and create an external app for each of the three aforementioned PHP versions.
You can copy and paste from an existing external app, but remember to change the Name, Address, and Command to the corresponding PHP version. For PHP 7.0 we used the following values:
- Name:
lsphp70
- Address:
uds://tmp/lshttpd/lsphp70.sock
- Command:
/usr/local/lsws/lsphp70/bin/lsphp
When you are done, you should have three external apps named lsphp70
, lsphp71
, and lsphp72
.
Set up Script Handlers¶
Set up a script handler for each external app, setting the Suffixes to lsphp70
, lsphp71
, and lsphp72
, respectively. Set Handler Type to LiteSpeed SAPI
and select the appropriate Handler Name from the dropdown.
When you are done, you should have three script handlers named lsphp70
, lsphp71
, and lsphp72
.
Set up Static Contexts¶
Set up a static context for each of these directories.
Set URI to the directory name, and make sure there is a /
at both the beginning and the end of the URI. For example, /test70/
. This will force MIME type (via the setting below) to all files in the test70
directory.
If you would prefer to force MIME type selectively, you can use regular expressions. For example, to force MIME type to PHP files only, set URI to exp:^/test70/$|^/test70/.*\.php$
.
Location can be either an absolute path or a relative path with variables such as $VH_ROOT
or $DOC_ROOT
Accessible must be set to Yes
.
The Force MIME Type setting is what instructs LiteSpeed to run a different version of PHP, so it is very important. In this case, we used application/x-httpd-lsphp70
(the lsphp70
is the suffix we created in the script handler previously).
Repeat the same steps to create contexts for the test71
and test72
directories as well.
Verification¶
Now with everything configured, restart OpenLiteSpeed and visit these pages again to check the result:
You should see different PHP versions for each URL, as configured.
Alternate Method¶
Alternatively, you can also use a rewrite rule with the H
flag to set a different handler per directory or per individual file.
Examples
Force every file in the /test/
directory to use PHP 7.0.
RewriteRule ^test/.*$ - [H=application/x-httpd-lsphp70]
Force just the test.php
file to use PHP 7.0
RewriteRule test.php$ - [H=application/x-httpd-lsphp70]
To make this work, you only need to set up external apps and script handlers as instructed above. There is no need to set up static contexts.