How to install PHP 7.0 or newer on Ubuntu

Install PHP in couple simple steps

"PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world."(www.php.net)

This tutorial will show you how to install PHP on Ubuntu in a couple of simple steps. There is some additional information added for those of You that are using Nginx to make sure it is working correctly.

Requirements

In order to follow this tutorial you will need the following things:

- server running on Linux Ubuntu(I am personally using Amazon AWS and Google Cloud)

- root access

Connect to your server

Connect to the server first using SSH. If you are on the Linux or Mac it is a matter of seconds, on Windows you would be using Putty or web browser(Amazon AWS and GCP offer that).

Start with the customer – find out what they want and give it to them.

Odoo text and image block
Odoo image and text block

Prepare your server

First of all get a sudo access:

sudo su

Update the Linux:

apt-get update && apt-get upgrade

Surely check if there is PHP already installed:

php -v

If your system says that there is already PHP installed and your PHP code is not working(for example contact form) then you might need to install only some additional packages.

Install desired packages

For a list of the packages available:

apt-cache search --names-only ^php

Installing some popular modules:

apt-get install php-pear php7.0-dev php7.0-zip php7.0-curl php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-xml

Yes, you guessed it right - you can choose your PHP version at this stage

Odoo text and image block
Odoo image and text block

Using Nginx? Check this out

Nginx 

has no native processing for PHP so you will have to install fpm:

sudo apt-get install php-fpm php-mysql

Once it is installed adjust the configuration by uncommenting cgi.fix_pathinfo and setting it to "0":

sudo nano /etc/php/7.0/fpm/php.ini

Restart both Nginx and fpm to apply changes:

sudo systemctl restart php7.0-fpm

Remember to update your Nginx config in sites-available

sudo nano /etc/nginx/sites-available/your-site.com

by adding:

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }

Then restart Nginx:

service nginx restart

Test your PHP

You can test your PHP configuration by creating a PHP file in any of your hosted websites(as in your Nginx or Apache config). For example /var/www/html folder can be used:

sudo nano /var/www/html/info.php

Simply paste this inside:

<?php
phpinfo();

Now simply access your config through the web browser by entering:

www.your-site.com/info.php

if you are not using a domain yet you can type:

your.server.ip/info.php

Remember to remove the file with config info to avoid sharing your PHP info with some random hackers;)


Odoo text and image block

References

I am not an expert and this tutorial was created to gather all useful info about the topic in one place. The following resources are recommended if you want to learn more:

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

How to install PHP (7 or 7.2) on Ubuntu

- The main PPA for supported PHP versions with many PECL extensions *****

PHP Manual

If you need any help feel free to contact me.