Install PHP 7 on Ubuntu Linux

Less than a week ago, PHP 7.0 was released. It's the first major release since PHP 5.0 was released in 2004, and there have been some pretty incredible improvements. For the past few weeks, I've been perusing the interwebs looking at various performance benchmarks of PHP 7 vs PHP 5.6. Most, if not all, statistics I've seen have shown that in almost all cases, it performs at least 2x quicker for the exact same code base.

Borrowing the script from http://www.php-benchmark-script.com, I ran a few performance tests of my own.

Check out this output for PHP 5.5:

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2015-12-08 03:27:18
Server : ubuntuserver@10.7.1.154
PHP version : 5.5.9-1ubuntu4.11
Platform : Linux
--------------------------------------
test_math                 : 0.826 sec.
test_stringmanipulation   : 0.916 sec.
test_loops                : 0.528 sec.
test_ifelse               : 0.415 sec.
--------------------------------------
Total time:               : 2.685 sec.

The difference for PHP 7 is pretty astounding:

--------------------------------------
|        PHP BENCHMARK SCRIPT        |
--------------------------------------
Start : 2015-12-08 03:42:44
Server : ubuntu@10.7.1.155
PHP version : 7.0.0
Platform : Linux
--------------------------------------
test_math                 : 0.225 sec.
test_stringmanipulation   : 0.337 sec.
test_loops                : 0.224 sec.
test_ifelse               : 0.267 sec.
--------------------------------------
Total time:               : 1.053 sec.

With the stable release (I never played with the beta), I attempted to install PHP 7.0. I ran into a few struggles, but I found that turning to Google didn't provide me with much, as it's still not exactly mainstream. I finally got it up and running, so I figured I'd share what I did to install. Some of these steps may be common place to you, but I'm by no means a Linux SysAdmin.

I ran this successfully on Ubuntu 14.04, but I'm sure this will work with newer versions (15.04, 15.10, etc) as well as older versions too. Eventually, newer releases of Ubuntu will come with PHP 7 by default, but this will have to hold us over until then. Open up a command prompt and type in the following commands:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php-7.0
$ sudo apt-get update
$ sudo apt-get install -y php7.0

In addition, you may have to run the following command to install some of the other necessary plugins. It wasn't until this that I was able to actually execute code through Apache.

$ sudo apt-get install libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json

Test to see if it's running

Once you have all the above finished, to test and make sure that PHP is installed correctly, simple execute the following command.

php -v

You should see something similar to this:

You should see version PHP 7 listed as the current version. If that still isn't enough proof for you, this option might actually even be better for you. Navigate to /var/www/html on your Linux box, create a new file called "index.php" (which you can do by typing touch index.php) and then opening that file in a text editor.

Enter the following code:

<?php
    phpinfo();
?>

Now, in a browser, if you navigate to "http://localhost", you should be greeted with a purple screen that says, "PHP Version 7.0.0" in a large heading.

Install MySQL and Apache

The above step will only work if you have an Apache Web Server running. In my case, 99% of the time that I install Apache, I also install MySQL in conjunction so I can have a complete LAMP stack. Use the following commands to install either Apache or MySQL.

Apache:

$ sudo add-apt-repository ppa:ondrej/apache2
$ sudo apt-get update
$ sudo apt-get install apache2

MySQL:

$ sudo add-apt-repository -y ppa:ondrej/mysql-5.6
$ sudo apt-get update
$ sudo apt-get install mysql-server-5.6

Did this work for you? Did you run into any issues? Let me know in the comments.