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

nginx default home page test

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

mariadb welcome

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:

http://localhost/phpinfo.php

You should see PHP default test page…

PHP 7.2 ubuntu nginx

Congratulations! You’re successfully installed the LEMP Stack on Ubuntu 16.04 / 18.04 / 18.10…

Enjoy~

You may also like the post below:

Setup Vanilla Forums on Ubuntu 18.04 / 16.04 / 18.10 with Nginx, MariaDB and PHP 7.2-FPM

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.