Category Archives: Adventures

Multiple PHP versions in the same server

php1I have a dedicated server that runs PHP 5.2, for compatibility with some legacy apps. This version is too old to run, so I was looking for a way to have multiple versions of PHP in the same server – and select what I need per website.

Cpanel/whm has very nice tools to auto-install the version you want, but allows only for one version. However, in many hosting companies, clients have the option to choose PHP version with simple .htaccess commands. How this happens?

After doing some research, I found that you can run as many versions as you want if you run them through suPHP, CGI or FastCGI.  My server runs php in suPHP mode, so the rest is not hard.

The main idea is that every PHP version is installed on it’s own directory, and then for each cPanel account we define what version we want it to run – or if we don’t it runs the default cPanel version.

So, in my case, I had PHP 5.2 installed as default. I wanted to install 5.5.3 to run as well. I logged in through ssh as root and did:

# cd /usr/src
# wget http://gr2.php.net/distributions/php-5.5.3.tar.gz
# tar xfz php-5.5.3.tar.gz
# cd php-5.5.3

In other words, I downloaded the release I wanted in /usr/src directory and uncompressed. All files passed to /usr/src/php-5.5.3

Next step:

# ./configure \
 --enable-bcmath \
 --enable-calendar \
 --enable-exif \
 --enable-ftp \
 --enable-gd-native-ttf \
 --enable-libxml \
 --enable-mbstring \
 --enable-pdo=shared \
 --enable-soap \
 --enable-sockets \
 --prefix=/opt/php55 \
 --with-bz2 \
 --with-config-file-path=/opt/php55/lib \
 --with-config-file-scan-dir=/opt/php55/lib/php.ini.d \
 --with-curl=/opt/curlssl/ \
 --with-freetype-dir=/usr \
 --with-gd \
 --with-gettext \
 --with-imap=/opt/php_with_imap_client/ \
 --with-imap-ssl=/usr \
 --with-jpeg-dir=/usr \
 --with-kerberos \
 --with-libxml-dir=/opt/xml2 \
 --with-libxml-dir=/opt/xml2/ \
 --with-mcrypt=/opt/libmcrypt/ \
 --with-mysql=/usr/ --with-libdir=lib64 \
 --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-pdo-mysql=shared \
 --with-pdo-sqlite=shared \
 --with-png-dir=/usr \
 --with-pspell \
 --with-xpm-dir=/usr \
 --with-zlib \
 --with-zlib-dir=/usr

That sets the configuration for php setup. If you follow this step, you should customise it to your needs. One of the options is that php should be installed in  /opt/php55.

When that finishes, assuming that everything is OK, we run the make command:

# make

and then

# make install

And PHP 5.5 is installed in /opt/php55.

Now we have to set our options in php.ini. A quick configuration:

# cp php.ini-production /opt/php55/lib/php.ini

It coppied the default settings. We should do some changes here and there:

# vi /opt/php55/lib/php.ini

For the next step, we have to add php5.5 handelr to suPHP and to apache.

# vi /opt/suphp/etc/suphp.conf

Before the end there will be something like:

application/x-httpd-php5="php:/usr/bin/php"

We add after that:

application/x-httpd-php55="php:/opt/php55/bin/php-cgi"

We save the file and go to change apache configuration as well

# vi /etc/httpd/conf/php.conf

Again, somewhere before the endo of file, there should be something like: suPHP_AddHandler application/x-httpd-php5.  After that, we add:

suPHP_AddHandler application/x-httpd-php55

We restart the apache service (in centos the command should be  # service httpd restart).

Now, to any website we want to use the newer version of php, we edit .htaccess and add this line on top of the file:

AddHandler application/x-httpd-php55 .php

and our website runs on PHP 5.5.

The next step is to change the default version of php to be 5.5 instead of 5.2, but that’s enough information for today.