Install LEMP Stack on Rocky Linux 8
LEMP stands for Linux, Nginx (Pronounced as Engine-X), MySQL/MariaDB, and PHP. This is a stack that is normally used together to build powerful websites and web applications.
Linux is the operating system where this stack runs. Nginx is a powerful webserver that powers the internet through serving web content, MySQL/MariaDB is the database server and PHP is the web scripting language that is used for the backend server-database communication in web application development.
This guide shall cover how to set up the LEMP stack on Rocky Linux 8. In our previous article, we covered how to install LAMP stack on Rocky Linux 8.
The main difference between LAMP and LEMP is the web server that is used. Whereas LAMP uses Apache, LEMP uses Nginx.
The steps below can be followed to install and configure LEMP stack on Rocky Linux 8.
Prerequisites
Before you can proceed with the steps to install LEMP stack, make sure your system has at least the following minimal requirements:
- 4GB RAM
- 4 vCPUs
- 50GB disk
- Normal User with sudo permissions
We have already installed Rocky Linux 8 on our Oracle Virtual Box environment.
Step 1: Install Nginx on Rocky Linux
Nginx is a lightweight and extremely fast web server. It is free and open source. Some well-known websites, such as GitHub, Netflix, and WordPress.com, use Nginx because it is stable and fast under high-traffic conditions while using few resources.
Before we can install Nginx, we need to make sure that all packages are updated.
$ sudo dnf update -y
Install Nginx package available from Rocky Linux Base repositories.
[root@rockylinux ~]# dnf install nginx
Rocky Linux 8 - AppStream 3.3 kB/s | 4.8 kB 00:01
Rocky Linux 8 - AppStream 1.7 MB/s | 8.7 MB 00:05
Rocky Linux 8 - BaseOS 4.7 kB/s | 4.3 kB 00:00
Rocky Linux 8 - BaseOS 691 kB/s | 5.6 MB 00:08
Rocky Linux 8 - Extras 1.6 kB/s | 3.5 kB 00:02
Rocky Linux 8 - Extras 2.7 kB/s | 4.3 kB 00:01
ELRepo.org Community Enterprise Linux Repository - el8 1.1 kB/s | 3.0 kB 00:02
ELRepo.org Community Enterprise Linux Repository - el8 121 kB/s | 272 kB 00:02
Extra Packages for Enterprise Linux Modular 8 - x86_64 59 kB/s | 61 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 33 kB/s | 61 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 2.3 MB/s | 10 MB 00:04
Dependencies resolved.
...
Total download size: 847 k
Installed size: 2.0 M
Is this ok [y/N]: y
Start and enable Nginx service.
$ sudo systemctl enable nginx $ sudo systemctl start nginx
Verify that the service is up and running:
$ sudo systemctl status nginx
Sample Output:
Step 2. Configure Firewall
Allow HTTP and HTTPS service through the firewall
$ sudo firewall-cmd --permanent --add-service={http,https} $ sudo firewall-cmd --reload
Visit the the server's web page on http://server-ip or http://FQDN
You can also confirm the version of Nginx installed with the command below:
[root@rockylinux ~]# nginx -v nginx version: nginx/1.14.1
Step 3. Install MariaDB database
LEMP stack can be run on either MariaDB or MySQL for the database server. This guide shall cover how to set up MariaDB on Rocky Linux 8. We have already covered a different tutorial to Install MariaDB on Rocky Linux 8.4 [Step-by-Step].
Download and install MariaDB server from the Rocky Linux repositories
[root@rockylinux ~]# dnf -y install @mariadb Rocky Linux 8 - AppStream 3.2 kB/s | 4.8 kB 00:01 Rocky Linux 8 - BaseOS 1.4 kB/s | 4.3 kB 00:03 Rocky Linux 8 - BaseOS 909 kB/s | 5.6 MB 00:06 Rocky Linux 8 - Extras 882 B/s | 3.5 kB 00:04 ELRepo.org Community Enterprise Linux Repository - el8 1.3 kB/s | 3.0 kB 00:02 Extra Packages for Enterprise Linux Modular 8 - x86_64 31 kB/s | 61 kB 00:01 Extra Packages for Enterprise Linux 8 - x86_64 36 kB/s | 53 kB 00:01 ... Installed: mariadb-backup-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-errmsg-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-gssapi-server-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-server-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 mariadb-server-utils-3:10.3.28-1.module+el8.4.0+427+adf35707.x86_64 perl-DBD-MySQL-4.046-3.module+el8.4.0+577+b8fe2d92.x86_64 perl-DBI-1.641-3.module+el8.4.0+509+59a8d9b3.x86_64 perl-Math-BigInt-1:1.9998.11-7.el8.noarch perl-Math-Complex-1.59-419.el8_4.1.noarch psmisc-23.1-5.el8.x86_64 Complete!
Start and enable MariaDB service
$ sudo systemctl enable mariadb $ sudo systemctl start mariadb
Step 4. Execute MySQL Hardening Script
Run the hardening script to harden your MariaDB server. You will be prompted for following input:
Enter current password for root (enter for none):
Change the root password? [Y/n] y
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
Sample Output:
[root@rockylinux ~]# 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! 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!
Connect to the database instance using the credentials for the root user set in the previous step.
[root@rockylinux ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.28-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Step 5. Install and Configure PHP
The next step is to install PHP and some of the necessary PHP extensions on Rocky Linux 8
By default, the Rocky Linux Appstream repo has PHP versions from PHP 7.2 to PHP 7.4. You can verify this by running the command below:
[root@rockylinux ~]# dnf module list php
Last metadata expiration check: 0:28:50 ago on Sun 22 Aug 2021 08:00:27 PM EAT.
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d][e] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
To install PHP on Rocky Linux, run the command below for select versions
# PHP 7.4 sudo dnf -y module install php:7.4 # PHP 7.3 sudo dnf -y module install php:7.3 # PHP 7.2 sudo dnf -y module install php:7.2
Sample Output:
[root@rockylinux ~]# sudo dnf -y module install php:7.4
Last metadata expiration check: 0:31:02 ago on Sun 22 Aug 2021 08:00:27 PM EAT.
Dependencies resolved.
...
Installed:
nginx-filesystem-1:1.14.1-9.module+el8.4.0+542+81547229.noarch oniguruma-6.8.2-2.el8.x86_64
php-cli-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-common-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
php-fpm-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-json-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
php-mbstring-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64 php-xml-7.4.6-4.module+el8.4.0+415+e936cba3.x86_64
Complete!
Verify the version of PHP installed to your system
[root@rockylinux ~]# php -v
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
Step 6. Install PHP Modules
PHP itself contains several features that can be individually installed on the Linux system. PHP is generally distributed for Linux in a number of packages, allowing you some choice over what is installed on your server.
Default packages often have no database-specific extensions installed, so they would not be able to immediately communicate with a PostgreSQL or MySQL or MariaDB database. You must install the PHP MySQL module to enable your PHP installation to communicate with a MariaDB database
Install PHP modules.
$ sudo dnf -y install php php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-zip
Step 7. Configure PHP-FPM
Configure PHP-FPM to change the user from Apache to Nginx
Edit the file /etc/php-fpm.d/www.conf
to change the user and group.
$ sudo vim /etc/php-fpm.d/www.conf
Find the lines below for the user and group and change them to Nginx.
user = nginx Group = nginx
Restart PHP-FPM service
$ sudo systemctl reload php-fpm
Step 8. Verify that LEMP Stack is working
Create a test file under /usr/share/nginx/html/info.php
and add the content below:
<?php phpinfo(); ?>
HINT:
<?php
is the preferred method of entering PHP mode because it is guaranteed to work. When in PHP mode, you can exit it by using ?>
(for <?
php and <?
); %>
(for <%
)Restart Nginx service
$ sudo systemctl restart nginx
Accessing this file through a web browser should show a page containing information about PHP.
Open the LEMP stack server on the web browser at http://server-ip/info.php
This verifies that our LEMP stack installation is complete and every item has been successfully installed.
Conclusion
In this tutorial we discussed about the the popular LEMP stack and implementing Nginx, MariaDB, and PHP. Nginx is quite simple to configure but provides faster access to web content than Apache, but can equally be configured to communicate with PHP and MariaDB in the backend. Gaining the basic knowledge of configuring PHP and MariaDB to operate with the web server can build the grounding you need to develop your web application further.
That's it for setting up LEMP stack on Rocky Linux 8.4. You can now start building your web applications and websites using this stack. You can also secure your server using SSL if you have a fully qualified domain name, FQDN.