15th Jun 2021

Multiple PHP Version with Apache on single instance | AWS | Digital Ocean

Web

Written By, Akshay Shah

Overview

There are several reasons you want to use multiple versions of PHP. If you have two applications, one application which runs on PHP 7.0 and a second application which runs on PHP 7.2, then you need multiple PHP versions on your server. You can also use a separate server for each PHP version, but it will increase your hosting cost. Installing multiple versions of PHP is the best way to reduce hosting cost.

Prerequisites

  • A fresh ubuntu server with minimum 1 GB RAM (recommended more for better performance)
  • Two valid domain names pointed with your VPS IP address. In this tutorial, we will use subdomain1.main_domain.com and subdomain2.main_domain.com.

Installing PHP Versions

  • As we have done all required basic setup we are going to install required versions of PHP. As for the demo we are going to set up PHP version 7.0 and 7.2 for that we need to first install the Ondrej PHP repository.
  • The software-properties-common package provides the apt-add-repository command-line utility, which we will use to add the ondrej/php PPA (Personal Package Archive) repository.
  • Now add the ondrej/php repository to the server. The ondrej/php PPA allows you to install multiple versions of PHP in the system.
  • Now start installing PHP versions, let’s start with php7.0.
  • Install php7.0, php7.0-fpm, php7.0-mysql, libapache2-mod-php7.0, and libapache2-mod-fcgid with the following commands:

    • php7.0 is a metapackage used to run PHP applications.
    • php7.0-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
    • php7.0-mysql connects PHP to the MySQL database.
    • libapahce2-mod-php7.0 provides the PHP module for the Apache web server.
    • libapache2-mod-fcgid contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.
  • Now repeat the process for PHP version 7.0,
  • After installing both PHP versions, start the php7.0-fpm service
  • Now, verify the status of php7.0-fpm service:
  • You’ll see the following output:
  • Repeat the same process for another PHP version, start the php7.2-fpm service:
  • Verify the status of php7.0-fpm service:
  • You’ll see the following output:
    php-7-output-2

    • actions is used for executing CGI scripts based on media type or request method.
    • fcgid is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
    • alias provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
    • proxy_fcgi allows Apache to forward requests to PHP-FPM.
  • Now restart the Apache service to apply your changes:
  • So we have successfully installed two PHP versions on the server.

Creating Directory Structures

  • First, create document root directories for both subdomain1.your_domain and subdomain2.your_domain:
  • By default, the Apache web server runs as a www-data user and www-data group. To ensure that you have to correct ownership and permissions of your website root directories, execute the following commands:
  • Next you will create an info.php file inside each website root directory. This will display each website’s PHP version information. Begin with subdomain1:
  • Add the following line:
  • Save and close the file. Now also add it in second directory subdomain2:
  • Add the following line:
  • Your web server should now have the document root directories that each site requires to serve data to visitors. Next, you will configure your Apache web server to work with two different PHP versions.
  • Add the following content. Make sure the website directory path, server name, and PHP version match your setup:

Configuring Apache

  • In this section, you will create two virtual host configuration files. This will enable your two websites to work simultaneously with two different PHP versions.
  • In order for Apache to serve this content, it is necessary to create a virtual host file with the correct directives. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf, you’ll create two new ones inside the directory /etc/apache2/sites-available/.
  • First create a new virtual host configuration file for the website subdomain1.your_domain. Here you will direct Apache to render content using php7.0:
  • Add the following content. Make sure the website directory path, server name, and PHP version match your setup:
  • In this file you updated the DocumentRoot to your new directory and ServerAdmin to an email that the your_domain site administrator can access. You’ve also updated ServerName, which establishes the base domain for this virtual host configuration, and you’ve added a SetHandler directive to run PHP as a fastCGI process server.
  • Save and close the file.
  • Next, create a new virtual host configuration file for the website subdomain2.your_domain. You will specify this subdomain to deploy php7.2:
  • Add the following content. Again, make sure the website directory path, server name, and PHP version match your unique information:
  • Save and close the file when you are finished. Then check the Apache configuration file for any syntax errors:
  • You’ll see the following output:
    Syntax OK
  • Next, enable both virtual host configuration files:
  • Now disable the default site, since you won’t need it.:
  • Finally, reload and restart the Apache service to implement your changes:
  • Now that you have configured Apache to serve each site, you will test them to make sure the proper PHP versions are running.

Testing

We have completed our setup now just test both sites by visiting http://subdomain1.your_domain and http://subdomain2.your_domain. You will see webpages like this, if does not work works wait for 12-24 hours to reflect new changes on servers:

Conclusion

You have now combined virtual hosts and PHP-FPM to serve multiple websites and multiple versions of PHP on a single server. The only practical limit on the number of PHP sites and PHP versions that your Apache service can handle is the processing power of your instance.

Written By,

Web developer at Yudiz Solutions Pvt. Ltd