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.
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:
- php7.1-pgsql: PostgreSQL extension
- php7.1-mysql: MySQL extension
- php7.1-memcache: Memcache extension
- php7.1-redis: Redis extension
- php7.1-gd: GD extension
- php7.1-imagick: ImageMagick extension
- php7.1-mbstring: mbstring extension
- php7.1-mcrypt: Mcrypt extension
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:
- Replicate changes from "/etc/php5/fpm/php.ini" in "/etc/php/7.1/fpm/php.ini"
- Replicate changes from "/etc/php5/fpm/pool.d/www.conf" in "/etc/php/7.1/pool.d/www.conf"
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-*