It has been quite some time since PHP 7 was released, it has major bug fixes, improved and new ways of writing syntax and many other changes you can find here.

So, I decided to write a guide on how to upgrade or install and setup PHP 7.x.x on your Ubuntu based system.

This is a major update, this can break your website or web application, please test your website or web application in a development environment before applying this to your production environment.

Installing

Adding PHP 7 ppa to the local database.

sudo add-apt-repository ppa:ondrej/php-7.0

Updating the local database

sudo apt-get update Installing PHP 7.Code language: JavaScript (javascript)
Installing PHP 7.
sudo apt-get install php7.0 php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-sqlite3 
Code language: CSS (css)

Now you can run PHP -v in the terminal to check if it has been installed successfully.

Upgrading from PHP 5 to PHP 7

This will purge PHP 5 and remove unwanted dependencies and then clean up the temporary files
sudo apt-get purge php5* && sudo apt-get --purge autoremove && apt-get clean && sudo apt-get install php7.0 php7.0-cli php7.0-common php7.0-curl php7.0-fpm php7.0-gd php7.0-json php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-sqlite3Code language: CSS (css)

Switching NGINX to PHP 7 from PHP 5

Edit the NGINX config to work with PHP 7 FPM, go the specific server config and change the following lines.

From this

location ~ .php$ { try_files $uri =404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;
}Code language: PHP (php)
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;
}Code language: PHP (php)

The only line changed is the fastcgi_pass.

From this

fastcgi_pass unix:/var/run/php5-fpm.sock;Code language: JavaScript (javascript)

To this

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;Code language: JavaScript (javascript)

Now all the PHP config files are moved to "/etc/php/7.x" from "/etc/php5/".

So, the path to the php.ini is now located in /etc/php/7.x/fpm/php.ini, this means any edits done to the older PHP 5 php.ini need to be re-applied because this is a fresh new in file.

You can restart PHP 7 FPM by entering this command.

sudo service php7.0-fpm restart Code language: CSS (css)

Thank you for reading

If you have any question leave a comment below.

I compiled a list of software and services that I use to improve my workflow, here is the link to the list.

Darryl Dias

I’m Darryl. I’m a 3D Artist, Programmer and Linux enthusiast. On this site I share my insights, tips and tricks, tutorials, methods and best practices.