How to Install PHP 7 on Ubuntu Server 14.04

In This Tutorial You are going to learn How to Install PHP 7 on Ubuntu Server 14.04.

There are two methods we can use to install PHP 7 on Ubuntu Linux. One method is to use a third party repository and install PHP 7 using apt-get install command. Another method is to install and compile PHP 7 from the Source.

Install PHP 7 with PPA - Personal Package Archives

So the first method is to use a third party software repository for Ubuntu Linux 14.04. So let's see how it is done.

Step One

Install Apache Web Server on Ubuntu 14.04

The main purpose of the PHP language is to use as a web scripting language and for that we need a web server. Will be using Apache Web Server for this tutorial.

sudo apt-get install apache2

Step Two

Add Ubuntu PPA/PHP Repository

PHP 7 Still does not comes with the default Ubuntu software repository, but we can use PPA/PHP as our source for PHP 7 installation. So first add the PPA(Personal Package Archives) to the apt source list using add-apt-repository command.

sudo add-apt-repository ppa:ondrej/php

Then update the source list

apt-get update

Step three

Install PHP 7

Now we can install php 7 in Ubuntu server 14.04 using apt-get command.

apt-get install php7.0

Once the Installation is finished, you can type php -v on the terminal to verify the installation.

You can also create a .php file with phpinfo() function, inside the /var/www/html folder to test the installation.

How to Install PHP 7 on Ubuntu Server 14.04

Build PHP 7 from Source Ubuntu 14.04

The Second method of installing PHP 7 on Ubuntu 14.04 is to build PHP 7 from source which requires more that a few steps. But let's do it anyway, as simple as possible.

Step One

Install and Configure Apache Web Server

sudo apt-get install apache2

sudo apt-get install apache2-dev

Then update the Ubuntu apache2 environment variables and restart the apache web server.

sudo source /etc/apache2/envvars

sudo service apache2 restart

Step Two

Install Dependencies

To Build PHP 7 from source, we need to install few dependency packages. This includes bison, re2c and libxml2. We can install all these packages using apt-get command.

sudo apt-get install bison re2c libxml2-dev libtool

Step Three

Download PHP 7

Go to Following URL https://secure.php.net/downloads.php and download the latest PHP 7 bz2 or gz installer to your Ubuntu server.

Download PHP 7 for Ubuntu Linux 14.04
Step Four

Extract the tar file

Create a folder called /usr/src/php7 and extract the downloaded php 7 tar file to the /usr/src/php7 folder.

sudo mkdir -p /usr/src/php7

sudo tar -jxvf php-7.0.6.tar.bz2 -C /usr/src/php7/

If you downloaded gz file instead of bz2, then use the -zxvf flag.

sudo tar -zxvf php-7.0.6.tar.bz2 -C /usr/src/php7/

Step Five

Configure and Install PHP 7

First move into the extracted folder inside the /usr/src/php7/ directory.

cd /usr/src/php7/php-7.0.6/

Then run the configuration script as follows.

./configure --with-apxs2=/usr/bin/apxs --prefix=/usr/local/php7 --with-config-file-path=/etc/php7/apache2 --with-mysqli

Then execute the make and make install commands one by one to build php7 on Ubuntu server 14.04.

sudo make

sudo make install

Also execute Following two commands.

sudo libtool --finish /usr/src/php7/php-7.0.6/libs

sudo chmod 644 /usr/lib/apache2/modules/libphp7.so

Step Six

Create php.ini file

mkdir -p /etc/php7/apache2

cp php.ini-development /etc/php7/apache2/php.ini

Php.ini-development file is inside the folder we extracted.

Step Seven

Create the Apache configuration File

vim /etc/apache2/conf-available/php7.conf

And add the Following lines

<FilesMatch \.php$>

SetHandler application/x-httpd-php

</FilesMatch>

Next enable the configuration File

a2enconf php7

Step Eight

Restart the Apache Web Server

And finally restart Apache server and we are done.

sudo service apache2 restart

Once again You can create a .php file with phpinfo() function, inside the /var/www/html folder to test the installation.

Build PHP 7 from Source Ubuntu 14.04

So that is how we can install PHP 7 on Ubuntu Server 14.04 and the first method is definitely the easiest and the safest method.

If you are a Linux Administrator try to build PHP from source at least once, there are lots of things you can learn from that. But for the production environment I will recommend to use a software repository.