Configure PHP by Directory or File

How Can We Help?
< Back
You are here:

Besides configuring PHP by virtual host, OLS 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 above, we have created the following directories: test, test70, test71, and test72. By default, they are all running PHP 7.3. We will make the last three listed directories run PHP 7.0, PHP 7.1, and PHP 7.2 respectively.

Set up External Apps

Create external apps for the three aforementioned PHP versions.

You can copy/paste from an exisiting external app, but remember to change the Name, Address, and Command to the corresponding PHP version.

When you are done, you should have external app list like this:

Set up Script Handlers

Set up a script handler for each external app, like so:

When you are finished, it should look like this:

Set up Static Contexts

Set up a static context for each of these directories.

For example, the first directory is test70so set URI to /test70/ (Do not forget the / at the beginning and the end.)

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, and most importantly, you need to update Force MIME Type in order to run a different version of PHP. In this case, use application/x-httpd-lsphp70, the lsphp70 is the suffix you created in the script handler previously.

Repeat the same steps to create contexts for test71 and test72 as well.


Applying Force MIME Type Selectively

The above context will apply Force MIME Type to all the files under the $DOC_ROOT/test70/ Location, e.g. /test70/example.png.

If you want to apply selectively, you can use a regular expression. For example, to apply Force MIME Type to PHP files only, set URI to exp:^/test70/$|^/test70/.*\.php$.




Now with everything configured, restart OpenLiteSpeed and visit these pages again to check the result:

As you can see, you now have a single vhost running four different PHP versions on four different directories.

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.

In this case, steps 1 and 2 would be the same as above, and there would be no need to set up a context.

For example:

RewriteRule ^test/.*$ - [H=application/x-httpd-lsphp70]

Above rule will force /test/ to run on PHP 7.0.

Another simple example:

RewriteRule test.php$ - [H=application/x-httpd-lsphp70]

This rule will force /test.php to run on PHP 7.0