• Home
  • /
  • Apache
  • /
  • Different PHP Versions on the same Server
Different PHP Versions on the same Server

Different PHP Versions on the same Server

Last week I received an E-mail from a web hosting provider I have been using for almost 5 years that it is time to renew the contract and pay the 121$ bill once again. 5 years ago I didn’t have much experience with private servers and I had no other choice than using a shared web hosting provider. Now that I gathered some experience in VPS hosting I thought it was time to migrate all my data/databases to a VPS for two reasons:

  1. I would have much more control over everything happening on my server.
  2. Digitalocean being 5$ a month for the minimal droplet using a VPS server would be much cheaper. You can use my referral link to sign up for Digitalocean and receive a free balance of 10$. You will have 2 month of free hosting to learn before implementing your projects. Here is the referral link: https://m.do.co/c/805fa62c168a

At first glance, migrating everything from one server to another is as easy as copying the whole file structure and importing all databases. That’s actually only the physical part of it. Beside the physical task of copying all data, there is often some minor to large software differentiations between the old and new server too, that need to be coped with. In my case the old server was set up with PHP version 5.6 and the new one with PHP 7.1. Since I had written the website in PHP 5.6, I faced some problems running it on PHP 7.1. Obviously it would have been much safer if I had change the code to support PHP 7.1 but since that website isn’t actually used that much, I didn’t want to invest anymore time in it.

This pushed me to have two versions of PHP installed on my Apache run Ubuntu 16.04 droplet where I could be choosing between PHP 5.6 and PHP 7.1 for all my different websites (Virtual Hosts).

In order to make this work, we will be using FastCGI to switch between php 5.6 FPM and 7.1 FPM. According to their homepage https://php-fpm.org/:

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.

In order to install two versions of PHP on the same machine you will also have to install ondrej/php which will allow you to co-install both on the same server.

Step by step guide on installing PHP 5.6 and 7.1 on an Apache server

1. Open terminal, create an ssh connection to your server and install all the needed packages including PHP 5.6, PHP 7.1 and ondrej/php. 

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo 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

2. Enable the two necessary php modules we will be using. FastCGI and Actions.

a2enmod actions
a2enmod fastcgi

3. Create two empty files for FastCGI to use.

sudo touch /usr/lib/cgi-bin/php56-fcgi
sudo touch /usr/lib/cgi-bin/php70-fcgi

4. Open your PHP 5.6 FPM configuration file and set PHP 5.6 as your main version of PHP. In order to do so open the configuration file with

sudo nano /etc/apache2/conf-available/php5.6-fpm.conf

and add the following to beginning of the file and save it with ctrl+X following a Y and ENTER

<IfModule mod_fastcgi.c> AddHandler php56-fcgi .php Action php56-fcgi /php56-fcgi Alias /php56-fcgi /usr/lib/cgi-bin/php56-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php56-fcgi -socket /var/run/php/php5.6-fpm.sock -pass-header Authorization Action php71-fcgi /php71-fcgi Alias /php71-fcgi /usr/lib/cgi-bin/php71-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php71-fcgi -socket /var/run/php/php7.1-fpm.sock -pass-header Authorization </IfModule> <Directory /usr/lib/cgi-bin> Require all granted </Directory>

4. Now we can enable the newly edited configuration file

sudo a2enconf php5.6-fpm

5. If you have any version of PHP installed on your server it is time to disable it and restart Apache. If you do not now what version of PHP you have active, just use

php -v

to find it out. Disable your installed PHP versions using the following command. Do not forget to swap the version with your corresponding version of PHP. Here we are assuming you have version 7.0 installed

sudo a2dismod php7.0
sudo systemctl restart apache2

5. Enable the use of htaccess, which is a folder specific configuration file for apache by opening your apache configuration file

sudo nano /etc/apache2/apache2.conf

and searching for “<Directory” using ctrl+W and replacing “AllowOverride None” with “AllowOverride All”. If you have trouble doing so check out this link. Save the changes and restart apache.

sudo systemctl restart apache2

6. Your server is now running on PHP 5.6. For the specific project you would like to use PHP 7.1, go to the root folder of that website (where your main website files are located aka. public_html) and create a file (if it does not already exist) with the name “.htaccess” and add the following line to the end of the file and save it

AddHandler php71-fcgi .php

7. To check it all worked create a file called “phpcheck.php” in the root folder of your website and add the following text to the file and save it

<?php phpinfo() ?>

Now go to “http://yourwebsite.com/phpcheck.php” to see what version of PHP is used for that specific website.

Conclusion

Well, that’s pretty much it. Just keep in mind that you will have to do step 6 every single time you are creating a project based on PHP 7.1.

 

happy coding!

Leave a Reply