How to use two PHP versions under APACHE on ubuntu

by Dani Arlandis — on  ,  , 

Sometimes we need to fix the PHP version in old projects that are no longer going to scale anymore. In the following scenario we have PHP installed by packages and so that we do not update the version and that it causes irreversible errors in our old project we will install another version of PHP downloading the code and compiling it ourselves.

These are the steps to procedure:

First we obtain the source code from a mirror using wget.

wget -O php-5.6.30.tar.gz

Then unzip with gzip and tar

gzip -d php-5.6.30.tar.gz
tar -xvf php-5.6.30.tar

Move to de folder.

cd php-5.6.30

Then make a dir for configure the new version.

sudo mkdir /usr/local/php563

And now we can configure and make install the source code, in these step when we need configure the libraries that we need in the new project.

sudo ./configure '--with-libdir=lib64' 'nsta--cache-file=../config.cache' '--prefix=/usr/local/php563' '--with-config-file-path=/usr/local/php563/etc/' '--disable-debug' '--enable-pdo' '--with-pdo-pgsql' '--with-pear=/usr/local/php563/pear'
sudo make && sudo make install

We establish the php.ini and configure the errors level for wanted enviroment.

sudo cp php.ini-production /usr/local/php563/etc/php.ini
sudo sed 's#display_errors = On#display_errors = Off#g' -i /usr/local/php563/etc/php.ini

Finally we configure httpd.conf from /etc/apache2/ specyfying the proyect route that it's going to use the new installed PHP version

 <Directory /usr/local/php563/bin>
   Require all granted

 ScriptAlias /php563 /usr/local/php563/bin
 <Directory /var/www/htdocs/> 
   Action php563 /php563/php-cgi
   AddHandler php563 .php
   <FilesMatch "\.php$">
     SetHandler php563

Note: In same cases are needed turn on Actions:

 sudo a2enmod actions

That's all, we have two PHP versions running at time in differents proyects.