Setting up multiple apache2 instances on Ubuntu 16.04
PHP handling on apache is done via modules of one sort or another, and running multiple version is problematic on a single instance. The solution is to run two instances of apache on the same machine. This allows one instance to run PHP 7 (the default on 16.04), and another can run PHP 5. Since normal http/https is on ports 80 and 443, the second instance will run on ports 81 and 444. Since it is running on the same machine, all file system and database access is the exact same.
All the commands herein have to be run as root, or with
sudo prefixed to the command.
sh ./setup-instance php5from
php5is the suffix for the second site; all commands for the second site will have that suffix. This will keep all of the same configuration for all sites on the new instance, including SSL certificates, other virtual hosts, etc. After running this command, you will see output like this:
Setting up /etc/apache2-php5 ... Setting up /etc/init.d/apache2-php5 ... Setting up symlinks: a2enmod-php5 a2dismod-php5 a2ensite-php5 a2dissite-php5 a2enconf-php5 a2disconf-php5 apache2ctl-php5 Setting up /etc/logrotate.d/apache2-php5 and /var/log/apache2-php5 ...
Install PHP 5: see http://askubuntu.com/questions/756181/installing-php-5-6-on-xenial-16-04 for the source of the directions for this step
apt-get install php5.6 php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml
Change line 1 of
/etc/apache2-php5/mods-available/php5.loadto use the correct path (likely
/usr/lib/apache2/modules/libphp5.6.so). Alternatively, one can use the
php5.6.conffiles installed in
/etc/apache2/mods-available(these will have to be moved to
You may have to configure
php5.6.conf), for example, setting
php_admin_flag engine Onfor the users' home directories.
On the second instance, disable anything that will conflict with the default PHP 5 module; for me this was disabling FastCGI and FPM, but yours may have php7.0; both are listed below. Also enable PHP 5. Note that PHP 5 was likely enabled on the default web server via the installation of the php5.6 package, so that needs to be disabled. Note that depending on what you did in step 4, you may have to replace "php5.6" below with "php5". Also note that some of these modules and configurations that are being disabled may not exist on your system -- that's fine.
a2dismod-php5 php7.0 a2dismod-php5 fastcgi a2enmod-php5 php5.6 a2disconf-php5 php7.0-fpm a2dismod-php5 proxy_fcgi a2dismod php5.6
Remove /etc/apache2-php5/userperms.conf, if it exists, and replace with empty file (that is a FastCGI/FPM file, which is not needed in PHP5, which is not being configured here to use FastCGI/FPM).(this was only for my local configuration)
Make sure the porst you want to use (81/444 in this example) are available: run
nmap localhost(you may have to install the
nmappackage first) to see which ports are currently in use. To find the processes running on a given port, run
lsof -i :81(replace 81 with the port you are investigating).
In /etc/apache-php5/, edit ports.conf, and change ports 80 and 443 to the ports that you want to use (81 and 444, in this example).
In /etc/apache-php5/sites-available/, edit 000-default.conf, default-ssl.conf, and any other web site configuration files that you want running on the second instance. The port numbers have to be changed there as well (from 80/443 to 81/444).
Although the new service (
apahce2-php5) was installed (in
/etc/init.d/), the system isn't aware of it yet. To fix this, see the commands listed in
/usr/share/doc/apache2/README.multiple-instances(which one you use will vary depending on your system).
service apache2-php5 restart
At this point, it should work: using the regular ports for http/https (i.e., no port number) will use PHP 7. Using ports 81/444 for http/https, respectively, will use PHP 5. For example: http://example.com:81/phpinfo.php and https://example.com:444/phpinfo.php.