How to Install LEMP on Ubuntu

LEMP stands for Linux, Nginx, MySQL, PHP

A server cannot be fulfill without a web server. This article will help you to setup LEMP on your Ubuntu. We will use MariaDB instead of MySQL.

Firstly, we will install nginx, to do this just type this command on your server’s terminal/shell

sudo apt-get install nginx

When you’re done you will see a default page of nginx on your domain/ip. eg. or

Now we will install mariadb as a database server by these commands:

sudo apt-get install mariadb-server
sudo mysql_secure_installation

Meanwhile, the installation process just keep agree and choose what option you prefer and it will ask a root password for the database access once.

Then we will install the PHP with it’s necessary extensions, before that we will use ondrej’s ppa by this

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

Now run this for PHP

sudo apt-get install php7.1-fpm php7.1-cli php7.1-common php7.1-mysql php7.1-xdebug php7.1-opcache php7.1-phpdbg php7.1-mbstring php7.1-gd php7.1-imap php7.1-ldap php7.1-pgsql php7.1-soap php7.1-curl php7.1-zip php7.1-xml php7.1-json

Then we will change a setting into the file /etc/php/7.1/fpm/php.ini for some security reasons. Just find it and make it like this


After that restart the php-fpm service by this

sudo service restart php7.1-fpm

Now you can add your nginx virtual hosts/block as needed. I will show an example below.
At first, remove the default block by this command

sudo rm /etc/nginx/sites-available/default
sudo rm /etc/nginx/sites-enabled/default

Then make a block for yours

sudo nano /etc/nginx/sites-available/

Write the codes same as below

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/;
    index index.php index.html index.htm;


    location / {
        try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;

    location ~ /\.ht {
        deny all;

The block is ready now, need to enable it by this.

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Reload the nginx and you’re done with the server configuration.

sudo service reload nginx

Enabling Xdebug

If you like to enable the Xdebug on your LEMP environment just edit the file /etc/php/7.1/mods-available/xdebug.ini and put the below codes at the end.

Then restart both nginx & php-fpm

sudo service reload nginx
sudo service restart php7.1-fpm