Adding PHP 7 to your Linux server

PHP 7 has been released for some time now and it seems pretty solid. Besides new features like return type hints, it is up to 3× faster than PHP 5.6. Luckily, for most Linux distributions there are PHP 7 repositories available.

Ubuntu

On Ubuntu 14.04 and later you have to ensure you can add a Personal Package Archives (PPA):


sudo apt-get install software-properties-common

[update] The PPA has changed. You need both ppa:ondrej/php and ppa:ondrej/php-qa.

Add the repositories and install PHP7:


sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/php-qa
sudo apt-get update
sudo apt-get install libapache2-mod-php7.0 php7.0-fpm php7.0-common php7.0-cli php-pear php7.0-curl php7.0-gd php7.0-gmp php7.0-intl php7.0-imap php7.0-json php7.0-ldap php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-ps php7.0-readline php7.0-tidy php7.0-xmlrpc php7.0-xsl
sudo apt-get --purge autoremove

On older Ubuntu versions, you will need to install python-software-properties:


sudo apt-get install python-software-properties

Debian

Add the Dotdeb repository to /etc/apt/sources.list:


deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all

Fetch and install the GnuPG key:


wget https://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg

Install the PHP7 packages:


sudo apt-get update
sudo apt-get install libapache2-mod-php7.0 php7.0-fpm php7.0-common php7.0-cli php-pear php7.0-curl php7.0-gd php7.0-gmp php7.0-intl php7.0-imap php7.0-json php7.0-ldap php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-ps php7.0-readline php7.0-tidy php7.0-xmlrpc php7.0-xsl
sudo apt-get --purge autoremove

CentOS

Add the PHP 7 repository (on CentOS 7):


sudo yum -y install epel-release
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm 
sudo rpm -Uvh remi-release-7*.rpm

Then install PHP 7:


sudo yum -y --enablerepo=yum --enablerepo=remi,remi-php70 install php-fpm php-common php-cli php-pear php-pdo php-mysqlnd php-gd php-mbstring php-mcrypt php-xml php-iconv php-soap php-opcache php-tidy php-json php-openssl php-bcmath
scl enable php70 'php -v'

See which CentOS versions are supported.

openSUSE

Add the PHP 7 repository (on version 42.1):


sudo zypper ar http://download.opensuse.org/repositories/devel:/languages:/php:/php7/openSUSE_Leap_42.1/devel:languages:php:php7.repo

Then install PHP 7:


sudo zypper refresh
sudo zypper install php7 php7-bcmath php7-bz2 php7-calendar php7-ctype php7-curl php7-devel php7-dom php7-enchant php7-exif php7-fileinfo php7-fpm php7-ftp php7-gd php7-gettext php7-gmp php7-iconv php7-imap php7-intl php7-json php7-ldap php7-mbstring php7-mcrypt php7-mysql php7-opcache php7-openssl php7-pdo php7-pear php7-phar php7-posix php7-pspell php7-readline php7-soap php7-tidy php7-tokenizer php7-wddx php7-xmlreader php7-xmlrpc php7-xmlwriter php7-xsl php7-zip php7-zlib 

See which openSUSE versions have a PHP 7 repository.

Docker

There is an official PHP Docker repo.

Start Docker:


docker run -p 80:80 -it --rm --name my-apache-php-app -v "$PWD":/var/www/html php:7-apache

Compile

Of course you can always build from source. You will have to install the required build tools. On Ubuntu this would be:


sudo apt-get install build-essential automake autoconf gcc libtool binutils libxml2-dev libcurl4-openssl-dev libfreetype6-dev libjpeg-dev libpng12-dev libbz2-dev libmcrypt-dev libmm-dev libxslt1-dev

Then compile PHP:


./configure \
--enable-fpm \
--with-gettext --enable-mbstring \
--with-mcrypt \
--enable-soap --with-xsl \
--with-bz2 --enable-zip --with-curl --with-openssl \
--with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --enable-exif \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-mm=/usr
make
sudo make install