HomeHOWTOSHow To Install LEMP Stack on Ubuntu 22.04|20.04

How To Install LEMP Stack on Ubuntu 22.04|20.04

Welcome to our guide on how to install LEMP stack on Ubuntu 22.04|20.04. LEMP Stack is a group of open-source tools commonly used for developing and deploying web applications.

How Does the LEMP Stack Function?

Nginx serves as the LEMP stack’s web server, listening for HTTP requests and routing them to the relevant PHP scripts. The user receives a response from the PHP script via Nginx after it has been generated.

The website’s data is stored and managed using MySQL. In order to obtain and store data as needed, PHP talks with MySQL.

The LEMP stack is a collection of four open-source web development technologies.
Among these technologies are:

  • Linux: The operating system that powers the web server.
  • Nginx: The web server software that handles HTTP requests.
  • MySQL: The relational database management system that stores the website’s data.
  • PHP: is a programming language used to create dynamic web applications.

Why is the LEMP Stack so well-liked among web developers?

There are various reasons why the LEMP stack is common in web development :

  • High performance: Nginx is renowned for being extremely fast, which makes it a great option for handling heavy traffic.
  • Scalability: The LEMP stack is very scalable, making it a fantastic option for websites that need to manage a lot of traffic.
  • Open-source: The LEMP stack’s entirety is open-source, which saves web developers money.
  • Flexibility: The LEMP stack is adaptable and may be tailored to meet the unique requirements of a website.
  • Security: The LEMP stack is renowned for its security, with Nginx offering a number of security features like SSL encryption and DDoS prevention.

Install LEMP Stack on Ubuntu 22.04|20.04

Follow the below steps for successful installation of the LAMP Stack on Ubuntu 2204|20.04.

1. Update the system

First, perform a system update.

sudo apt update
sudo apt upgrade
sudo reboot

2. Install Nginx Web Server on Ubuntu 22.04|20.04

Nginx is included in the default Ubuntu 22.04 repositories. It is simple to install by running the command below;

sudo apt install nginx

Sample results:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip2 nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libnginx-mod-stream-geoip2 nginx nginx-common nginx-core
0 upgraded, 9 newly installed, 0 to remove and 38 not upgraded.
Need to get 696 kB of archives.
After this operation, 2,395 kB of additional disk space will be used.
Do you want to continue? [Y/n] y

After installing it, we can enable Nginx to start automatically at boot time by running the following command.

sudo systemctl enable nginx

Run the following command to launch Nginx:

sudo systemctl start nginx

Check its current status.

$ 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: enabled)
     Active: active (running) since Mon 2022-10-24 17:58:57 EAT; 2min 9s ago
       Docs: man:nginx(8)
   Main PID: 4529 (nginx)
      Tasks: 3 (limit: 4626)
     Memory: 3.9M
        CPU: 48ms
     CGroup: /system.slice/nginx.service
             ├─4529 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ├─4532 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
             └─4533 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Okt 24 17:58:57 ezrah-VirtualBox systemd[1]: Starting A high performance web server and a reverse proxy server...
Okt 24 17:58:57 ezrah-VirtualBox systemd[1]: Started A high performance web server and a reverse proxy server.

We can see that Nginx is operating and that auto-start at boot time is enabled, as Enabledsuggests. The result also shows how much RAM Nginx is utilizing. If the aforementioned command doesn’t end up running promptly. In order to end it, you must hit q.

Verify the Nginx version.

$ nginx -V
nginx version: nginx/1.18.0 (Ubuntu)

To open TCP port 80 if you are using a UFW firewall, do this command.

sudo ufw allow http

Use the URL http://Server.IP or http://domain_name to check that Nginx is accessible from a browser.
You should arrive at the Nginx HTTP server test page, which essentially verifies that the Nginx web server has been set up and is operational.

Install LEMP Stack on Ubuntu

3. Install MySQL/MariaDB Database Server on Ubuntu 22.04|20.04

MySQL may be easily replaced by MariaDB. It is being created by former MySQL team members who fear Oracle may convert MySQL into a closed-source product.

In order to install MariaDB on Ubuntu 22.04|20.04, do the following command:

sudo apt install mariadb-server mariadb-client

Sample results:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  galera-4 gawk libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl libdbi-perl libfcgi-bin
  libfcgi-perl libfcgi0ldbl libhtml-template-perl libmariadb3 libmysqlclient21 libndctl6 libpmem1 libsigsegv2
  libterm-readkey-perl liburing2 mariadb-client-10.6 mariadb-client-core-10.6 mariadb-common mariadb-server-10.6
  mariadb-server-core-10.6 mysql-common
