How To Install LEMP on Ubuntu 20.04 LTS

Install LEMP on Ubuntu 20.04

In this tutorial, we will show you how to install LEMP on Ubuntu 20.04 LTS. For those of you who didn’t know, A LEMP software stack is a group of open-source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Nginx webserver (which replaces the Apache component of a LAMP stack). The site data is stored in a MySQL database (using MariaDB), and dynamic content is processed by PHP.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you through the step by step installation LEMP stack on an Ubuntu 20.04 (Focal Fossa) server.

Install LEMP on Ubuntu 20.04 LTS Focal Fossa

Step 1. First, make sure that all your system packages are up-to-date by running these following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Installing Nginx on Ubuntu 20.04.

Nginx is a high-performance web server and very popular these days. It also can be used as a reverse proxy and caching server. Run the following command from the Terminal to install Nginx webserver:

sudo apt install nginx

Once installed, check to see if the Nginx service is running:

sudo systemctl start nginx
sudo systemctl status nginx

Now if you have your UFW firewall running, you will need to allow connections to Nginx:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'

Check the firewall status:

$ sudo ufw status

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Let us go ahead and run Nginx test page. Go to your web browser and visit your domain or IP:

http://your-server-ip-address 
OR
http://your-domain.com

Install LEMP on Ubuntu 20.04

Step 3. Installing MariaDB on Ubuntu 20.04.

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who are concerned that Oracle might turn MySQL into a closed-source product. Run the following command to install MariaDB:

sudo apt install mariadb-server mariadb-client

Once complete, you can verify MariaDB is installed by running the below command:

sudo systemctl status mariadb

By default, MariaDB is not hardened. You can secure MariaDB using the mysql_secure_installation script. you should read and below each step carefully which will set a root password, remove anonymous users, disallow remote root login, and remove the test database and access to secure MariaDB:

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- 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

To log into MariaDB, use the following command (note that it’s the same command you would use to log into a MariaDB database):

mysql -u root -p

Step 4. Installing PHP on Ubuntu 20.04.

Unlike Apache, Nginx does not contain native PHP processing. For that, we have to install PHP-FPM (FastCGI Process Manager). Run the following command to install PHP7.4 and some common extensions:

sudo apt install php7.4 php7.4-fpm php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline php7.4-mbstring php7.4-xml php7.4-gd php7.4-curl

Once installed, check the PHP version:

php --version

Next, configuring Nginx to work with PHP-FPM:

sudo nano /etc/nginx/conf.d/default.conf

Paste the following text into the file:

server {
  listen 80;
  listen [::]:80;
  server_name _;
  root /usr/share/nginx/html/;
  index index.php index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

 # A long browser cache lifetime can speed up repeat visits to your page
  location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
  }

  # disable access to hidden files
  location ~ /\.ht {
      access_log off;
      log_not_found off;
      deny all;
  }
}

Then, test the Nginx configuration syntax for correctness. If it is OK, restart the Nginx service to apply the new changes:

sudo nginx -t
sudo systemctl restart nginx

Step 5. Test PHP.

To test PHP, create a test file named info.php with the content below. Save the file, then browse to it to see if PHP is working:

sudo nano /usr/share/nginx/html/info.php

Copy the following into your text editor:

<?php
phpinfo();
?>

Try to access it at http://your_server_ip/info.php. If the PHP info page is rendered in your browser then everything looks good and you are ready to proceed further.

Congratulations! You have successfully installed LEMP stack. Thanks for using this tutorial for installing LAMP (Linux, Nginx, MariaDB and PHP) in Ubuntu 20.04 LTS Focal Fossa system. For additional help or useful information, we recommend you to check the official Nginx, MySQL and PHP web site.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!