In this tutorial our goal is the setup of a LEMP on the new 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.
We will install the Nginx web server along with the new PHP 7 and we will configure PHP-FPM in order to obtain the highest performance. We also cannot miss the installation and configuration of the MySQL 5.7 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,…as 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
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.