Instructions for installing LEMP on Ubuntu 18 - HOSTVN Blog

Install LEMP on Ubuntu 18 - LEMP stands for Linux, Nginx (Engine-X), MariaDB / MySQL and PHP to create a Web server environment to help deploy websites on the Internet.

In this article HOSTVN will guide you to install LEMP on Ubuntu 18.

Instructions for installing LEMP on Ubuntu 18

1. Request

To follow this guide, you need to have a VPS using the OS Ubuntu 18.04. If you do not have a VPS, immediately refer to the packages Cloud VPS of the HOSTVN.


2. Install LEMP on Ubuntu 18

2.1. Step 1: Install Nginx

First you need to install some necessary libraries by running the following command

In this tutorial HOSTVN will guide you to install Nginx mainline latest version at the time of writing v17.10. To install Nginx You need to create source.list with the following command:

Next, enter nginx signing key so apt can verify the authenticity of the packages:

Then proceed to key verification

Screenshot_3 - installing LEMP on Ubuntu 18

To install Nginx, run the following commands:

To check the Nginx version, use the following command

Screenshot_4 - install LEMP on Ubuntu 18

Run Nginx with the following two commands

To check if Nginx is working, use the command

Screenshot_5 - installing LEMP on Ubuntu 18

2.2. Step 2: Install MariaDB

To install MariaDB 10.4 on Ubuntu 18.04, you will need to add MariaDB repository to the system. First you need to install software-properties-common with the following command:

Then you run the following command to add the Repository Key to the system


Next add repository with the following command

To install Mariadb, run the following commands:

Next you proceed to place root password for Mariadb by running the following commands one after the other

Instead Password-You-Want-Set with your password


After setting the root password, run the following commands to start Mariadb

Finally proceed to security MariaDB with the following command

Enter current password for root (enter for none): Enter the root password you have set
Switch to unix_socket authentication (Y / n): n
Change the root password? (Y / n): n
Remove anonymous users? (Y / n): Y
Disallow root login hack? (Y / n): Y
Remove test database and access to it? (Y / n): Y
Reload privilege tables now? (Y / n): Y

To check if MariaDB is running, use the command


2.3. Step 3: Install PHP-FPM

a. Install PHP-FPM

To install PHP you should use ppa: ondrej / php PPA to be able to install the latest PHP-FPM version

Next you install PHP with the following command. In this article HOSTVN will install PHP 7.4

b. Configure PHP-FPM

First, back up the default configuration file

Next you open the file /etc/php/7.4/fpm/pool.d/www.conf

Edited into

Edited into

Edited into

Edited into

c. Configure php.ini

All necessary php parameter configuration will be in the file /etc/php/7.4/fpm/php.ini. Before editing, you should backup the file php.ini with the following command

Some basic parameters you can edit as follows

You revise it as follows:

d. Start PHP-FPM

After the installation process has completed, run the following two commands to start PHP-FPM

To check if php-fpm is already running, use the following command

Screenshot_8 - installing LEMP on Ubuntu 18

When you use PHP-FPM. All PHP configuration files are located in the directory /etc/php/7.4/fpm

Screenshot_10 - install LEMP on Ubuntu 18

2.4. Step 4: Install PhpMyAdmin

a. Install PhpMyAdmin

To install PhpMyAdmin you run the following commands:

  • Note: Access To get the link down to the latest version
b. Configure PhpMyAdmin

Next you proceed to configure PhpMyadmin:

Open the file /usr/share/phpMyAdmin/ and edit the following parameters

- Find

Add any paragraph of characters between the single quotes. For example:

- Next add the following code

Then you need to create folders tmp for PhpMyAdmin

c. Security configuration for PhpMyAdmin

You open the file /etc/nginx/conf.d/default.conf delete all content and replace with the following:

Next you run the following command:

Open the file /etc/nginx/nginx.conf and add above the include line /etc/nginx/conf.d/*.conf; the following rule

Screenshot_11 - install LEMP on Ubuntu 18

Restart Nginx to reload the configuration

Then you can access: address-vps / pma and login with user as root and password is MariaDB root password

Screenshot_12 - installing LEMP on Ubuntu 18

2.5. Step 5: Create a Virtual host

Virtual Host A configuration file that allows multiple domains to run on the same server. All vhost files will be in the directory /etc/nginx/conf.d/. For convenience, each website should have a separate vhost, for example:

In this example, the website will be created with the corresponding vhost is /etc/nginx/conf.d/ with the following content:

Next, you need to create the directory containing the website source code and the directory containing the log file with the following commands

Restart Nginx to load the configuration

After configuration is complete you point the domain to vps and then create the file /home/ With the following content and type your domain name in the address bar of the browser to check


3. Tài liệu tham khảo

4. Kết luận

Qua bài viết này HOSTVN đã hướng dẫn các bạn cách cài đặt LEMP trên Ubuntu 18. Nếu có bất kỳ ý kiến đóng góp nào các bạn có thể để lại bình luận ở bên dưới. Ngoài ra các bạn có thể xem thêm Hướng dẫn cách cài đặt LEMP trên Centos 7.