Run multiple PHP version on the same server using php-fpm and xdebug on Ubuntu 16.04

After trying to debug a legacy app on my computer it was necessary to run an apache vhost using php 5.6, but my apache was running on php 7.1.

After some Google research this solution finally worked for me:

Install php 5.6 and 7.1 as fpm

  1. Install php 5.6 and php7.1 fpm including dev-tools
sudo su
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install libapache2-mod-fastcgi php5.6-fpm php5.6 php5.6-dev php5.6-mcrypt php5.6-mbstring php5.6-mysql php5.6-zip php5.6-gd php5.6-xml php7.1-fpm libapache2-mod-fastcgi php7.1-fpm php7.1 php7.1-dev php7.1-mbstring php7.1-mysql php7.1-zip php7.1-gd php7.1-xml php7.1-curl php7.1-intl php7.1-json php7.1-mcrypt

Enable all necessary Apache modules:

a2enmod actions 
a2enmod fastcgi

Configure Apache default vhost

Add handlers for both version to your default Apache vhost

<IfModule mod_fastcgi.c>
AddHandler php56-fcgi-www .php
Action php56-fcgi-www /php56-fcgi-www
Alias /php56-fcgi-www /usr/lib/cgi-bin/php56-fcgi-www
FastCgiExternalServer /usr/lib/cgi-bin/php56-fcgi-www -socket /run/php/php5.6-fpm.sock -pass-header Authorization
<Directory "/usr/lib/cgi-bin">
Require all granted
</Directory>
</IfModule>
<IfModule mod_fastcgi.c>
AddHandler php71-fcgi-www .php
Action php71-fcgi-www /php71-fcgi-www
Alias /php71-fcgi-www /usr/lib/cgi-bin/php71-fcgi-www
FastCgiExternalServer /usr/lib/cgi-bin/php71-fcgi-www -socket /run/php/php7.1-fpm.sock -idle-timeout 1800 -pass-header Authorization
<Directory "/usr/lib/cgi-bin">
Require all granted
</Directory>
</IfModule>
<IfModule mod_fastcgi.c>
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler php71-fcgi-www
</FilesMatch>
</IfModule>

I have one vhost for each project so I added this part to my projects vhost:

<VirtualHost *:80>
...
<IfModule mod_fastcgi.c>
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
SetHandler php56-fcgi-www
</FilesMatch>
</IfModule>
...
</VirtualHost>

Restart your apache:

sudo service apache2 restart

That’s it.

php 5.6 for just one vhost

Install xdebug for php 5.6

The xdebug part was a little bit tricky as we need to compile and build xdebug with a custom phpize version/php-config.

Different phpize and php-config versions on my local machine

Basically you can follow the instrcutions of the official Xdebug page: https://xdebug.org/wizard.php 
BUT: when it comes to ./configure, you need to set the a different php-config.

  1. Download xdebug-2.5.2.tgz
  2. Unpack the downloaded file with tar -xvzf xdebug-2.5.2.tgz
  3. Run: cd xdebug-2.5.2
  4. Run: /usr/bin/phpize5.6.
  5. Run: ./configure --with-php-config=/usr/bin/php-config5.6
  6. Run: make
  7. Run: cp modules/xdebug.so /usr/lib/php/20131226
  8. Edit /etc/php/5.6/fpm/php.ini and add the line
    zend_extension = /usr/lib/php/20131226/xdebug.so
  9. Restart the webserver sudo server php5.6-fpm restart

If you have any questions, just let me know…