Dual PHP | Apache

ubuntuphp-logo-Converted-01-3mysqlapache

Prerequisits

Now that PHP 7 is getting more and more popular it’s the time to start the migration. But how are we going to maintain the old apps and develop the new ones?

What will you get out of this manual:

  • Dual install both PHP 5.6 and PHP 7
  • Apache 2.4 with FastCGI
  • Easy swap between virtual host from different PHP versions

PHP Sources installation

We will assume you have a clean Ubuntu 16.04 both desktop and server versions have been tested. To begin we are going to add the repository of packages of sir Ondřej Surý, a reference for PHP packages in Ubuntu.

Note: if you have troubles with adding repository try:

sudo apt-get install python-software-properties # this is just to enable the command add-apt-repository
sudo dpkg-reconfigure locales # this is to enable the UTF-8 lang that the repo needs

So following the instructions add the ondrej repo and update your sources with this commands:

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Now we are ready to install the Apache + Dual PHP + MySQL bundle.

sudo apt-get install php5.6 php5.6-fpm php7.0 php7.0-fpm apache2 mysql-server-5.7 libapache2-mod-fastcgi

Note: this is just a basic install, you can also install: redis, elastic, memcached, extensions like json, curl or soap, just add them if you need them like php5.6-soap or php7.0-soap

If all went well, you should have now the FastCGI Process Manager of PHP 5.6 and PHP 7.0 installed. So you can run:

sudo service php5.6-fpm status && sudo service php7.0-fpm status

Testing the Installation

Lets now generate a couple of PHP info so we can test our configurations:
so we are going to create in /var/www/html/ 2 folders with a index file that contains the phpinfo(); command.

sudo su #important to be root to be able to echo the content, otherwise do it manually with vim
mkdir /var/www/html/php5.6 && echo "<?php phpinfo();" >> /var/www/html/php5.6/index.php
mkdir /var/www/html/php7.0 && echo "<?php phpinfo();" >> /var/www/html/php7.0/index.php

Configuring Apache and FastCGI

We are going to configure the Fast CGI module in apache, for that we need to activate a couple of modules:

sudo a2enmod actions alias rewrite && sudo apache2ctl restart

Note: If you find a warning about serverName, just add “ServerName Localhost” to the apche conf file. In /etc/apache2/apache2.conf

To make PHP work with Fast CGI you will have to create a configuration file so we are going to do:

sudo vim /etc/apache2/conf-available/fastcgi.conf

then paste the text:

#PHP 5.6 configuration
AddHandler php5.fcgi .php 
Action php5.fcgi /php5.fcgi 
Alias /php5.fcgi /usr/lib/cgi-bin/php5.fcgi 
FastCgiExternalServer /usr/lib/cgi-bin/php5.fcgi -socket /run/php/php5.6-fpm.sock -pass-header Authorization -idle-timeout 3600

#PHP 7.0 configuration
AddHandler php7.fcgi .php
Action php7.fcgi /php7.fcgi
Alias /php7.fcgi /usr/lib/cgi-bin/php7.fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php7.fcgi -socket /run/php/php7.0-fpm.sock -pass-header Authorization -idle-timeout 3600

<Directory /usr/lib/cgi-bin>
        Require all granted
</Directory>

Once the script is created we proceed to activate it:

sudo a2enconf fastcgi

Now FAST CGI and PHP 5.6 and PHP 7 with FPM are all configured and working. So to test it, we are going to modify the default virtualhost of apache.

Configure Apache Virtualhost

sudo vim /etc/apache2/sites-available/000-default.conf

We are going to add this lines:

        <Directory /var/www/html/php5.6>
            <FilesMatch "\.php$">
                SetHandler php5.fcgi
            </FilesMatch>
        </Directory>
        <Directory /var/www/html/php7.0>
            <FilesMatch "\.php$">
                SetHandler php7.fcgi
            </FilesMatch>
        </Directory>

The resulting default file is like this:

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        <Directory /var/www/html/php5.6>
            <FilesMatch "\.php$">
                SetHandler php5.fcgi
            </FilesMatch>
        </Directory>
        <Directory /var/www/html/php7.0>
            <FilesMatch "\.php$">
                SetHandler php7.fcgi
            </FilesMatch>
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
        # error, crit, alert, emerg. 
        # It is also possible to configure the loglevel for particular 
        # modules, e.g. 
        #LogLevel info ssl:warn 
        
        ErrorLog ${APACHE_LOG_DIR}/error.log 
        CustomLog ${APACHE_LOG_DIR}/access.log combined 
        
        # For most configuration files from conf-available/, which are 
        # enabled or disabled at a global level, it is possible to 
        # include a line for only one particular virtual host. For example the 
        # following line enables the CGI configuration for this host only 
        # after it has been globally disabled with "a2disconf". 
        #Include conf-available/serve-cgi-bin.conf 
</VirtualHost>

What we are telling to Apache is to use different FPM depending on the directory. So basically after this, you can create as many virtual hosts as you want and specify the version of PHP you want to use.

Don’t forget to restart apache after editing the virtual host

sudo apache2ctl restart

Now the final test, go to your machine and open the sites:

PHP 5.6 Example

php5.6info

PHP 7.0 Example

php7.0.info

comments: romero.latorre@gmail.com
Note: If you want to add others versions of PHP just add another configuration to your fpm file, basically all the places you have added php 5.6 and php 7 you could add php 7.1 

To know the available versions check: https://launchpad.net/~ondrej/+archive/ubuntu/php