Suggested packages:
  gawk-doc libmldbm-perl libnet-daemon-perl libsql-statement-perl libipc-sharedcache-perl mailx mariadb-test
The following NEW packages will be installed:
  galera-4 gawk libcgi-fast-perl libcgi-pm-perl libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl libdbi-perl libfcgi-bin
  libfcgi-perl libfcgi0ldbl libhtml-template-perl libmariadb3 libmysqlclient21 libndctl6 libpmem1 libsigsegv2
  libterm-readkey-perl liburing2 mariadb-client mariadb-client-10.6 mariadb-client-core-10.6 mariadb-common mariadb-server
  mariadb-server-10.6 mariadb-server-core-10.6 mysql-common
0 upgraded, 27 newly installed, 0 to remove and 38 not upgraded.
Need to get 18.2 MB of archives.
After this operation, 163 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

MariaDB server should launch instantly after installation. To determine its state, use systemctl.

$ systemctl status mariadb
 mariadb.service - MariaDB 10.6.7 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-10-24 18:36:59 EAT; 31s ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
    Process: 7219 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
    Process: 7220 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 7222 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= ||   VAR=`cd /usr/bin/..; /usr/bin/galera_>
    Process: 7264 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
    Process: 7266 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
   Main PID: 7251 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 13 (limit: 4626)
     Memory: 57.1M
        CPU: 658ms
     CGroup: /system.slice/mariadb.service
             └─7251 /usr/sbin/mariadbd

Examine the MariaDB server version information.

$ mariadb --version
mariadb  Ver 15.1 Distrib 10.6.7-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

Use this command to start MariaDB if it isn’t already running;

sudo systemctl start mariadb

Enable MariaDB database;

sudo systemctl enable mariadb

Execute the post-installation security script now;

sudo mysql_secure_installation
  • When prompted to enter the MariaDB root password, press Enter because the root password has not yet been set.
  • Switching to Unix socket authentication is unnecessary because MariaDB already uses it.
  • You do not need to change the root password when using Unix socket authentication, so do not change it.
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                                                                                                                                                                           you haven't set the root password yet, the password will be blank,                                                                                                                                                                           so you should just press enter here.
                                                                                                                                                                                                                                                                                                                                                                                                                                                      Enter current password for root (enter for none): <PRESS ENTER>                                                                                                                                                                                           OK, successfully used password, moving on...                                                                                                                                                                                                                                                                                                                                                                                                                                              

Setting the root password ensures that nobody can log into the MariaDB                                                                                                                                                                       root user without the proper authorisation.                                                                                                                                                                                                                                                                                                                                                                                                                                               

Set root password? [Y/n] y                                                                                                                                                                                                                   New password: <NEW PASSWORD>                                                                                                                                                                                                                                Re-enter new password: <RE-NEW PASSWORD>                                                                                                                                                                                                                       Password updated successfully!                                                                                                                                                                                                               Reloading privilege tables..                                                                                                                                                                                                                  ... Success!                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          

By default, a MariaDB installation has an anonymous user, allowing anyone                                                                                                                                                                    to log into MariaDB without having to have a user account created for                                                                                                                                                                        them.  This is intended only for testing, and to make the installation                                                                                                                                                                       go a bit smoother.  You should remove them before moving into a                                                                                                                                                                              production environment.                                                                                                                                                                                                                                                                                                                                                                                                                                                                   Remove anonymous users? [Y/n] y                                                                                                                                                                                                               ... Success!                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

Normally, root should only be allowed to connect from 'localhost'.  This                                                                                                                                                                     ensures that someone cannot guess at the root password from the network.                                                                                                                                                                                                                                                                                                                                                                                                                  Disallow root login remotely? [Y/n] y                                                                                                                                                                                                         ... Success!                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

By default, MariaDB comes with a database named 'test' that anyone can                                                                                                                                                                       access.  This is also intended only for testing, and should be removed                                                                                                                                                                       before moving into a production environment.                                                                                                                                                                                                                                                                                                                                                                                                                                              Remove test database and access to it? [Y/n] y                                                                                                                                                                                                - Dropping test database...                                                                                                                                                                                                                  ... Success!                                                                                                                                                                                                                                 - Removing privileges on test database...                                                                                                                                                                                                    ... Success!                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

Reloading the privilege tables will ensure that all changes made so far                                                                                                                                                                      will take effect immediately.                                                                                                                                                                                                                                                                                                                                                                                                                                                             Reload privilege tables now? [Y/n] y                                                                                                                                                                                                          ... Success!                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

Cleaning up...                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

All done!  If you've completed all of the above steps, your MariaDB                                                                                                                                                                          installation should now be secure.                                                                                                                                                                                                                                                                                                                                                                                                                                                        

Thanks for using MariaDB! 

By default, the MariaDB package on Ubuntu uses unix socket to authenticate user login, which means you can log into the MariaDB console using the OS username and password.
To log in without providing the MariaDB root password, use the following command.

mariadb -u root -p

Check the available databases;

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
MariaDB [(none)]> QUIT
Bye

4. Install PHP on Ubuntu 22.04|20.04

Then, on Ubuntu 22.04, install PHP. By default, Ubuntu 22.04 includes PHP 8.1 in its default repositories.
As a result, installing PHP 8.1 is as simple as running the command below:

sudo apt install php php-fpm php-mysql

Sample results:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.1 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
  libaprutil1-ldap php-common php8.1 php8.1-cli php8.1-common php8.1-fpm php8.1-mysql php8.1-opcache php8.1-readline
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php8.1 libapr1 libaprutil1 libaprutil1-dbd-sqlite3
  libaprutil1-ldap php php-common php-fpm php-mysql php8.1 php8.1-cli php8.1-common php8.1-fpm php8.1-mysql php8.1-opcache
  php8.1-readline
0 upgraded, 20 newly installed, 0 to remove and 38 not upgraded.
Need to get 8,800 kB/9,020 kB of archives.
After this operation, 35.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y         

This will install other default PHP modules alongside it.

  • If you need to install any additional modules, simply run the command apt install php-extension, where extension is the name of the PHP module. As an example,
sudo  apt install php-{bcmath,bz2,imap,intl,mbstring,soap,sybase,xml,zip}

Check the installed PHP version:

$ php -v
PHP 8.1.2-1ubuntu2.11 (cli) (built: Feb 22 2023 22:56:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2-1ubuntu2.11, Copyright (c), by Zend Technologies

Ensure that PHP-FPM is started and enabled.

sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm

Check the status:

$ systemctl status php8.1-fpm
 php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-07 21:59:16 EAT; 2min 27s ago
       Docs: man:php-fpm8.1(8)
   Main PID: 108579 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
      Tasks: 3 (limit: 4629)
     Memory: 10.4M
        CPU: 49ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─108579 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)"

5. Configure Nginx Server Block for PHP on Ubuntu 22.04|20.04

The Nginx server block can be configured easily on Ubuntu 22.04|20.04 at /etc/nginx/conf.d/ directory.

Use a command-line text editor such as vi to create a new server block file.

sudo vim /etc/nginx/conf.d/test.conf

In this guide, we will enable PHP(fastcgi_pass and the php8.1-fpm.sock). So the file will contain the below lines:

server {
  # Example PHP Nginx FPM config file
  listen 80;
  root /var/www/html;

  # Add index.php to setup Nginx, PHP & PHP-FPM config
  index index.php index.html index.htm index.nginx-debian.html;

  server_name example.tutornix.com www.example.tutornix.com;

  location / {
    try_files $uri $uri/ =404;
  }

  # pass PHP scripts on Nginx to FastCGI (PHP-FPM) server
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;

    # Nginx php-fpm sock config:
    fastcgi_pass unix:/run/php/php8.1-fpm.sock;

    # Nginx php-cgi config :
    # Nginx PHP fastcgi_pass 127.0.0.1:9000;
  }

  # deny access to Apache .htaccess on Nginx with PHP, 
  # if Apache and Nginx document roots concur
  location ~ /\.ht {
    deny all;
  }
} 

Then, validate the Nginx config:

$ sudo nginx -t                                                                                         
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reload Nginx if the test is successful.

sudo systemctl reload nginx

To test PHP-FPM with Nginx, we must first create an info.php file in the webroot directory.

sudo vim /var/www/html/info.php

Copy and paste the PHP code below into the file.

<?php
phpinfo();
?>

Save the file and try accessing the PHP file using the URL http://domain_name/info.php

How To Install LEMP Stack on Ubuntu 22.04|20.04

And there you have it. Ubuntu 22.04|20.04 has your LEMP stack installed. Ensure that all the PHP modules required for the specific application you want to deploy are present.

You can now remove the PHP info file once you’ve confirmed Nginx PHP processing;

sudo rm -rf /var/www/html/info.php

Conclusion

This concludes our tutorial on how to install LEMP Stack on Ubuntu 22.04|20.04. Hope you find this guide informative.

You can check more on PHP, MariaDB, and Nginx.

Other cool guides:

- Advertisment -

Recent posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here