Setting Up Multiple PHP Versions
Here at LiteSpeed, we are in the midst of a major overhaul of our documentation. It’s a lot of hard work — like carving granite with tweezers — but we don’t want to make you wait till it’s finished to get some of the benefits. So, on the blog, we will be sharing tips about how you can make LSWS run better for you. Today’s post is an overview of setting up multiple PHP versions when you use a control panel (though it will work if you don’t use a control panel as well).
(This post is based on a newly refurbished wiki article on the same topic. The wiki article contains more details on all steps in the process.)
Perhaps you’re having trouble porting an application to the newest version of PHP. Or you run shared hosting and some of your users want to be on the cutting edge of everything, while others don’t ever want to be bothered with upgrading anything. LiteSpeed helps you keep them all happy, and has been doing so for a while now. It is only recently, with CloudLinux’s PHP Selector, that there’s finally a convenient way for Apache hosts to allow different users to run different versions of PHP. (And we fully support CloudLinux, in every sense of the word. Neat fact: PHP Selector now supports PHP 4.4. As hacker bait, of course.) LiteSpeed has been allowing its users multiple versions of PHP since 2010. And even if you do have CloudLinux, on LiteSpeed, you can let your users run multiple versions of PHP. (PHP Selector allows shared hosting users to select between the multiple versions of PHP that the admin sets up. LiteSpeed makes it possible for the users themselves to run multiple versions at the same time.)
So how do we set up multiple versions of PHP? Before we start, you need to have the different versions of PHP compiled for LiteSpeed in different locations. (This is covered in the wiki article.) After the versions of PHP are ready, there are two main steps:
- Set up the different PHP versions as separate external applications in the WebAdmin console.
- Set up script handlers to tell the server which scripts go to which external application (and thus which PHP version).
1. External Applications: We are going to be adding these different versions of PHP as LSAPI applications (because they run faster on LiteSpeed’s native PHP SAPI).
- In the WebAdmin console, under the External App tab, click “Add”.
- Choose “LSAPI App” as the external application type.
- Fill in the settings for this PHP version. The most important points are that the “Address” must be unique and the “Command” path must match the location of that version of LSPHP.
Here’s one version of PHP (for PHP 5.3.24):
And another external application for another version:
We’re going to stop at two versions, but you could do this for as many versions as you like. (You could even set up applications for the same PHP build number with different extensions.)
2. Script Handlers: The external applications are up and ready to receive scripts, but the server has no mapping to tell it which scripts go to which external application. This mapping is done by setting up script handlers.
- In the WebAdmin console, under the Script Handler tab, click “Add”.
- Put in a script suffix (or two, or more), select “LiteSpeed API” as the type, and pick the external application that you want scripts with this suffix to be sent to.
Here’s a script handler for one of my PHP versions:
And for another:
Setting up script handlers tells the server what MIME types should go to which applications. The script handler creates a MIME type for the suffix entered and the server remembers that, when it sees that MIME type, it should send the associated script to a particular application. (This only works automatically for the first suffix entered. For two or more suffixes, you need to set up the MIME types manually. There is more on that in the wiki article.) If you (and your users) now alter your PHP file suffixes to match those listed in the script handlers, LSWS will know what to do with scripts written for different versions of PHP. (I, for instance, would change the suffixes on all files that I want to go to PHP 5.4.14 to .php5414. I probably don’t need to change the suffixes for files going to PHP 5.3.24 because I mapped the .php suffix to the lsphp5324 external application in my script handler. PHP 5.3.24 is now my default.)
3. But I don’t want to change my suffixes: There is another way to set up which files get sent to which applications (and thus which version of PHP). We said before that the server goes by MIME type when deciding which scripts to send where. This means that you don’t necessarily have to change the files’ suffixes, you just have to change their MIME types. Changing MIME types doesn’t sound much easier, but it is. It also means that you can stipulate that scripts in a certain directory go to a certain external application regardless of their suffix.
Controlling MIME types by location can be done at the virtual host level through the WebAdmin console (using contexts), but, if you’re using a control panel, virtual host-level settings in the WebAdmin console are not applied. This means that we have to do these configurations directly with directives in .htaccess files. There are a number of ways to do this using the AddHandler, AddType, and ForceType directives (and these are all addressed in the wiki article). For this overview, we’re only going to touch on ForceType.
ForceType (in a .htaccess file) forces all files in that directory (regardless of suffix) to be read as one MIME type. This means that we can simply put our scripts in different directories to have them processed by different applications. Now, continuing my example from above, all my .php scripts are already getting processed by the lsphp5324 external application, so I don’t have to touch them. But I can make a directory for scripts that I want to go to PHP 5.4.14 and use the following directive in a .htaccess file:
$ ForceType application/x-httpd-php5414
This will cause all files in this directory to be processed as the above MIME type and thus be sent to the lsphp5414 external application (and PHP 5.4.14).
Now, whenever we have a script for a certain application, we just have to make sure we put it in the right folder.
Notes:
- LiteSpeed PHP with suEXEC currently only accepts the following suffixes (and the corresponding MIME types): php, php4, php5, php52, php53, phtml, php54, fastphp.
Comments