How to install PHP 5.6 (as an Apache module) on Ubuntu Server 14.04/14.10

Q: When will PHP 5.6 be in the official Canonical repos?

A: Probably never.

This question was answered by Marc Deslauriers, security engineer at Canonical, and make sense since Canonical point of view about server updates are: stability, security, and updates-that-doesn’t-broken-legacy-code comes first. They don’t want to update because PHP 5.5 syntax are significantly different than 5.3.

So, what we can do if we need or want newer versions than 5.3? An option is to install by compiling the source code, which is awful to maintain. Another option is to install by some rpm found on internet, which not only is hard to maintain, but also probally won’t work without some black magic and many updates to our core system libraries.

Luckly, Ondřej Surý created a non-official PPA that provides an easy way to install and maintain PHP 5.6 (or 5.5, 5.4, and so on).


Our recipe:

1. We need Apache 2.4:

check your version with:

# apache2 -v

This will return something like:

Server version: Apache/2.4.10 (Ubuntu)
Server built: Sep 10 2014 11:32:50

which means that you are using the 2.4 version. If you are using a version lower than 2.2, you need to update to 2.4 (“sudo add-apt-repository ppa:ondrej/apache2”). WARNING: if you update to 2.4, be aware that you will need to review all of your configurations since the structure and syntax are not fully compatible.

2. remove the old PHP, install the new PPA and reinstall PHP:

# sudo apt-get remove php5 libapache2-mod-php5
# sudo add-apt-repository ppa:ondrej/php5-5.6
# sudo apt-get update
# sudo apt-get install php5 libapache2-mod-php5

3. create a test site:
# sudo mkdir /var/www/php-test
# echo “<?php phpinfo() ?>” | sudo tee /var/www/php-test/index.php

4. create the Apache .conf file (don’t forget the “.conf” extension!)
# sudo nano /etc/apache2/sites-available/php-test.conf

<VirtualHost *:80>
DocumentRoot /var/www/php-test.com

DirectoryIndex index.php

AddHandler php-script .php
</VirtualHost>

5. enable the site and restart Apache:

# a2ensite php-test

# sudo /etc/init.d/apache2 restart

6. open the site in your browser and check if its all working


By this way our PHP site will work as an Apache module. The other way is to do via FastCGI, which I intend to learn another day.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s