Installing LEMP Stack on Rocky Linux 9

Hello,

In this week’s feature highlight, we look at How to Install LEMP Stack on Rocky Linux 9

LEMP 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.

Update system

First, we will update the system to the latest with the following commands,

dnf update

Install Nginx Web Server

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

dnf install nginx -y

Output:

[root@server ~]# dnf install nginx -y
Last metadata expiration check: 0:12:50 ago on Sat 16 Jul 2022 10:32:06 PM CEST.
Dependencies resolved.
================================================================================
 Package                Arch        Version                Repository      Size
================================================================================
Installing:
 nginx                  x86_64      1:1.20.1-10.el9        appstream      594 k
Installing dependencies:
 nginx-filesystem       noarch      1:1.20.1-10.el9        appstream       11 k
 rocky-logos-httpd      noarch      90.11-1.el9            appstream       24 k

Transaction Summary
================================================================================
Install  3 Packages

Total download size: 629 k
Installed size: 1.8 M
Downloading Packages:
(1/3): nginx-filesystem-1.20.1-10.el9.noarch.rp  34 kB/s |  11 kB     00:00
(2/3): rocky-logos-httpd-90.11-1.el9.noarch.rpm  60 kB/s |  24 kB     00:00

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:

[root@server ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor pre>
     Active: active (running) since Sat 2022-07-16 22:46:08 CEST; 269ms ago
   Main PID: 1309 (nginx)
      Tasks: 3 (limit: 11120)
     Memory: 2.8M
        CPU: 36ms
     CGroup: /system.slice/nginx.service
             ├─1309 "nginx: master process /usr/sbin/nginx"
             ├─1310 "nginx: worker process"
             └─1311 "nginx: worker process"

Check Nginx version

nginx -v

Output:

[root@server ~]# nginx -v
nginx version: nginx/1.20.1

To make your pages available to the public, you will have to edit your firewall rules to allow HTTP requests on your web server by using the following commands.

firewall-cmd --permanent --zone=public --add-service=http 

firewall-cmd --permanent --zone=public --add-service=https

firewall-cmd --reload

Output:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-service=https
success
[root@server ~]# firewall-cmd --reload
success

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 the user Nginx the owner of the web directory. By default, it’s owned by the root user.

chown nginx:nginx /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.

yum install mariadb-server mariadb -y

Output:

[root@server ~]# dnf install mariadb-server mariadb -y
Last metadata expiration check: 0:16:13 ago on Sat 16 Jul 2022 10:32:06 PM CEST.
Dependencies resolved.
================================================================================
 Package                       Arch    Version                 Repository  Size
================================================================================
Installing:
 mariadb                       x86_64  3:10.5.13-2.el9         appstream  1.6 M
 mariadb-server                x86_64  3:10.5.13-2.el9         appstream  9.3 M
Installing dependencies:
 checkpolicy                   x86_64  3.3-1.el9               appstream  339 k
 libaio                        x86_64  0.3.111-13.el9          baseos      23 k
 mariadb-common                x86_64  3:10.5.13-2.el9         appstream   32 k
 mariadb-connector-c           x86_64  3.2.6-1.el9_0           appstream  195 k
 mariadb-connector-c-config    noarch  3.2.6-1.el9_0           appstream  9.8 k
 mariadb-errmsg                x86_64  3:10.5.13-2.el9         appstream  188 k
 mysql-selinux                 noarch  1.0.4-2.el9             appstream   35 k
 perl-AutoLoader               noarch  5.74-479.el9            appstream   30 k
 perl-B                        x86_64  1.80-479.el9            appstream  188 k
 perl-Carp                     noarch  1.50-460.el9            appstream   29 k

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@server ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.5 database server
     Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor p>
     Active: active (running) since Sat 2022-07-16 22:50:32 CEST; 285ms ago
       Docs: man:mariadbd(8)
             https://mariadb.com/kb/en/library/systemd/
   Main PID: 3649 (mariadbd)
     Status: "Taking your SQL requests now..."
      Tasks: 15 (limit: 11120)
     Memory: 73.0M
        CPU: 599ms
     CGroup: /system.slice/mariadb.service
             └─3649 /usr/libexec/mariadbd --basedir=/usr

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

mysql_secure_installation

Output:

[root@server ~]# 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
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@server ~]# mysql -e "SHOW DATABASES;" -p
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
[root@server ~]#

Install PHP

To Install PHP-FPM by running the following command.

yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y

Output:

[root@server ~]# yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
Last metadata expiration check: 0:01:32 ago on Sat 16 Jul 2022 10:50:35 PM CEST.
Dependencies resolved.
================================================================================
 Package               Arch        Version                 Repository      Size
================================================================================
Installing:
 php                   x86_64      8.0.13-1.el9            appstream       14 k
 php-fpm               x86_64      8.0.13-1.el9            appstream      1.6 M
 php-gd                x86_64      8.0.13-1.el9            appstream       45 k
 php-mbstring          x86_64      8.0.13-1.el9            appstream      474 k
 php-mysqlnd           x86_64      8.0.13-1.el9            appstream      154 k
 php-opcache           x86_64      8.0.13-1.el9            appstream      511 k
 php-xml               x86_64      8.0.13-1.el9            appstream      136 k

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

systemctl start php-fpm

systemctl enable php-fpm

systemctl status php-fpm

Output:

[root@server ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor p>
     Active: active (running) since Sat 2022-07-16 22:55:32 CEST; 297ms ago
   Main PID: 4472 (php-fpm)
     Status: "Ready to handle connections"
      Tasks: 6 (limit: 11120)
     Memory: 12.5M
        CPU: 95ms
     CGroup: /system.slice/php-fpm.service
             ├─4472 "php-fpm: master process (/etc/php-fpm.conf)"
             ├─4473 "php-fpm: pool www"
             ├─4474 "php-fpm: pool www"
             ├─4475 "php-fpm: pool www"
             ├─4476 "php-fpm: pool www"
             └─4477 "php-fpm: pool www"

By default, PHP-FPM runs as the apache user. Since we are using the Nginx web server, we need to change the following line.

Using your favorite editor, edit the file /etc/php-fpm.d/www.conf.

nano /etc/php-fpm.d/www.conf

Find the below lines,

user = apache
group = apache

Change them into

user = nginx
group = nginx

Once changed, you will need to reload php-fpm,

systemctl reload php-fpm

Test your PHP, by creating a simple info.php file with a phpinfo() in it. The file should be placed in the root directory for your web server, which is /usr/share/nginx/html/.

To create the file use:

echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php

Restart the Nginx and PHP-FPM.

systemctl restart nginx php-fpm

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

image

Done!

(Visited 159 times, 1 visits today)