Use multiple PHP versions on Ubuntu 16.10 with Apache 2 and in the CLI

Submitted by Peter Majmesku on Thu, 11/10/2016 - 16:55

I needed an easy ability to switch multiple PHP versions on my local development environment. This ability should include the command line interface and webserver components of PHP. Here I am sharing my findings with you. I guess that this functionality is not new on Ubuntu 16.10. Most probably you can use the features already on Ubuntu 16.04 (LTS version) also.

You can use multiple PHP versions with one Apache server. Easily install them via the Apt package manager by the famous PPA from Ondrej. Then you can install multiple PHP versions like so:

sudo apt install php7.1 php5.6 php7.0

For being able to switch the PHP versions within the Apache webserver, you need the Lib Apache packages. You can install them like so:

sudo apt install libapache2-mod-php7.0
sudo apt install libapache2-mod-php7.1
sudo apt install libapache2-mod-php5.6

Afterwards you are able to switch the PHP version, which is being executed by Apache. E.g.:

sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

To switch he PHP CLI version, you can use the "update-alternatives" command. This command let's choose you the various versions of your program. In the background it will create symlinks for you. For getting into the version chooser dialogue, execute the following command:

sudo update-alternatives --config php

To switch PHP extensions for a specific PHP version, you can use the "phpenmod" and "phpdismod" commands. For enabling the Xdebug PHP extension for PHP 7.0, use the following command:

sudo phpenmod -v 7.0 xdebug

Apache 2: Troubleshoot Vhost config for local development

Submitted by Peter Majmesku on Mon, 10/17/2016 - 09:46

General permissions

If you get this message in the webbrowser:

You don't have permission to access / on this server

and this one in the error.log file from Apache:

[Mon Oct 17 08:44:39.025804 2016] [core:error] [pid 8335:tid 139844481865472] (13)Permission denied: [client] AH00035: access to / denied (filesystem path '/home/peter/Dev') because search permissions are missing on a component of the path

then you need to execute the following command on your user folder (for me: /home/peter):

chmod +x /home/YOUR-USERNAME

If Apache is displaying your PHP file's code in the webbrowser, then run this command (worked for PHP7 on Ubuntu 16.04):

sudo apt install php libapache2-mod-php

Cache folder permissions

I assume you have your webroot files in your userfolder at /home/USER. As the PHP process is writing cache files as the www-data user per default (on Ubuntu/Debian), you must configure user and group permissions. Add the www-data user into the user's group and your user into the www-data group. So that they can use their files. The example command is:

sudo usermod -a -G www-data YOUR-USERNAME

sudo usermod -a -G YOUR-USERS-GROUP www-data

Then make the www-data user (as which Apache is acting), act by your user's group. This setting can be made in the file at /etc/apache2/envvars. Modify the lines as follows:

export APACHE_RUN_USER=www-data

Apache2 RewriteRule: Map images folder from remote system to your local virtual host

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:33

With the following directive in your Apache2 virtual host file (at /etc/apache2/sites-available) you must not download the images from products etc. on your local system for development. You just map your local folder location to the remote one. The following example is for a Drupal site (files are under /sites/default/files):

RewriteEngine On
RewriteOptions Inherit
RewriteRule /sites/default/files/(.*)\.(jpeg|jpg|gif|png)$ http://www.my-website.com/sites/default/files/$1.$2 [L,R=301]


This 1 line does also work:

RedirectMatch ^/media/(.*)\.(jpeg|jpg|gif|png)$ https://www.my-website.com/static/media/$1.$2

Subscribe to Apache