Multiple versions of PHP on Debian/Ubuntu

For some reason, when you search online for how to setup multiple versions of PHP on your Debian or Ubuntu server you will find many articles that state that you have to compile PHP manually. This is not true, as I will demonstrate. Compiling is not necessary, it’s complicated for less experienced users and requires you to install many additional packages on your server.

Having multiple versions is actually quite easy. You just need to install two prerequisites:


apt-get install apt-transport-https ca-certificates

Get the GPG key

wget -O /etc/apt/trusted.gpg.d/php.gpg

Add custom repository:

echo 'deb jessie main' >> /etc/apt/sources.list.d/php.list

Update apt cache:

apt-get update

And now you are able to install different versions of PHP alongside each other. For example you could install PHP 7.1 and php extension for this version like this:

apt-get install php7.1 php7.1-fpm php7.1 php7.1-mysql

PHP 7.1 will be installed to /usr/bin/php7.1 and symlink will be made in /etc/alternatives that will enable you to call this version of PHP from the command line with just ‘php’ command. Other versions you install from will be set up in similar manner. One thing is worth mentioning here – when setting cron jobs that execute PHP scripts via CLI you should set absolute path (e.g. /usr/bin/php7.0), or the version of PHP will change at some point if you install more version of PHP or upgrade your server and that could potentially cause problems with some PHP applications.

Configuring your web server to use this version of PHP is not that hard and there are multiple ways to do this. For example, this is how I configured Apache for an application that requires PHP 7.1:

<FilesMatch \.php$>
  SetHandler "proxy:fcgi://"

PHP-FPM is configured to listen to loopback network interface on port 9071 in /etc/php/7.1/fpm/pool.d/www.conf by commenting out the default socket and adding a new entry, like this:

;listen = /run/php/php7.1-fpm.sock
listen =

That’s all there is to it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reload Image