One Server, Two PHP Versions:
How to upgrade your server's PHP for some but not all users,
and not conflict with cPanel, WHM, and EasyApache 3

William S. Statler, 25 Aug 2016

Introduction

This article is aimed at people who are running a dedicated web server, using cPanel/WHM and EasyApache 3. If you're leasing space on a shared server, this information probably won't be of much use to you (unless you can persuade the folks running your server to give it a try!).

If you only need to run PHP versions 5.4.x or later, and you can use EasyApache 4 and Apache 2.4.x, you probably don't need this article. EasyApache 4 will handle everything for you.

I didn't invent all this. There are many discussions on the Net about how to run multiple PHP versions on one server, but I bow down in abject gratitude to Stephen Simpson and his article How to Build Multiple Versions of PHP With cPanel, which is the only source I've found that explains how to do it in a way that is compatible with cPanel/WHM and EasyApache 3.

Why have two versions of PHP?

SCENARIO: You really need a newer version of PHP for compatibility with up-to-date software, but one website or account on your server is still running software from 2009, so you've been holding off.

SCENARIO: You're not quite sure what might break from a PHP upgrade, and you want to test everything carefully, one website or one piece of software at a time.

In this article, I'm going to assume that you are currently running an old version of PHP that is working correctly (other than being old), and that you want to upgrade the server's default PHP to a newer version but preserve the old version for one or more websites or accounts.

You can use a similar technique if your default PHP is a new version and you want to add an older version. See Stephen Simpson's article for more information.

What you will need

Fix any problems above before you continue, by running EasyApache 3 and making the necessary changes.

What you will end up with

You will have a separately-installed version of PHP that is identical to the antique version you're currently running. This version will be "frozen", in the sense that EasyApache 3 will never update it or make any configuration changes to it. You can use this version anywhere on your server simply by adding a line to the .htaccess file in the appropriate directory.

You will also have a new, up-to-date version of PHP installed as your server's default. This version will be used wherever it has not been overridden via .htaccess.

Warnings

Running old, unsupported versions of PHP (or other software) is a security risk. Please think of this as a temporary fix, to be used until you're able to complete all needed updates.

Make backups of your files and databases before you start changing things.

In the example below, I'll show an upgrade from PHP 5.4.45 to 5.6.25. Please make the obvious changes to match the PHP versions you're working with.

If anything breaks, it's not my fault. :-)

Step 1: Install a new copy of your old PHP

No, you can't just copy your existing version of PHP to a different directory. You'll have to rebuild it from source.

Put phpinfo() files in strategic locations

Make a file that looks like this:

<?php
phpinfo();
?>

Name it whatever you like (phpinfo.php or nobodywillguessthisfilename.php or whatever), and put copies in directories where you want to keep track of which PHP version is in use. One copy should be in a directory that will use the new version of PHP after you've updated (for example, /home/gooduser/public_html/phpinfo.php), and a copy should be in each directory that will be using the old PHP version (for example, /home/baduser/public_html/ancientsoftware/phpinfo.php).

Make sure to change the owner and group of each file to match the owner of the directory, e.g.:

chown gooduser:gooduser /home/gooduser/public_html/phpinfo.php

You can now access these files from a browser to get detailed reports on the version of PHP in use:

http://gooduserdomain.com/phpinfo.php

PHP Version 5.4.45


... ...
Build Date Jul 28 2016 23:50:03
Configure Command './configure' '--disable-fileinfo' '--disable-pdo' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-libxml' '--enable-sockets' '--prefix=/usr/local' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-curl=/opt/curlssl/' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libxml-dir=/opt/xml2/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-mysqli=/usr/bin/mysql_config' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pic' '--with-zlib' '--with-zlib-dir=/usr'
... ...
Configuration File (php.ini) Path /usr/local/lib
Loaded Configuration File /usr/local/lib/php.ini
Scan this dir for additional .ini files (none)
Additional .ini files parsed (none)
... ...

Here's the important stuff you'll need:

Download and extract PHP source code

Fetch the PHP version that matches the one you currently have installed on your server:

cd /usr/local/src/
wget -O php-5.4.45.tar.gz http://php.net/get/php-5.4.45.tar.gz/from/a/mirror

If you're looking for a really old version of PHP, you might need to go to the PHP Museum (for example, http://museum.php.net/php5/php-5.2.17.tar.gz). Also, some old versions (including 5.2.17) may need patching before they can be used. See below for notes on installing a patch.

Extract the source code from the archive:

tar -xvz --no-same-owner -f php-5.4.45.tar.gz

Configure and compile PHP

Go to your new source code directory:

cd /usr/local/src/php-5.4.45

From your browser, open one of your phpinfo() pages and copy that humongous Configure Command into a text editor:

'./configure' '--disable-fileinfo' '--disable-pdo' '--enable-bcmath' ...etc etc etc...

You'll need to make two changes to the Configure Command:

You can also extract the Configure Command from the terminal using php -i, but I don't recommend this, because it will give you what was used to configure the command-line (CLI) version of PHP. What you want is the Configure Command from the web (CGI) version, which may not be identical on your server.

Paste your edited Configure Command into your terminal and execute it:

./configure --disable-fileinfo ...etc etc etc... --prefix=/usr/local/php54 ...etc etc etc...

If you're compiling a very old PHP version that needs a patch, install it now. Example for PHP 5.2.17:
wget https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt
patch -p0 -b < txtbgxGXAvz4N.txt

