This guide demonstrates how to install LEMP Stack (MariaDB) on Ubuntu 20.04 server which use’s Ngnix as a server, to use Apache as a server we need to Install LAMP Stack on Ubuntu version’s 20.04 / 18.04 / 16.04

Here is the detailed step by step process to install LEMP Stack on Ubuntu version 18.04

1. Installing Nginx Web Server

The Nginx web server is the most popular web servers in the world. It’s active community and detailed documentation eventually makes it a great choice for hosting a website.

Install Nginx using ( Advanced Package Tool ) apt:

Start off by updating your server’s package index:

sudo apt update
sudo apt install nginx

After it’s installed, we can enable Nginx to auto-start at boot time by running the following command.

sudo systemctl enable nginx

Then start Nginx with this command:

sudo systemctl start nginx

Now check out its status.

sudo systemctl status nginx

Output:

nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; <strong>enabled</strong>; vendor preset: enabled)
     Active: active (running) since Fri 2020-04-10 14:11:43 UTC; 3s ago
       Docs: man:nginx(8)
    Process: 8533 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 8545 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 8549 (nginx)
      Tasks: 3 (limit: 9451)
     <strong>Memory: 3.9M</strong>
     CGroup: /system.slice/nginx.service
             ├─8549 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─8550 nginx: worker process
             └─8551 nginx: worker process

Enabled” indicates that auto-start at boot time is enabled and we can see that Nginx is running. You can also see how much RAM Nginx is using from the output. If the above command doesn’t immediately quit after running. You need to press “q” to make it quit.

Check Nginx version.

nginx -v

Output:

nginx version: nginx/1.17.9 (Ubuntu)

Now installation of Nginx server is done. You can verify that by visiting your server’s public IP address in your web browser.

http://server_domain_or_IP
nginx_default page

In case connection is refused or failed to complete, there might be a firewall preventing incoming requests to TCP port 80. If you are using iptables firewall, then you need to run the following command to open TCP port 80.

sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT

If you are using UFW firewall, then run this command to open TCP port 80.

sudo ufw allow http

Finally, we need to make www-data (Nginx user) as the owner of web directory. By default, it’s owned by the root user.

sudo chown www-data:www-data /usr/share/nginx/html -R

2. Installing MariaDB

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. Enter the following command to install MariaDB on Ubuntu 20.04.

sudo apt install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb

Output:

● mariadb.service - MariaDB 10.3.22 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-04-10 14:19:16 UTC; 18s ago
       Docs: man:mysqld(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 9161 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 9451)
     Memory: 64.7M
     CGroup: /system.slice/mariadb.service
             └─9161 /usr/sbin/mysqld

If it’s not running, start it with this command:

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now run the post installation security script.

sudo mysql_secure_installation


Now configure the VALIDATE PASSWORD PLUGIN.

Note: Always use strong passwords for database credentials.

Type Y to enable, or any other key to continue without enabling and for rest of the question’s press “Y” enter.

Command photo copy for reference

Login as MariaDB root user.

sudo mariadb -u root

Now copy paste the below command, to login as root user.

use mysql;
update user set plugin='mysql_native_password' where user='root';
flush privileges;
quit;

Now run the command sudo mariadb -u root -p instead of sudo mariadb -u root

sudo mysql -u root -p

To exit, run

exit;

3. Installing PHP

PHP is a server side scripting language which helps your code to communicate MySQL database and get information to display it on a web page or to enter given information into database.

Install PHP using ( Advanced Package Tool ) apt:

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

PHP extensions are commonly needed for content management systems (CMS) like WordPress.

Installing these PHP extensions ensures that your CMS runs smoothly. Now start php7.4-fpm.

sudo systemctl start php7.4-fpm

Enable auto-start at boot time.

sudo systemctl enable php7.4-fpm

Check status:

systemctl status php7.4-fpm

4. Creating an Ngnix server block

An Nginx server block is like a virtual host in Apache. We will not use the default server block because it’s inadequate to run PHP code and if we modify it, it becomes a mess. So remove the default symlink in sites-enabled directory by running the following command. (It’s still available as /etc/nginx/sites-available/default.)

sudo rm /etc/nginx/sites-enabled/default

Then use a command-line text editor like Nano to create a brand new server block file under /etc/nginx/conf.d/ directory.

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

Paste the following text into the file. The following snippet will make Nginx listen on IPv4 port 80 and IPv6 port 80 with a catch-all server name.

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;
  }
}

Save and close the file. (To save a file in Nano text editor, press Ctrl+O, then press Enter to confirm. To exit, press Ctrl+X.)

Then test Nginx configurations.

sudo nginx -t

If the test is successful, reload Nginx.

sudo systemctl reload nginx

5. Testing PHP with Nginx

You can test this by creating a new file called info.php in the webroot directory.

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

Type or paste the following code into the new file. This is valid PHP code that will return information about your server:

<?php
phpinfo();
?>

When you are finished, save and close the file by typing CTRL+X and then y and ENTER to confirm.

You can now access this page in your web browser by visiting the domain name or public IP address you’ve set up in your Nginx configuration file, followed by /info.php:

http://server_domain_or_IP/info.php

You should see your server’s PHP information. This means PHP scripts can run properly with Nginx web server.

PHPInfo Ubuntu 20.04

After checking the relevant information about your PHP server through that page, it’s best to remove the file you created as it contains sensitive information about your PHP environment and your Ubuntu server. You can use rm to remove that file:

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

You can always regenerate this file if you need it later.

Conclusion:

Now that you have Installed LEMP STACK on Ubuntu 20.04, you can now host your website in the server. One of the popular thing you can do is install WordPress and design your own website or blog for your business.

To manage MySQL with GUI, we need to install phpMyAdmin. Operations such as the management of databases, tables, indexes, permissions, and so on are executed with the graphical user interface of phpMyAdmin.

Hope this tutorial is helpful and comment down if you have any query or issue.