Install Multiple PHP Version with Apache on Ubuntu 18.04 (Part -1)

Apache Installation

Install Apache web server from the official repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of Apache web server.

sudo apt update 
sudo apt install apache2 libapache2-mod-fastcgi 

Ubuntu 18.04 Users:
sudo apt install apache2 libapache2-mod-fcgid

PHP Installation

For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.

sudo apt install python-software-properties
sudo add-apt-repository ppa:ondrej/php

For this tutorial, we are using the PHP 5.6 and PHP 7.2 to configure with Apache web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system.

apt update
sudo apt install php5.6 php5.6-fpm
sudo apt install php7.2 php7.2-fpm

After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

sudo systemctl status php5.6-fpm
sudo systemctl status php7.2-fpm

Apache Configuration

Now enable few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with Apache server.

sudo a2enmod actions fastcgi alias proxy_fcgi

Ubuntu 18.04 Users:
sudo a2enmod actions fcgid alias proxy_fcgi

Get ready for the configuration of websites on your Apache server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

sudo mkdir /var/www/html/php56
sudo mkdir /var/www/html/php72

Now, create and index.php containing the phpinfo() function.

echo "<?php phpinfo(); ?>" > /var/www/html/php56/index.php
echo "<?php phpinfo(); ?>" > /var/www/html/php72/index.php

Let’s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under /etc/apache2/sites-available with the extension .conf. Create a file for the first virtual host and edit in your favorite text editor.

sudo vim /etc/apache2/sites-available/php56.example.com.conf

Add the following content. Make sure to use correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

sudo vim /etc/apache2/sites-available/php72.example.com.conf

Add the following content to file with proper ServerName and DocumentRoot.

You both of the websites are configured now. But they are still not active. Apache keeps active sites under /etc/apache2/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use below command to do the same.

sudo a2ensite php56.example.com
sudo a2ensite php72.example.com

After making all the changes restart Apache to reload new settings changes.

sudo systemctl restart apache2

Your setup has been completed now. Go to the next step to test your setup.

 

Use php version with htaccess method

You can also add the <FilesMatch> directive to your .htaccess file. Before you do, make sure that AllowOverride is enabled, otherwise Apache will ignore .htaccess.

Open the Apache config file.

sudo nano /etc/apache2/apache2.conf

Scroll down the the following section and make sure that  AllowOverride  is set to All.

 /etc/apache2/apache2.conf
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Save and exit (press CTRL + X, press Y and then press ENTER)

Restart Apache.

sudo systemctl restart apache2

Now you can add the  <FilesMatch> directive to .htaccess

PHP 5.6 Example

.htaccess
<FilesMatch \.php$>
    # Apache 2.4.10+ can proxy to unix socket
    SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
</FilesMatch>

PHP 7.2 Example

.htaccess
<FilesMatch \.php$>
    # Apache 2.4.10+ can proxy to unix socket
    SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
</FilesMatch>

 

Test Setup

Edit /etc/hosts file on your local system and make an entry like below. This will resolve temprory names to localhost IP address.

sudo vim /etc/hosts

Add following entry to end of file

127.0.0.1 php72.example.com
127.0.0.1 php56.example.com

Open a web browser and visit both of the sites. You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.

Multiple PHP Version with Apache 1

Multiple PHP Version with Apache 2

 

 

Ref: https://tecadmin.net/install-multiple-php-version-apache-ubuntu/

Leave a Reply

Your email address will not be published.