Installing LEMP Stack on Ubuntu 21.04

Hello,

In this week’s feature highlight, we look at How to Install LEMP Stack on Ubuntu 21.04

Updating the system

We first update the system to make sure that all our installed packages are upto date. Your Ubuntu system can be updated easily with the following command.

sudo apt update

sudo apt upgrade

Install Nginx

We will start by installing the Nginx web server. To complete the installation, use the following command.

apt install nginx

Once the installation is complete, enable Nginx (to start automatically upon system boot), start the web server and verify the status using the commands below.

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

user@server:~$ systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
 Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
 Active: active (running) since Wed 2021-04-28 20:28:28 UTC; 21s ago
   Docs: man:nginx(8)
Main PID: 25815 (nginx)
  Tasks: 3 (limit: 2248)
 Memory: 5.4M
 CGroup: /system.slice/nginx.service
         ├─25815 nginx: master process /usr/sbin/nginx -g daemon on; master>
         ├─25816 nginx: worker process
         └─25817 nginx: worker process

To check Nginx version.

nginx -v

Output:

user@server:~$ nginx -v
nginx version: nginx/1.18.0 (Ubuntu)

Verify that the web server is running and accessible by accessing your server’s IP address.

From your browser,

http://IP_address
image

we need to make user nginx as the owner of web directory. By default it’s owned by the root user.

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

Install MariaDB Server

MariaDB is a popular database server. The installation is simple and requires just a few steps as shown.

sudo apt install mariadb-server mariadb-client

Output:

user@server:~$ sudo apt install mariadb-server mariadb-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfile-find-rule-perl libnumber-compare-perl libtext-glob-perl
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl
  libconfig-inifiles-perl libdbd-mariadb-perl libdbi-perl
  libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl
  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl

Once the installation is complete, enable MariaDB (to start automatically upon system boot), start the mariaDB and verify the status using the commands below.

systemctl start mariadb

systemctl enable mariadb

systemctl status mariadb

Output:

user@server:~$ systemctl status mariadb
● mariadb.service - MariaDB 10.5.9 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor prese>
     Active: active (running) since Wed 2021-04-28 20:31:14 UTC; 30s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 27439 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 14 (limit: 2248)
     Memory: 69.1M
     CGroup: /system.slice/mariadb.service
             └─27439 /usr/sbin/mariadbd

Finally, you will want to secure your MariaDB installation by issuing the following command.

sudo mysql_secure_installation

Output:

user@server:~$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Once secured, you can connect to MySQL and review the existing databases on your database server by using the following command.

sudo mysql -e "SHOW DATABASES;" -p

Output:

user@server:~$ sudo mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

Install PHP 7.4

To Install PHP 7.4, use the command.

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

To start php7.4-fpm.

systemctl start php7.4-fpm

To auto-start on boot php7.4-fpm.

systemctl enable php7.4-fpm

To Check status.

systemctl status php7.4-fpm

Output:

user@server:~$ sudo systemctl status php7.4-fpm
● php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor >
     Active: active (running) since Wed 2021-04-28 20:44:08 UTC; 44s ago
       Docs: man:php-fpm7.4(8)
   Main PID: 38209 (php-fpm7.4)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0r>
      Tasks: 3 (limit: 2248)
     Memory: 9.3M
     CGroup: /system.slice/php7.4-fpm.service
             ├─38209 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
             ├─38210 php-fpm: pool www
             └─38211 php-fpm: pool www

Setting Up Server Blocks

You need to remove the file default, located in /etc/nginx/sites-enabled.

rm /etc/nginx/sites-enabled/default

Create new server block file under /etc/nginx/conf.d/ directory.

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

Add following text into the file.

  server {
  listen 80;
  listen [::]:80;
  server_name _;
  root /var/www/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;
  }
}

Next, test to make sure that there are no syntax errors in any of your Nginx files.

nginx -t

If there aren’t any problems, restart Nginx to enable your changes.

systemctl reload nginx

Test PHP

To test PHP-FPM with Nginx Web server, we need to create a info.php file in the webroot directory.

nano /var/www/html/info.php

Add the following PHP code into the file.

<?php phpinfo(); ?>

Now again, access http://localhost/info.php or http://yourserver-ip-address/info.php. You should see a page similar to below one.

image

Done!

(Visited 374 times, 1 visits today)

Leave a Reply

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