How to Install LEMP on Ubuntu 16.04 | 18.04 | 18.10

LEMP is an acronym for Linux (Ubuntu), Engine-X (Nginx) HTTP Server, MariaDB or MySQL Database Server and PHP-FPM Scripting Language… It is a group of open source software and building blocks of many of the web applications and majoriy of the content management systems (CMS) in use today…
There are two popular stacks that are mostly use today: LAMP, which we discussed here, and LEMP which this post is about..
Content Management Systems like WordPress, Joomla, Drupal and others, they all primarily use the LAMP or LEMP Stack…
If you’re going to be developing any PHP based applications or websites, then you’re probably going to be using the LEMP or LAMP Stack as well…
This brief tutorial is going to show students and new users how to install Nginx, MariaDB and PHP on Ubuntu Linux 16.04 | 18.04 and 18.10 servers…
To get started with installing the LEMP Stack, follow the steps below:
Step 1: Prepare Ubuntu Linux
The LEMP stack includes the Linux machine… in this case, Ubuntu… To get LEMP you must first install a Linux machine… this post assumes you’ve already install Ubuntu server..
After installing Ubuntu server, run the commands below to update the server…
sudo apt update && sudo apt dist-upgrade && sudo apt autoremove
Step 2: Install Nginx HTTP Server
Nginx HTTP Server represents the E in the LEMP stack… It’s the probably the second most popular web server installed today… not far behind the most popular web server, Apache2…
To install Nginx HTTP on Ubuntu server, run the commands below…
sudo apt update sudo apt install nginx
After installing Nginx, the commands below can be used to stop, start and enable Nginx service to always start up with the server boots.
sudo systemctl stop nginx.service sudo systemctl start nginx.service sudo systemctl enable nginx.service
To test Nginx setup, open your browser and browse to the server hostname or IP address and you should see Nginx default test page as shown below.. When you see that, then Nginx is working as expected..
ex.. http://localhost

Step 3: Install MariaDB Database Server
MariaDB stands for M in LEMP and it’s a great place to start when looking at open source database server… Although MySQL was originally the default database server among Linux systems, MariaDB has taken over.. To install it run the commands below.
sudo apt-get install mariadb-server mariadb-client
After installing MariaDB database server, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots..
On Ubuntu 16.04 LTS
sudo systemctl stop mysql.service sudo systemctl start mysql.service sudo systemctl enable mysql.service
On Ubuntu 18.04 LTS and 18.10
sudo systemctl stop mariadb.service sudo systemctl start mariadb.service sudo systemctl enable mariadb.service
After that, run the commands below to secure MariaDB server by creating a root password and disallowing remote root access.
sudo mysql_secure_installation
When prompted, answer the questions below by following the guide.
- Enter current password for root (enter for none): Just press the Enter
- Set root password? [Y/n]: Y
- New password: Enter password
- Re-enter new password: Repeat password
- Remove anonymous users? [Y/n]: Y
- Disallow root login remotely? [Y/n]: Y
- Remove test database and access to it? [Y/n]: Y
- Reload privilege tables now? [Y/n]: Y
Restart MariaDB server
To test if MariaDB is installed, type the commands below to logon to MariaDB server
sudo mysql -u root -p
Then type the password you created above to sign on… if successful, you should see MariaDB welcome message

Step 4: Install PHP-FPM and Related Modules
The last component of the LEMP stack is PHP-FPM… It’s the P in the LEMP stack… To install PHP-FPM and related PHP-FPM modules, run the commands below…
sudo apt install php-fpm php-common php-mbstring php-xmlrpc php-soap php-gd php-xml php-intl php-mysql php-cli php-zip php-curl
After installing PHP, run the commands below to find the version installed on the server…
php -v
You should see an output like the one below:
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies
The version number determines the location of PHP default configuration file… For PHP 7.2, the location is as shown below:
sudo nano /etc/php/7.2/fpm/php.ini
Replace the version number above with the version of PHP installed..,..
When the file opens, make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.
file_uploads = On allow_url_fopen = On memory_limit = 256M upload_max_filesize = 100M max_execution_time = 360 date.timezone = America/Chicago
After making the change above, save the file and close out.
Step 4: Restart Nginx
When you’re done making PHP changes above, run the commands below to restart Nginx HTTP server for PHP settings to apply…
sudo systemctl restart nginx.service
To test PHP settings with Nginx, create a phpinfo.php file in Nginx root directory by running the commands below
sudo nano /var/www/html/phpinfo.php
Then type the content below and save the file.
<?php phpinfo( ); ?>
Save the file.. then browse to your server hostname followed by phpinfo.php
Next, run the commands below to open Nginx default site configuration file…
sudo nano /etc/nginx/sites-available/default
The uncomment the PHP block as shown in the code below:
# pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; }
save the file and exit..
After that, open your browser and browse to the URL below:
You should see PHP default test page…

Congratulations! You’re successfully installed the LEMP Stack on Ubuntu 16.04 | 18.04 | 18.10…
Enjoy~
You may also like the post below:
Help! I’ve just installed the LEMP stack per these instructions, but when I try to run https://localhost/phpinfo.php I get a 502 Bad Gateway status. Can you help?
I had the same problem – turns out I hadn’t read instructions carefully enough. You need to change the version number inside the default file to php7.2-fpm.sock (in my version case) on line 60 in the file:
/etc/nginx/sites-available/default
then restart nginx
the installed file had php7.0
I think you don’t have to write “HTTPS://” when typing localhost. also as mentioned above write correct “fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;” number in “php….-fpm.sock” mine is 7.2 yours maybe different and make sure only to uncommenting the lines this article mentions.
If pHp infor does not show but you get a the save file then the instructions above were not followed. When they say write out, they mean to remove the # so the command is enabled. Remove the # in front of location ~ \.php$ {
include snippets/fastcgi-php.conf; and the other statements highlighted in the instructions.
Make sure to add index.php to the list
sudo nano /etc/nginx/sites-available/default
# Add index.php to the list if you are using PHP
index index.html index.php index.htm index.nginx-debian.html;
AND change php version from 7.0 to 7.2
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
Restart ngnix
thank !!!!!!
you are a best ! good job
This is the best POST ever since in my life without stress
I love you writer.