Install a LEMP Server on Ubuntu

Install a LEMP Server on Ubuntu

LEMP Ubuntu
tutorial - how to install a LEMP Server on Ubuntu

LEMP Server – An Introduction

A LAMP Server (Linux Apache2 Mysql PHP) server is quite known, but there are also alternatives that have both advantages and disadvantages. Depending on the use case, however, it may be more efficient to use an alternative. This article explains how to install a LEMP server (Linux nginX MariaDB PHP). NGINX is pronounced ‘Engine X’, therefore the “E” in “LEMP”.

This is a step by step guide for the installation. In order to test the installation we will also install PHPMYADMIN on LEMP.

Why not use gridscale to quickly create an Ubuntu 16.04 LTS server? This takes only a few seconds and you can start the installation.

Preparing the server

Use the following command to update the server. The installation of updates is important not only for security and stability, but some installations do not work without the current package list – for example, with newly created repositories. APT, the package manager under Debian-based distributions, takes over most of the work. The following command starts the process:

apt -y update && apt -y upgrade && apt -y dist-upgrade

After the process is complete, the server is up to date. To make the Ubuntu Server now a LEMP server, the individual elements are installed step by step. It is recommended to start with NGINX.

Install NGINX

Since Ubuntu 14.04 LTS, NGINX can be installed also over APT. The following command must be executed to install NGINX on your server:

apt -y install NGINX

Then a few lines in the config have to be adjusted. To edit the config, Nano (or another text editor) can be used:

nano /etc/nginx/sites-available/default

The corresponding lines must be modified like this:

location ~ \.php$ {
     	fastcgi_split_path_info ^(.+\.php)(/.+)$;
     	#   	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
     	#   	# With php7-cgi alone:
     	#     	fastcgi_pass;
     	#   	# With php7-fpm:
     	fastcgi_pass unix:/var/run/php7-0-fpm.sock;
     	include fastcgi.conf;

Subsequently, Nano can be terminated after the changes have been saved. Next, MariaDB will be installed.

Installing MariaDB

MariaDB is an alternative to MySQL. It understands the same commands as MySQL and is therefore compatible with all applications that support MySQL. Of course, MariaDB is not an exact copy, so there are also a few differences in the configuration. But more about that later. First, MariaDB must be installed with APT on your server.

apt -y install mariadb-server mariadb-client

A big difference to MySQL is the authentication. As with the installation you may have noticed, MariaDB wanted to have no password for the root account.  This can be set afterwards, but is not necessary for the basic operation of MariaDB. Authentication is based on the Linux user. So if you are connected as root or use sudo, you can access the server. How you can circumvent this will be explained later in this tutorial. First, some programs need to be installed and configured. Now PHP is installed on the server.

Installing PHP

Like MariaDB and NGINX, PHP can be easily installed via APT. However, two additional extensions are required for the successful collaboration of all components. The following command will allow you to install everything needed to run PHP on your server.

apt -y install php php-fpm php-mysql

PHP is now ready.

Installing PHPmyAdmin

As announced at the beginning, PHPmyAdmin can be installed to test the installation at the end. Since the installation of PHPmyAdmin recognizes that MariaDB is used and therefore does not ask for a password, the installation must take place before securing the MariaDB. Conversely, if PHPmyAdmin is to be installed later, the security of MariaDB (see next chapter) must be undone again.
But step by step. With the following command PHPmyAdmin can be installed on the server via APT:

apt -y install phpmyadmin

You must create a link to the correct directory in order for PHPmyAdmin to be available at http: /// phpmyadmin. The following command is used:

ln -s /usr/share/phpmyadmin/ /var/www/html/phpmyadmin

You must also add something to the nginx config in order for phpMyAdmin to be also available under / phpmyadmin.

nano /etc/nginx/sites-available/default

Add the following to the server {…} block:

location /phpmyadmin {
  index index.php;

Subsequently, Nano can be terminated after the change has been saved.

Then restart NGINX to complete the installation:

service nginx restart

If the installation was successful, the login window of PHPmyAdmin should now be visible under http: // <IPoderDomain> / phpmyadmin. Since no password has yet been set for MariaDB, a login is not yet possible.

Now MariaDB and the rest of the server can be backed up.

Securing MariaDB

First you have to login as root user on your own MariaDB instance:

mysql -uroot

The following commands should then be executed in this order to secure MariaDB. It is important to enter your own password instead of “NEW PASSWORD”:

use mysql;
update user set password=PASSWORD("NEWPASSWORD") where User='root';
update mysql.user set plugin='' where user='root';
flush privileges;

Afterwards, a new login with the above mentioned command should no longer work. To register now, the parameter -p or –password is required.

mysql -uroot -p

You must then enter your own password. The login should now be successful.
Now the firewall rules have to be adapted to secure the server.

Setting the firewall

This article (German only) shows various tools that can be used to customize IP tables (the Linux internal firewall). For this tutorial, UFW is recommended for simplicity. The following commands must be used in this order to secure the firewall. If necessary, confirm with y:

apt -y install ufw
ufw allow ssh
ufw allow http
ufw enable

The firewall of the server is thus secured. Use the following command to display the status of the firewall:

ufw statusThe output should now show the logs and ports that are released.

Your LEMP server is now ready to use. To check if everything works, you can log in at http: // <IPoderDomain> / phpmyadmin and take a look at the system overview (at the right edge of the start page after the login).


With a LEMP server as the basis for web applications, you have a stable yet flexible solution. By installing via APT, all packages are updated automatically when the server is updated. A LAMP server can be set up as a comparison. Then you can compare under which configuration the web connection runs better. The tutorial for the LAMP server can be found here.

Zurück zur Tutorial Übersicht Back to Tutorial Overview

LEMP Server – An Introduction A LAMP Server (Linux Apache2 Mysql PHP) server is quite known, but there are also alternatives that have both advantages and disadvantages. Depending on the use case, however, it may be more efficient to use an alternative. This article explains how to install a LEMP server (Linux nginX MariaDB PHP). […]

Schade, dass dir der Artikel nicht gefallen hat.
Was sollten wir deiner Meinung nach besser machen?

Vielen Dank für dein Feedback!
Wir melden uns bei dir, sobald der Artikel zu deinem Wunschthema fertig ist.

Übrigens: kennst du schon unser Tutorial zum Thema Configuring an Apache-Server as Reverse-Proxy on Ubuntu?