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.
sudo apt-get install software-properties-common -y
- 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.
sudo add-apt-repository ppa:ondrej/php
- 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:
sudo apt-get install php7.0 php7.0-fpm php7.0-mysql libapache2-mod-php7.0 libapache2-mod-fcgid -y
- 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,
sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y
- After installing both PHP versions, start the php7.0-fpm service
sudo systemctl start php7.0-fpm
- Now, verify the status of php7.0-fpm service:
sudo systemctl status php7.0-fpm
- You’ll see the following output:
- Repeat the same process for another PHP version, start the php7.2-fpm service:
sudo systemctl start php7.2-fpm
- Verify the status of php7.0-fpm service:
sudo systemctl status php7.2-fpm
- You’ll see the following output:
- 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:
sudo systemctl restart apache2
- 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:
sudo mkdir /var/www/subdomain1.your_domain sudo mkdir /var/www/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:
sudo chown -R www-data:www-data /var/www/subdomain1.your_domain sudo chmod -R 755 /var/www/subdomain1.your_domain sudo chown -R www-data:www-data /var/www/subdomain2.your_domain sudo chmod -R 755 /var/www/subdomain2.your_domain
- 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:
sudo nano /var/www/subdomain1.your_domain/info.php
- Add the following line:
<?php phpinfo(); ?>
- Save and close the file. Now also add it in second directory subdomain2:
sudo nano /var/www/subdomain2.your_domain/info.php
- Add the following line:
<?php phpinfo(); ?>
- 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.
sudo nano /etc/apache2/sites-available/subdomain1.your_domain.conf
- 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:
sudo nano /etc/apache2/sites-available/subdomain1.your_domain.conf
- Add the following content. Make sure the website directory path, server name, and PHP version match your setup:
<VirtualHost *:80> ServerAdmin admin@subdomain1.your_domain ServerName subdomain1.your_domain DocumentRoot /var/www/subdomain1.your_domain DirectoryIndex info.php <Directory /var/www/subdomain1.your_domain> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> <FilesMatch \.php$> # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/subdomain1.your_domain_error.log CustomLog ${APACHE_LOG_DIR}/subdomain1.your_domain_access.log combined </VirtualHost>
- 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:
sudo nano /etc/apache2/sites-available/subdomain2.your_domain.conf
- Add the following content. Again, make sure the website directory path, server name, and PHP version match your unique information:
<VirtualHost *:80> ServerAdmin admin@site2.your_domain ServerName site2.your_domain DocumentRoot /var/www/site2.your_domain DirectoryIndex info.php <Directory /var/www/site2.your_domain> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> <FilesMatch \.php$> # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost" </FilesMatch> ErrorLog ${APACHE_LOG_DIR}/site2.your_domain_error.log CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined </VirtualHost>
- Save and close the file when you are finished. Then check the Apache configuration file for any syntax errors:
sudo apachectl configtest
- You’ll see the following output:
Syntax OK - Next, enable both virtual host configuration files:
sudo a2ensite subdomain1.your_domain sudo a2ensite subdomain2.your_domain
- Now disable the default site, since you won’t need it.:
sudo a2dissite 000-default.conf
- Finally, reload and restart the Apache service to implement your changes:
sudo systemctl reload apache2 sudo systemctl restart apache2
- 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.