How To Run/Enable Many/different PHP Versions(7.0 ,7.2 etc… ) In One Instance On Ubuntu/Apache 16.04

How To Run/Enable Many/different PHP Versions(7.

0 ,7.

2 etc… ) In One Instance On Ubuntu/Apache 16.

04vengadesh sBlockedUnblockFollowFollowingApr 24In mycase i want to use PHP7.

0 and 7.

2 in same instance,PHP7.

0 is available in default,PHP 7.

2 isn’t available on Ubuntu 16.

04 default repositories… in order to install it, you will have to get it from third-party repositories.

Step 1: Update Apache2 and install FastCGIsudo apt updatesudo apt install apache2After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always start up when the server boots.

sudo systemctl stop apache2.

servicesudo systemctl start apache2.

servicesudo systemctl enable apache2.

serviceFirst install Apache2 FastCGI module by entering below commandsudo apt update sudo apt install apache2 libapache2-mod-fastcgi Ubuntu 18.

04 Users:sudo apt install apache2 libapache2-mod-fcgidThe commands above will error out for some candidate… In order to add that package, download it and install it manually by entering below command.

cd /tmp && wget http://mirrors.






debsudo dpkg -i libapache2-mod-fastcgi_2.




deb; sudo apt install -fStep 2: Install PHP7.

2 PHP-7.

2-FPMRun the commands below to add the below third party repository to upgrade to PHP 7.

2sudo apt-get install software-properties-commonsudo add-apt-repository ppa:ondrej/phpThen update and upgrade to PHP 7.

2sudo apt updateNext, run the commands below to install PHP 7.

2 and related modules.

sudo apt install php7.

2 php7.

2-fpm php7.

2-common (or)sudo apt-get install php7.

2 libapache2-mod-php7.

2 php7.

2-mcrypt php7.

2-cli php7.

2-xml php7.

2-zip php7.

2-mysql php7.

2-gd php7.

2-imagick php7.

2-recode php7.

2-tidy php7.

2-xmlrpcEnter php enable commandsudo a2enmod php7.

2Step 3: Configure Apache2 PHP SettingsNow that Apache2 and PHP are installed, you may want to configure Apache2 to use PHP properly.

The default Apache2 PHP configuration file for PHP is located at /etc/php/7.


iniOpen PHP Apache2 configuration file by running the commands belowsudo nano /etc/php/7.


iniThen edit the file to suit your environments.

Scroll down the file line by line and modify the lines for the settings below… always configure an appropriate setting for your environment.

file_uploads = Onallow_url_fopen = Onmemory_limit = 256Mupload_max_filesize = 100Mpost_max_size= 100Mmax_execution_time = 360date.

timezone = America/Chicagomake the same above changes in cli/php.

in and fpm/php.

ini also by below path and save the file.

nano /etc/php/7.


ininano /etc/php/7.


iniAfter save, stop and start the phpsudo pkill php7.

2-fpmsudo service php7.

2-fpm start (or)systemctl restart php7.

2-fpmStep 4: Configure Apache2 HTTP to use PHP7.

2-FPMNow that both Apache2 and PHP7.

2-FPM are installed, run the commands below to enable FastCGI module for Apache2sudo a2enmod actions fastcgi alias proxy_fcgiNext, open Apache2 default.

conf configuration file.

In mycase i am not using 000-default.

conf i have copied default.

conf file and renamed to other names like example.


conf and example.


conf1 when creating virtual host.

To avoid confusion while creating virtual host make root folder name for source code and .

conf file are in same name.

sudo nano /etc/apache2/sites-available/example.


confAdd the following lines in .

conf file, then save the file<FilesMatch .

php$># Apache 2.


10+ can proxy to unix socketSetHandler "proxy:unix:/var/run/php/php7.



com/"</FilesMatch>In the below line,SetHandler “proxy:unix:/var/run/php/php7.



com/” example.

com represents the url domain and php7.

2fpm represents php version you have to change that related to your url domain and php version.

Example .


conf file shown below, your .

conf file should look like this<VirtualHost *:80> ServerAdmin admin@example.

com DocumentRoot /var/www/example.

com ServerName example.

com ServerAlias example.

com <Directory /var/www/example.

com> Options +FollowSymlinks AllowOverride All Require all granted </Directory> <FilesMatch .

php$># Apache 2.


10+ can proxy to unix socketSetHandler "proxy:unix:/var/run/php/php7.



com/"</FilesMatch> ErrorLog ${APACHE_LOG_DIR}/error.

log CustomLog ${APACHE_LOG_DIR}/access.

log combined</VirtualHost>In the below line,DocumentRoot /var/www/example.

com and <Directory /var/www/example.

com> are represents the root path and folder where we kept the source code.

In my case i am not using the default DocumentRoot /var/www/html i have using seperate folders to avoid confusionAfter save enable .

conf filesudo a2ensite example.


confsudo a2enmod rewriteStep 5: Restart Apache2 and Testsudo systemctl restart apache2.

service (or)sudo service apache2 restartThat’s it!.This is how you use Apache2 with PHP-FPM (FastCGI) and many PHP versions.

In this tutorial i have explained with PHP7.

2, do the same things for all PHP versions you needEnjoy!.

. More details

Leave a Reply