< All Docs

How to run different websites with different versions of PHP

Create a Microhost account to try this guide with a $50 credit.

Table of Content


If you run a system or web server, you need to know how to install and use different versions of PHP on the same server. This is very helpful when you have more than one PHP programme on your server and each one works with a different version of PHP.

Information of Default PHP version:

Step 1 : First check the current version of PHP, by creating a file in the default directory with the below code.

# vim /var/www/html/info.php
// Show all information, defaults to INFO_ALL
// Show just the module information.
// phpinfo(8) yields identical results.


 Step 2: now go to browser and search for : server_ip/info.php

This page will show you the default php information.

Run sites with multiple versions of PHP

1. By .htaccess method

Step 1.1 First ensure that parameter “AllowOverload" is set to "All” in /etc/apache/apache.conf.

To confirm this, open the file, /etc/apache/apache.conf( In our case we used apache service on Ubuntu) and search for below line code.

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

Step 1.2 Now create or open the .htaccess file from /var/www/html/site1/ and paste the following:

# vim /var/www/html/site1/.htaccess
<FilesMatch ".(php4|php5|php3|php2|php|phtml)$">
# From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://"

Note: To run your site with another php version, just change the phpx.y-fpm parameter on the above line ( line- 4)as per your requirement.

Step 1.4: Now run the below command to reflect the changes.

#  a2enmod proxy_fcgi setenvif  

Step 1.3: Now to check the current version of PHP for this website, create another info.php file but this time in /var/www/html/site/ and fill the info.php file with same content.

 go to your browser and search for server_ip/site1/info.php

2. By Virtual Host method

Step 2.1: To use the Virtual Host method, you need to open the config file in which you have defined the <VirtualHost > tag and add the below line to your configuration file.

 <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"

Step 2.2: Now just restart the web server's service. and go to browser to search the php.info just you did in step: 1.3

How to change the default version of PHP on your system

Step 1: To change the default version of your php running on your machine. Just enter the below command.

 # update-alternatives --config php 

Previous How To Reset Your MySQL or MariaDB Root Password on Ubuntu 18.04
Next How to Save a Command Output to a File in Linux
Table of Contents