Multiple PHP versions in the cPanel server


The PHP version on the servers will be the most tested out or proven one. But some clients may want a newer version and make use of the various upcoming features. Due to the various compatibility issues,  we can't perform a server wide PHP version change. In such cases, we need to install the newer version with the existing one and enable the client to switch to the newer one by changing the Handler.
Scenario
The client is hosted on a server with php 5.3.24 and he want to enable  5.4.14 for his domain.  There are  various blogs about running different major version of PHP on the server ie php 5 and php 4. But enabling different minor versions of same major version is a tricky one.
Recompile and install the default PHP (Optional)

If the existing installation is fine and has the necessary modules , then you can skip this step. Otherwise you need to recompile it using
/scripts/easyapache
Once it is done. You can proceed with the new PHP 5.4.14


Step 1:

Download the source file

mkdir /usr/local/src/php_source

cd /usr/local/src/php_source

Extract the  cource
tar -xvf php-5.4.14.tar.bz2

You can get the configuration options for the new install from the existing installation using the following command
 php -i |grep configure|sed 's/Configure Command =>  //g'|sed "s/'//g"

Once it is available modify it and make sure the installation path --prefix is specified and is compiled as cgi using the option --enable-cgi. In this case I am using the installation path as --prefix=/usr/local/php54
Make sure to remove the configuration parameter for apxs  ie   --with-apxs2=/usr/local/apache/bin/apxs
The final configuration option for new install will look like the following

 ./configure  --disable-fileinfo --disable-phar --enable-bcmath --enable-calendar --enable-libxml --enable-mbstring --enable-pdo=shared --enable-soap --enable-sockets --enable-wddx --enable-zend-multibyte --enable-zip --prefix=/usr/local/php54 --enable-cgi --with-bz2 --with-curl=/opt/curlssl/ --with-curlwrappers --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-libdir=lib64 --with-libexpat-dir=/usr --with-libxml-dir=/opt/xml2/ --with-mcrypt=/opt/libmcrypt/ --with-mm=/opt/mm/ --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-pdo-mysql=shared --with-pdo-sqlite=shared --with-pic --with-png-dir=/usr --with-pspell --with-tidy=/opt/tidy/ --with-xmlrpc --with-xpm-dir=/usr --with-xsl=/opt/xslt/ --with-zlib --with-zlib-dir=/usr
 Once the configuration is complete. Make the compilation using the following command
 make
Now proceed with the installation
make install

Once the installation is complete you will get an output like the following one
# make install
Installing shared extensions:     /usr/local/php54/lib/php/extensions/no-debug-non-zts-20100525/
Installing PHP CLI binary:        /usr/local/php54/bin/
Installing PHP CLI man page:      /usr/local/php54/php/man/man1/
Installing PHP CGI binary:        /usr/local/php54/bin/

Once the installation is completed, confirm the version by using the command

#  /usr/local/php54/bin/php -v
PHP 5.4.14 (cli) (built: May  3 2013 12:21:56)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Now we need to configure it for web server access

It is performed through specifying the new PHP handler for php file

 AddHandler specifically is meant to communicate with the server. It assigns or "maps" a handler to the file extension you specify, giving the server instructions on how to deal with it. For example, AddHandler could be used to activate a particular file type, which is why it must communicate with the server.
Eg:
AddHandler application/x-httpd-php54 .php
The AddHandler directive tells Apache that whenever it encounters a file with a .php extension, it should mark it as having a MIME type of application/x-httpd-php54.

AddType
AddType, on the other hand, is meant to communicate with the "client" or browser being using. The AddType directive tells the browser what type of data to expect and the browser follows suit. For example, if used incorrectly AddType can tell a browser that an image you would like displayed is text or html; this command would then force the browser to display it as such.The AddType directive allows you to define or override the media type for a particular file extension. It takes the following form: TheAddType directive should only affect the clients interpretation of what your sending it. A common one that is used is text/htmlwhich most clients will readily accept and interpret for what you want. Different browsers like I.E. Firefox and Chrome can sometimes interpret these headers differently.
Mime-types are used for several things in Apache.
  1. to tell the browser how to treat the file
  2. to tell apache which handler to use
With AddHandler you can bind a handler (like PHP) to a specific mime-type. So using a different mime-type for PHP could result in a different parser being used.
Apache again needs to be configured for the PHP MIME type.
AddType application/x-httpd-php53 .php53
AddHandler and AddType have a huge different meaning. Basically AddHandler is used if we want to handle files at server and AddType is used if we want to handle files at the browsers. Some file types like php, asp must handle at server and not at user browser, hence we will use AddHandler with them. That’s if you are developing a ruby on rail or PHP app then you should use AddHandler directive since they must get parsed at server only. While some file type like html, js etc must handled at user’s browser hence we will use AddType directive with them. When defining how a ".cgi" file is to be treated by the server, AddHandler would be your chosen directive. However, when defining how a particular piece of content is to be interpreted by the client, AddType would be more appropriate.

AddType defines how a client (example A web browser) should deal with the file (or data stream), while AddHandler defines how a server should parse a file.
The Actiondirective allows you to specify a CGI script to be executed when a certain media type is requested. It takes the following form:
you will need to tell the server to execute the PHP executable each time it encounters a PHP script. Add the following below any existing entries in the "Action" section.
Action content-type path
The content-type has to be a valid MIME type such as text/htmlimage/png, or application/pdf. The path refers to an existing CGI script, and must be relative to the directory specified by the DocumentRoot directive

Action application/x-httpd-php54 /cgi-sys/php54
The Action directive tells Apache that whenever it encounters a file of type application/x-httpd-php53 it should pass it to the script /cgi-sys/php54 and capture the output.

Assume that you are working on default cPanel server.

Open the file /usr/local/apache/conf/includes/pre_virtualhost_global.conf and add the following entries to enable
Add the following lines to the above file and save
Action application/x-httpd-php54 /cgi-sys/php54
 AddType application/x-httpd-php54 .php54
To make cPanel skip the .htacces scan and remove the mime types, do the  followin

/usr/local/cpanel/bin/apache_conf_distiller --update
touch /var/cpanel/easy_skip_update_php_mime_types 
vi /usr/local/cpanel/cgi-sys/php54
Add the following contents to the above file and save
#!/bin/sh
exec /usr/local/php54/bin/php-cgi
Setting Permissions
chown root:wheel /usr/local/cpanel/cgi-sys/php54
chmod +x /usr/local/cpanel/cgi-sys/php54
Soft-linking for command-line executions:
ln -s /usr/local/php54/bin/php-cgi /usr/local/bin/php54

Copying php.ini from build folder
mv /usr/local/php_source/php.ini-production /usr/local/php54/lib/php.ini
You need to restart httpd after the configuration 
/etc/init.d/httpd restart
Now everything is set. If you want to enable php5.3 for a particular account, create a .htaccess file as mentioned below.

nano -w /home/$username/public_html/.htaccess
AddHandler application/x-httpd-php54 .php

chown $username:$username /home/$username/public_html/.htaccess
chmod 644 /home/$username/public_html/.htaccess

4 comments:

Alexandru said...

Thank you! It helped a lot.

Anonymous said...

Hello,

Nice tutorial worked great for me, but I'm unable to make the PDO and PDO_MySQL and PDO_Mysqli extensions make working for both the PHP versions, could you please do let me know what could be an issue?

Anonymous said...

hey thanks for the tutorial but am getting an error like this....
[ext/fileinfo/libmagic/apprentice.lo] Error 1

any fix??

Anonymous said...

Ek dum Jhakaaas...:)