How to Set Up Multiple Versions of PHP in Ubuntu 12.04 With VirtualMin

Setting up multiple PHP versions in Ubuntu 12.04 with VirtualMin is simple enough, but not as simple as executing sudo apt-get install php5.5 and sudo apt-get install php5.6 and be done with it. You’ll need to specify the exact subversion you want to install (e.g. PHP 5.5.9, a minimum requirement for Laravel 5.2 folks) and then you’ll need to tell VirtualMin that there are multiple PHP versions present in the system, so that it would show up nicely inside VirtualMin user interface.

VirtualMin provides such a tutorial but it only applies to CentOS. So I built this tutorial, based on this tutorial, this cookbook, and this forum thread.

Disclaimer: This is just a personal note, a way to remind me on how to do things weeks or months after this post was written. You may use it as a guide, but there is no guarantee that the steps described here will work on your system. My sistem consists of Ubuntu 12.04.2 with Apache 2.2.22 and VirtualMin 5.04.

In a nutshell, follow this steps:

  1. Install phpbrew
  2. Prepare applications needed to build PHP
  3. Initiate phpbrew
  4. Install and build your choice of PHP version
  5. Integrate with VirtualMin user interface
  6. Set up local PHP.ini copies
  7. Set up PHP usage during SSH session

Step 1. Install phpbrew

Why phpbrew? It helps you build and install multiple versions of PHP. You can do this as root and put the build directory for server-wide use, or you can do this as a user with sudo access and put the build directory inside $HOME path. You can visit phpbrew’s Github page for more information.

Let’s open your terminal and download phpbrew:

And move it to some directory listed in your $PATH:

Step 2. Prepare applications needed to build PHP:

Building and installing PHP with phpbrew will require some applications and libraries. We need to download them, as root:

Step 3. Initiate phpbrew

As stated above, phpbrew can be used to build and deploy a PHP version inside $HOME path, so that each user can have his/her own version of PHP. To integrate with VirtualMin, you’ll need a server-wide installation of multiple versions of PHP, which requires you to initiate phpbrew as root.

So, while you’re still in root mode, do this:

The above command will initiate phpbrew, set up all environment variables and create directory .phpbrew under $HOME (in our case it’s /root/.phpbrew).

You can check environment variables created by phpbrew:

Next, edit ~/.phpbrew/init file with your favorite text editor, and add these extra lines at the end of the file:

Those lines mean any PHP versions built and installed with phpbrew will be placed inside directory /opt/phpbrew.

Save, quit the text editor, and copy phpbrew’s bashrc to $PHPBREW_HOME path:

Next, we’ll also need to edit /etc/bash.bashrc file, and add these extra lines so that every user can now access phpbrew.

At this point, you can check PHP versions available for installation via phpbrew:

If the version you’re looking for is not listed, you can update phpbrew’s release info:

 Step 4. Install and build your choice of PHP version

You have phpbrew and its supporting applications installed. Now it’s time to install a PHP version. In my case, I need to install PHP 5.5.9:

The above command will build and install PHP 5.5.9 with some variants needed for Ubuntu 12.04. Other variants of installation are also available and you can check it on phpbrew’s GitHub page.

Wait for about 7.5 minutes (in my case). After it finished, restart your Apache server.

Step 5. Integrate with VirtualMin user interface.

We need to tell VirtualMin of these new versions of PHP built with phpbrew. VirtualMin looks for PHP versions at several paths, one of them is /opt/rh. So, we’ll create a symbolic link from phpbrew’s build directory to /opt/rh (while still in root mode):

Now, log into Webmin as a root, choose VirtualMin/System Settings/Re-Check Configuration. You’ll see the new PHP version listed at “The following PHP versions are available…”.

virtualmin_d

At this point, you can exit from the root mode:

Step 6. Set up local PHP.ini copies

The new version of PHP is now placed below /opt/phpbrew along with its PHP.ini. But if we’re going to make this version of PHP available for a Virtual Server, we need a way to create a local copy of this PHP.ini for that Virtual Server. VirtualMin looks for a local copy of PHP.ini at ~/etc/php5.4~/etc/php5.5, or ~/etc/php5.6, depending on which PHP did you choose. In my case, I want to put a local copy of PHP.ini at ~/etc/php5.5.

But first, if you’re using bin/sh as your shell, you’ll need to change it to bin/bash. Otherwise, you’re going to see ‘source command not found‘ error. More information is available at this thread.

While logged in as a user, copy the default PHP.ini from /opt/phpbrew to ~/etc/php5.5:

A slight modification is needed if you’re setting up a subserver. You’ll need to copy the PHP.ini file to ~/domains/subserver.yourdomain.com/etc/php5.5.

That’s it. Log into VirtualMin as a user, choose VirtualMin/Services and you’ll see versions of PHP listed at the menu (In my case, I’ve installed PHP 5.5.9 and PHP 5.6.24)

virtualmin_b

Now, go to VirtualMin/Server Configuration/PHP Versions and choose the PHP version you want.

virtualmin_e

That’s it. Now, try to run a php script with phpinfo(); command in it. You should see something like this:

As you can see, the system recognizes the new PHP 5.5.9 I just installed and loaded the local PHP.ini at ~/etc/php5.5/php.ini.

8. Set up PHP usage during SSH session

So far, we’ve been able to switch the PHP version inside VirtualMin. But what if we also wanted to switch the PHP version inside a SSH session? For example, if you’re a Laravel 5.2 developer, you’ll spend some time tinkering with php artisan commands via terminal, and you’re at least going to need PHP 5.5.9 for that.

Recall that we haven’t switched the PHP version during our SSH session. So, if we logged in as a user and run

we’ll be shown the version of our native non-phpbrew PHP (in my case, it’s PHP 5.3.10).

How to use PHP 5.5.9 which we have built? phpbrew uses the switch command to switch your PHP version:

But, to be able to use this command, you need to find a way to use the php version we’ve built (as root) at /opt/phpbrew/php/php-5.5.9/bin. And if you run export -p command, you’ll notice these environment variables:

So, if you’re not currently in root mode, the command

will result in this:

Do we need to build and install PHP version for each user? Of, course not. This can be solved by pointing the configuration files to user’s ~/.phpbrew (this is just the default folder to keep phpbrew settings, any other folder is fine), while keep using the PHP versions built on /opt/phpbrew folder.

There should be no more error messages. And if we run php -v command this time, we’ll get the PHP version we want:

Furthermore, we can add those two lines into ~/.bashrc file, so that each time we logged in, we’ll immediately switch to PHP 5.5.9.

That is all. Thanks for reading.

1 Comment

  1. RunCloud

    Great post you have here. Did you know that if you manage your server with RunCloud, you can just change the PHP version just a click. We developed RunCloud which will configure your server to the best configuration so you can focus only on things that matters. Check out http://runcloud.io to get your 15 days free trial.

Leave a Reply

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