LinuxTutorialsUbuntu

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 that 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 the step-by-step installation LEMP stack on an Ubuntu 20.04 (Focal Fossa) server.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

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 the 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 is 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 the 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 the 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 the 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 the 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 websites.

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!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button