We help IT Professionals succeed at work.

Install multiple versions of PHP but specify in host config

Ive usually installed PHP via:-
sudo apt install php libapache2-mod-php php-mysql

Open in new window

Which works great and installs the latest version of PHP and configures everything fine :-)

Problem is I want some of my sites to run on PHP 5 (for historical purposes), some to run on the current version of PHP 7 and some to run on a future version of PHP 7 for testing.

On my hosting website, I can add a line into the vhost configuration to tell it what version of PHP to run, for example to run PHP7 I just add this into the htaccess file and it runs the site with PHP 7:-
AddType application/x-httpd-php70 .php
This is exactly what I want to do, however all the guides Ive found online talk about running everything under the same version, but not individual sites.

Anyone got any ideas on this?

Ideally I want to download different versions of PHP into a folder \PHP\PHP7.3\ and then inside htaccess point to use \PHP\PHP7.3\ or something different.

Im using Ubuntu server with Apache if that helps, and this is only for development purposes, not production.
Watch Question

You're not going to be able to do this on the same server


In the main configuration, add these:

<Directory "/www/htdocs/site1">
AddType application/x-httpd-php70 .php

<Directory "/www/htdocs/site2">
AddType application/x-httpd-php5 .php


Most Valuable Expert 2018
Distinguished Expert 2019
There are plenty of howto guides available through Google to do that - here's a quick example: https://tecadmin.net/install-multiple-php-version-apache-ubuntu/

Basically, you proxy the PHP request through a socket to the correct handler for your specific version of PHP, either through your .htaccess or your VirtualHost config block.
nociSoftware Engineer
Distinguished Expert 2019


Gentoo allows installing multiple version of the php server by default. Still running multiple on one webserver can be challenge.

Probably switching to php-fpm is an option (fastcgi php) then you can run several instances ( different config or versions ) 

and map certain paths to certain fpm backends.  which is considerable simpler to maintain and manage.

(It also allows for setups like for only one of the paths on your site you need humoungous amount of memory for buffering) then you can use a setup with limited number of backends for that one wile the others have multiple instances running.