Share this page: Follow me to be notified of new articles:

Migrate to PHP7 with Nginx + PHP-FPM

Introduction

PHP 7 has been recently released. It offers new features and benchmarks that show a decrease in memory consumption and great improvements in response time.
The guide below offers simple migration steps to switch from PHP5.x to PHP7 using Nginx and PHP-FPM.
PHP 7 migration

Add the package repository

Ubuntu

add-apt-repository ppa:ondrej/php

Debian

Edit "/etc/apt/sources.list":
emacs /etc/apt/sources.list
Add these 2 lines:
deb http://packages.dotdeb.org jessie all
deb-src http://packages.dotdeb.org jessie all
Save, exit and add the GPG key:
wget https://www.dotdeb.org/dotdeb.gpg
sudo apt-key add dotdeb.gpg

Install PHP 7.1

apt-get update
apt-get install php7.1-fpm php7.1-cli php7.1-curl php7.1-xml
Additional popular packages:

Replace PHP-FPM socket in Nginx configuration

sed -i -- 's|/var/run/php5-fpm.sock|/var/run/php/php7.1-fpm.sock|g' /etc/nginx/sites-enabled/*
You can double check that the socket was replaced correctly with grep:
root@server:~# grep '.sock' /etc/nginx/sites-enabled/*
/etc/nginx/sites-enabled/site1:        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
/etc/nginx/sites-enabled/site2:        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
/etc/nginx/sites-enabled/site3:        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
/etc/nginx/sites-enabled/site4:        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;

Personalize PHP-FPM configuration

If you made changes to the PHP-FPM configuration in PHP5:

Restart PHP-FPM 7.1, restart Nginx and stop PHP-FPM 5

/etc/init.d/php7.1-fpm restart # Restarting will apply the configuration changes
/etc/init.d/nginx restart
/etc/init.d/php5-fpm stop

Removing PHP5 extensions

Once you have ensured that everything is working properly you can safely remove PHP-FPM 5 and other PHP5 extensions from your server:
apt-get remove --purge php5-*
Last update of this page: March 27, 2017
Please do not copy any of the content of this website without asking me first.