PHP 7 on Ubuntu

Dec 31, 2015

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

<code class="highlighter-rouge">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

<code class="highlighter-rouge"><code class="highlighter-rouge">

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.<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash">

From this<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash">

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 class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash">

To this<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash">

“` 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 <code class="highlighter-rouge">fastcgi_pass`.`

<code class="highlighter-rouge"><strong>From this</strong>``<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash">

``` ```

fastcgi_pass unix:/var/run/php5-fpm.sock;

``````
``

To this<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash"><code class="highlighter-rouge">

fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; <code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash"><code class="highlighter-rouge">

Now all the PHP config files are moved to <code class="highlighter-rouge">/etc/php/7.x` from `<code class="highlighter-rouge">/etc/php5/`.`

<code class="highlighter-rouge"><code class="highlighter-rouge">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.``<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash"><code class="highlighter-rouge"><code class="highlighter-rouge"><code class="highlighter-rouge">

You can restart PHP 7 FPM by entering this command.<code class="highlighter-rouge"><code class="highlighter-rouge"><code class="bash"><code class="highlighter-rouge"><code class="highlighter-rouge"><code class="highlighter-rouge">

sudo service php7.0-fpm restart Thank you for reading

If you have any question leave a comment below.


If you like this content, please consider buying me a coffee.

Buy me a coffeeBuy me a coffee

You will get access to contributer exclusive resources and content.

Thank you!



Darryl Dias

I am a student, artist, and author currently living in Florida. My interests range from technology to arts. I am also interested in programming, writing

Analytics by GoSquared