Compile and install:

make
make install

Verify that you successfully installed the CGI version of PHP in the directory you configured earler with --prefix=:

ls -l /usr/local/php54/bin/php-cgi

If you see php-cgi listed, your installation was successful.

Create a php.ini file

Go to the /lib/ subdirectory of your new PHP installation:

cd /usr/local/php54/lib/

Check your phpinfo() page to find your default Loaded Configuration File, and make a copy of it:

cp /usr/local/lib/php.ini .

You'll need to make at least a few edits. Use grep to check for any lines that might refer to an incorrect directory:

grep "/usr/" php.ini

You may see something like this:

include_path = ".:/usr/lib/php:/usr/local/lib/php"
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20100525"
sendmail_path = "/usr/sbin/sendmail -t -i"
;sybase.interface_file = "/usr/sybase/interfaces"

The include_path is pointing at your old installation, and needs to be changed like so:

include_path = ".:/usr/local/php54/lib/php"

If you have an extension_dir line, check out what's in the directory. It's easiest to just copy the entire old /extensions/ directory into your new installation:

cp -r /usr/local/lib/php/extensions /usr/local/php54/lib/php

And then edit the extension_dir line in php.ini to point at the new copy:

extension_dir = "/usr/local/php54/lib/php/extensions/no-debug-non-zts-20100525"

Look through the entire php.ini file for any other issues or oddities that need fixing.

Step 2: Tell Apache about the PHP you just installed

Modify httpd.conf by using Include files

You can't just edit httpd.conf, because EasyApache 3 will destroy your changes. Instead, we'll make a separate config file for your newly-installed PHP, and use Include to make sure Apache reads it.

Create a config file (/usr/local/apache/conf/php54.conf), and put the following lines in it:

# Configuration for PHP 5.4.45
AddType application/x-httpd-php54 .php54 .php
<Directory />
   suPHP_AddHandler application/x-httpd-php54
</Directory>

Next, edit /usr/local/apache/conf/includes/pre_main_global.conf, and append a line to Include the config file you just created:

Include /usr/local/apache/conf/php54.conf

(The pre_main_global.conf file is automatically read when httpd.conf is read, so your new config will be included.)

Edit suphp.conf

Edit the file /opt/suphp/etc/suphp.conf. Look for the section labeled [handlers], and append these lines:

;Handler for separately-installed PHP 5.4.45
application/x-httpd-php54="php:/usr/local/php54/bin/php-cgi"

Restart Apache httpd

Restarting Apache should have no immediate effect, other than to enable the changes you've made. Your newly-installed PHP won't be used anywhere until you edit the appropriate .htaccess files. So go ahead and:

service httpd restart
   OR
apachectl restart
   OR
/etc/init.d/httpd restart

Edit .htaccess files

Adding this to an .htaccess file will cause Apache to use your newly-installed PHP in that directory and all subdirectories:

# Run under PHP 5.4.45, not under server's default PHP version
AddType application/x-httpd-php54 php

Which .htaccess files should you edit? If you want, you add these lines to .htaccess in every /public_html/ directory on your server, to protect them from the PHP upgrade you're going to install with EasyApache 3. Then, after the upgrade, you can remove them one at a time, and verify that the affected website is working under the new PHP version.

More likely, you already know which directory you need to protect. It'll be the /public_html/ of that guy who's still using the vintage-2009 software, or maybe one of his subdirectories.

Edit your .htaccess files as described, and then go to your browser and check the phpinfo() pages that you installed in those directories. You should see an unchanged PHP version, but a new Build Date and a different Loaded Configuration File. Verify that the website is still working under this freshly-installed PHP.

Step 3: Update the server's default PHP version with EasyApache 3

I'm only going to give a brief outline of this step. If you need help running EasyApache 3, please check the online docs.

This would be a really good time to make another backup of your files and (especially) your databases.

Run EasyApache 3

First, update cPanel/WHM to the latest version, if needed.

You can find EasyApache 3 in the Software section of WHM. I recommend that you download a copy of your **DEFAULT** profile, just in case you need to restore the previous configuration. Then click the Customize Profile gear icon for **DEFAULT** (or for any other saved profile that you'd like to start with). Make any changes you need.

When you get to the PHP section, select the newest and shiniest PHP version that you'd like installed as your server's default. (As of right now, Aug 2016, that probably means the latest 5.6.x version. PHP 7 is available, but it's still rather new, and it might be incompatible with some of your software. But try it if you're feeling brave!)

When you're happy with everything, save the profile and click Build.

After the build completes, you'll see the Configure PHP and suEXEC popup window. Unless you know there are changes you need to make, do nothing and just close the popup.

Check your results

From your browser, open a couple of your phpinfo() pages. Pick one that's in a directory running the server's default PHP, and another that's in a directory you protected from the upgrade using an .htaccess file. Verify that both are showing the expected PHP version, Build Date, and Loaded Configuration File.

Check all the websites running on your server, and make sure they're working.

All done! Congratulations!

Help!!!

If you find anything confusing (or wrong!) in the above article, you can e-mail me at billstatler at bentonrea dot com.

Also: Yes, I do this kind of work for money. Not a whole lot of money, either. Check out my Website Maintenance Services at statler.ws/services/.

©2016 William S. Statler. Licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.
All other rights reserved.

Return to my Home Page