How to install LEMP with PHP7 on Ubuntu 16.04 (Xenial Xerus)

Discussion in 'Suggest HOWTO' started by lnxgs, Mar 20, 2016.

  1. lnxgs

    lnxgs Member

    I would like to share a tutorial, I wrote originally in italian, in order to be useful for more people.

    In this tutorial our goal is the setup of a LEMP on the new upcoming version of Ubuntu 16.04 (Xenial Xerus).
    A LEMP is a variant LAMP stack traditionally used for web pages delivery which consists in a mix among Linux, Apache, MySQL and PHP. In the last years, an attractive alternative arrived: its name is LEMP, where Nginx replaced the Apache web server.

    In the following guide we will setup a LEMP on a Ubuntu 16.04 virtual machine, which has as the IP address. This IP address will be used in the examples, so remember to replace this address with your own.

    We will install the Nginx web server along with the new PHP 7 and we will configure PHP-FPM in order to obtain the higest performance. We also cannot miss the installation and configuration of the MySQL database.

    So, let's start!

    As always, the first thing to do is update our operating system with last patches and packages.

    sudo apt-get update && sudo apt-get upgrade

    By default, Ubuntu does not install a ssh server, so we should turn it on (if it is not already installed, of course).

    sudo apt-get install openssh-server

    The most easy step is installing the MySQL database.

    sudo apt-get install mysql-server mysql-client

    During the MySQL installation, we are prompted to enter the root password of the database. Please, enter a password: it will be used for creating tables , users, and other schemas.

    Now we are installing the Nginx web server.

    sudo apt-get install nginx

    Start the web server.

    sudo service nginx start

    Then install PHP 7, PHP-FPM and the packages that enable the connections with the database.

    sudo apt-get install php7.0-fpm php7.0-mysql

    You can also install other packages to extend the functionality of the web server like mcrypt, you like.
    If you would like to disable IPv6 support, open the configuration file of Nginx

    sudo nano /etc/nginx/sites-available/default

    Comment by prefixing the hash character # to the line

    listen [::]:80 default_server;

    The line should become so:

    #listen [::]:80 default_server;

    On the same /etc/nginx/sites-available/default file, search for the line:

    # Add index.php to the list if you are using PHP

    After this line we can find a list of index works.

    After the first index put index.php like this

    index index.php index.html index.htm index.nginx-debian.html;

    Then you should enable PHP-FPM, look for
    # pass the PHP scripts

    Find the line
    # fastcgi_pass unix:/run/php/php7.0-fpm.sock;

    Remove the hash character #, so the line becomes

    fastcgi_pass unix:/run/php/php7.0-fpm.sock;

    Save and close /etc/nginx/sites-available/default file.

    Open /etc/php/7.0/fpm/php.ini file

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

    add at the end of the file the following line:


    Save and close the file.

    Reload the PHP-FPM service.

    sudo service php7.0-fpm reload

    Now we will create a test page on the default root which is /var/www/html/.

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

    Add this code on the file.


    At this point we can open the test page using a browser:

    A web page should appear. This page returns many values: for example, "Server API" must be FPM/FastCGI. This aspect tells us we did a good job.

    If you experienced issues, add a comment...
    Sorry for my English. It is not perfect...
    Based on the italian post Installare NGINX con PHP7, PHP-FPM, MySQL su Ubunut 16.04
    till likes this.
  2. lnxgs

    lnxgs Member

    if you like to upgrade ispconfig to last version and you have installed the php7 cli, please run this command before the upgrade.
    ln -f /usr/bin/php5 /etc/alternatives/php

    At the end run this one:
    ln -f /usr/bin/php7.0 /etc/alternatives/php

    This is needed because the current version of Ispconfig uses some function that are not implemented on PHP7 (such as mysql_).
    Last edited: Apr 16, 2016

Share This Page