How to Install LEMP Stack (Nginx, MariaDB, PHP8.1) on Ubuntu 23.04

LEMP Stack is a combination of free, open source software. The acronym LEMP refers to the first letters of Linux (Operating system), Nginx Server, MySQL (database software), and PHP, PERL or Python, principal components to build a viable general purpose web server.

Updating the system

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

apt update

apt upgrade

Install Nginx

Start by installation of the Nginx web server. to complete the installation, use the below commands.

apt install nginx

Output:

root@localhost:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
nginx is already the newest version (1.22.0-1ubuntu3).

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

systemctl start nginx

systemctl enable nginx

systemctl status nginx

Output:

 root@localhost:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enable>
     Active: active (running) since Thu 2023-04-13 19:05:38 UTC; 33min ago
       Docs: man:nginx(8)
    Process: 1895 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; ->
   Main PID: 604 (nginx)
      Tasks: 5 (limit: 3386)
     Memory: 13.6M
        CPU: 210ms
     CGroup: /system.slice/nginx.service
             ├─ 604 "nginx: master process /usr/sbin/nginx -g daemon on; master>
             ├─1896 "nginx: worker process"
             ├─1897 "nginx: worker process"
             ├─1898 "nginx: worker process"
             └─1899 "nginx: worker process"

Check the Nginx version,

nginx -v

Output:

root@localhost:~# nginx -v
nginx version: nginx/1.22.0 (Ubuntu)

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

From your browser,

http://IP_address

image

we need to make the user Nginx the owner of the 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.

apt install mariadb-server mariadb-client

Output:

root@crown:~# apt install mariadb-server mariadb-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  galera-4 libcgi-fast-perl libcgi-pm-perl libclone-perl
  libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl libdbi-perl
  libencode-locale-perl libfcgi-bin libfcgi-perl libfcgi0ldbl
  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl
  libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libmariadb3 libmysqlclient21 libndctl6 libpmem1
  libsnappy1v5 libtimedate-perl liburi-perl liburing2 mariadb-client-10.6
  mariadb-client-core-10.6 mariadb-common mariadb-server-10.6
  mariadb-server-core-10.6 mysql-common socat

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:

root@localhost:~# systemctl status mariadb
● mariadb.service - MariaDB 10.11.2 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; preset: enab>
     Active: active (running) since Thu 2023-04-13 19:05:39 UTC; 6min ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 749 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 8 (limit: 3386)
     Memory: 110.5M
        CPU: 834ms
     CGroup: /system.slice/mariadb.service
             └─749 /usr/sbin/mariadbd

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

mysql_secure_installation

Output:

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):
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:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

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

mysql -e "SHOW DATABASES;" -p

Output:

root@vps:~# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+

Install PHP 8.1

Install PHP 8.1 using below command,

apt install php php-fpm php-mysql php-common php-cli php-common php-json php-opcache php-readline php-mbstring php-xml php-gd php-curl

Start and enable php8.1-fpm

systemctl start php8.1-fpm

systemctl enable php8.1-fpm

Check status of php8.1-fpm

systemctl status php8.1-fpm

Output:

 root@crown:~# 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 pr>
     Active: active (running) since Thu 2022-03-17 14:12:02 UTC; 57min ago
       Docs: man:php-fpm8.1(8)
   Main PID: 41457 (php-fpm8.1)
     Status: "Processes active: 0, idle: 2, Requests: 1, slow: 0, Traffic: 0req>
      Tasks: 3 (limit: 1074)
     Memory: 11.6M
        CPU: 359ms
     CGroup: /system.slice/php8.1-fpm.service
             ├─41457 "php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)" >
             ├─41458 "php-fpm: pool www" "" "" "" "" "" "" "" "" "" "" "" "" "">
             └─41459 "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 the following text to 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/php8.1-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-FPM with the Nginx Web server, we need to create an info.php file in the webroot directory.

nano /var/www/html/info.php

Add the following PHP code to the file.

<?php phpinfo(); ?>

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

image

This concludes the installation of LEMP Stack on Ubuntu 23.04 server.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS