Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save aaronbloomfield/92c707631a0191152bc7faf0124fd651 to your computer and use it in GitHub Desktop.
Save aaronbloomfield/92c707631a0191152bc7faf0124fd651 to your computer and use it in GitHub Desktop.
Running multiple PHP versions on Apache2 and Ubuntu 16.04

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.

  1. Read /usr/share/doc/apache2/README.multiple-instances

  2. Run sh ./setup-instance php5 from /usr/share/doc/apache2/examples, where php5 is 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 ...


3. 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
   - `add-apt-repository ppa:ondrej/php`
   - `apt-get update`
   - `apt-get install php5.6 php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml`

4. Change line 1 of `/etc/apache2-php5/mods-available/php5.load` to use the correct path (likely `/usr/lib/apache2/modules/libphp5.6.so`).  Alternatively, one can use the `php5.6.load` and `php5.6.conf` files installed in `/etc/apache2/mods-available` (these will have to be moved to `/etc/apache2-php5/mods-available`).

5. You may have to configure `/etc/apache2-php5/mods-available/php5.conf` (or `php5.6.conf`), for example, setting `php_admin_flag engine On` for the users' home directories.

6. 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
  1. 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)

  2. Make sure the porst you want to use (81/444 in this example) are available: run nmap localhost (you may have to install the nmap package 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).

  3. 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).

  4. 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).

  5. 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).

  6. Restart apache2: 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.

@timotick
Copy link

timotick commented Jul 9, 2020

I'm using Ubuntu 18.04 (linode) and I can't seem to find apache2-php5 mentioned in line 12. Any help will be very much appreciated.

Sorry, found it!!. It's apache2@php5.service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment