Buy me a Coffee


PHP 7 on Ubuntu

2 min read

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.
``` 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 ```
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-sqlite3 ```
## Switching [NGINX](//nginx.org) to PHP 7 from PHP 5

Edit the NGINX config to work with PHP 7 FPM, go the specific <code class="highlighter-rouge">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; } ```
**To this**
``` 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; } ```
The only line changed is the `fastcgi_pass`.

From this

``` fastcgi_pass unix:/var/run/php5-fpm.sock; ```
**To this**
``` fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; ```
Now all the PHP config files are moved to `/etc/php/7.x` from `/etc/php5/`.

So the path to the <code class="highlighter-rouge">php.ini is now located in <code class="highlighter-rouge">/etc/php/7.x/fpm/php.ini, this means any edits done to the older PHP 5 <code class="highlighter-rouge">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 ```
Thank you for reading,

If you have any question leave a comment below.