Upgrade to PHP 7 on Linux


This guide assumes that you are running PHP 5.x on an Ubuntu 16.04 machine, using PHP-FPM in conjunction with Nginx. It also assumes that you have a non-root user configured with sudo privileges for administrative tasks.

Adding a PPA for PHP 7 Packages

A Personal Package Archive, or PPA, is an Apt repository hosted on Launchpad. PPAs allow third-party developers to build and distribute packages for Ubuntu outside of the official channels. They're often useful sources of beta software, modified builds, and backports to older releases of the operating system.

Ondřej Surý maintains the PHP packages for Debian, and offers a PPA for PHP on Ubuntu. Before doing anything else, log in to your system, and add Ondřej's PPA to the system's Apt sources:

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php

Once the PPA is installed, update the local package cache to include its contents:

$ sudo apt-get update

Current PHP packages

This only applies if you are upgrading from a previous version. Note down the current PHP packages you have

$ dpkg -l | grep php | tee packages.txt

Now that we have access to packages for PHP 7.0, we can replace the existing PHP installation.

Upgrading PHP-FPM with Nginx

This section describes the upgrade process for a system using Nginx as the web server and PHP-FPM to execute PHP code.

First, install the new PHP-FPM package and its dependencies:

$ sudo apt-get install -y php7.0

Check PHP Version

Now use the following command to check installed php version on your system.

$ php -v

Note: If you have made substantial modifications to any configuration files in /etc/php5/, those files are still in place, and can be referenced. Configuration files for PHP 7.0 now live in /etc/php/7.0.

Install PHP 7 Modules

You may also need to install modules based on your application requirements. Use the following command to find our available php 7 modules.

$ sudo apt-cache search php7-* 

If you are using MySQL, be sure to re-install the PHP MySQL bindings:

$ sudo apt-get install php7.0-mysql

Updating Nginx Site(s) to Use New Socket Path

Nginx communicates with PHP-FPM using a Unix domain socket. Sockets map to a path on the filesystem, and our PHP 7 installation uses a new path by default:

PHP 5 /var/run/php5-fpm.sock

PHP 7 /var/run/php/php7.0-fpm.sock

Open the default site configuration file with vim (or your editor of choice):

$ sudo vim /etc/nginx/sites-enabled/default

Your configuration may differ somewhat. Look for a block beginning with location ~ \.php$ {, and a line that looks something like fastcgi_pass unix:/var/run/php5-fpm.sock;. Change this to use unix:/var/run/php/php7.0-fpm.sock.


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

Exit and save the file and we should repeat this process for any other virtual sites defined in /etc/nginx/sites-enabled which need to support PHP.

Replace strings in multiple files in bulk by sed:

$ sudo sed -i "s/php5-fpm/php7.0-fpm/g" `grep "php5-fpm" -rl /etc/nginx/sites-available`

Restart Nginx:

$ sudo service nginx restart


Got error PHP Warning: Module 'mcrypt' already loaded in Unknown on line 0

Comment extension=mcrypt.so in /etc/php/mods-available/mcrypt.ini (which should cover both the CLI and FPM SAPIs), like this:

; configuration for php mcrypt module
; priority=20
; extension=mcrypt.so

after doing this, it shows up in the list of compiled modules:

$ php -m | grep mcrypt

WordPress plugin LaTeX for WordPress use preg_replace_callback(); function in wp-content/plugins/latex/latex.php on line 47 and 49, comment this 2 line and comment out code:

$text = preg_replace_callback(
    function($m) {
        return chr(intval($m[1]));
    $text); # decimal notation
$text = preg_replace_callback(
    function($m) {
        return chr(intval("0x$m[1]"));
    $text); #hex notation
4.17 avg. rating (84% score) - 6 votes