How to Install LEMP Stack on Rocky Linux 8

Channel: Linux
Abstract: start the Nginx service. $ sudo systemctl start nginx To verify the status of the Nginx service$ sudo systemctl status nginx Now make sure to enable N

The term LEMP is an acronym for Linux, Nginx, MariaDB or MySQL and PHP. This group of software is used to develop and deploy web applications written in PHP.

For the LEMP stack - Ngnix serves as the webserver, MariaDB or MySQL provides a database for the website, PHP helps to serve PHP pages and Linux provides the Operating system.

Popular CMS such as WordPress, Joomla, Drupal uses the LEMP stack. This stack helps to host high traffic and scalable websites.

Best Way How to Install a LEMP Serv...

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

Best Way How to Install a LEMP Server on Rocky Linux 2022

In this tutorial, we learn how to install the LEMP stack on Rocky Linux 8.4. Also, learn to configure the Nginx server block to host your domain.

Step 1: Install Nginx

Nginx is a free, open-source, high-performance web server. LEMP stack uses Nginx to serve HTTP traffic.

First, update all the available packages on Rocky Linux.

$ sudo dnf upgrade

Note: dnf update is a deprecated alias.

Now, install Nginx on Rocky Linux.

$ sudo dnf install nginx 

Once Nginx is installed, start the Nginx service.

$ sudo systemctl start nginx

To verify the status of the Nginx service, type:

$ sudo systemctl status nginx

Now make sure to enable Nginx at the start at boot time.

$ sudo systemctl enable nginx

To check the Nginx version which is installed, type:

$ nginx -v

Output:

nginx version: nginx/1.14.1

The output shows we have installed Nginx 1.14.1 version.

To allow HTTP and HTTPS traffic (in case you want to encrypt the webserver with SSL ) on the firewall.

$ sudo firewall-cmd --permanent --zone=public --add-service=http
$ sudo firewall-cmd --permanent --zone=public --add-service=https

To make the changes effective, reload the firewall service:

$ sudo firewall-cmd --reload

You can verify by listing the current firewall settings:

$ sudo firewall-cmd --permanent --list-all

Open a browse and enter server IP or domain name to see the Nginx default page on Rocky Linux. On Rocky Linux, the default Nginx webpage is located in /usr/share/nginx/html.

Nginx default webpage on Rocky Linux Step 2: Install MariaDB

The MariaDB database is a fork of MySQL from Oracle. MariaDB shows improved performance over MySQL and offers better storage engines. Here we will install MariaDB to store data for your website.

To install MariaDB on Rocky Linux, type:

$ sudo dnf install mariadb-server mariadb

To start MariaDB service, type:

$ sudo systemctl start mariadb

Now verify MariaDB status.

$ sudo systemctl status mariadb

MariaDB should be now up and running. To enable MariaDB on system boot, type

$ sudo systemctl enable mariadb

Fresh MariaDB installation is not secure. MariaDB package comes with a script preinstalled which helps to improve the security of your database server.

To start the script, type:

$ sudo mysql_secure_installation
MariaDB set a new root password Secure MariaDB

The new installation on MariaDB has root password blank. This script allows to set a new root password, remove anonymous users, disallow root remote login, remove test database.

Now you can login MariaDB server using the root password which we set now.

$ sudo mysql -u root -p

Output:

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)]>

The output shows we have installed MariaDB 10.3 version and got a shell to manage database operations.

Step 3: Install PHP

PHP is mainly used to generate dynamic content for the webserver. Rocky Linux 8 AppStream comes with PHP version 7.2, 7.3, and 7.4 as of writing this tutorial. The default version is PHP 7.2. To install the latest version of PHP, then enable the free Remi repository.

To list the available PHP version:

$ sudo dnf module list php

Output:

Last metadata expiration check: 2:57:54 ago on Fri 06 Aug 2021 02:16:31 AM UTC.
 Rocky Linux 8 - AppStream
 Name                                      Stream                                      Profiles                                                       Summary
 php                                       7.2 [d]                                     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 enable PHP 7.4 module, type:

$ sudo dnf module enable php:7.4

Nginx requires PHP-FPM (PHP fastCGI process manager) which is an interpreter between PHP and webserver for better performance. Now we can install PHP, PHP-FPM, and the most common PHP extensions.

$ sudo dnf install php php-fpm php-cli php-gd php-curl php-zip php-mbstring php-opcache php-intl php-mysqlnd

To verify, check the PHP version:

$ php -v

Output:

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
     with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies

From the output, you can see it has installed the PHP 7.4 version.

Let's now check how to configure Nginx to execute PHP using PHP-FPM.

For this, we have to make a couple of changes in the PHP-FPM configuration in /etc/php-fpm.d/www.conf.

Open the PHP-FPM configuration file in your favorite editor.

$ sudo vi /etc/php-fpm.d/www.conf

Change the two variables user and group from apache to nginx.

user = nginx
group = nginx

Now start the PHP-FPM, type:

$ sudo systemctl start php-fpm

Enable PHP-FPM on system reboot, type:

$ sudo systemctl enable php-fpm

Now you can check its status, type:

$ sudo systemctl status php-fpm
PHP-FPM service status

Finally, restart the Ngnix to load the config changes made by php-fpm:

$ sudo systemctl restart nginx

Now we can verify PHP, extensions, FPM by adding info.php script into the default webroot directory.

$ sudo vi /usr/share/nginx/html/info.php

Add the below content to the info.php file:

<?php

phpinfo();

?>

Restart the Nginx to make the changes effective

$ sudo systemctl restart nginx

Open a browser and access domain name or server IP followed by info.php.

http://server-ip-or-domain-name.com/info.php
php.info page

It's recommended to delete the php.info page after verifying as you don't want the outside world to see your PHP information.

Now we are done with the LEMP stack installation on Rocky Linux 8. Let's also check how to configure the Nginx server block to host an example website.

Step 4: Configure Nginx Server Block in Rocky Linux

In Nginx, the server block defines the settings for a specific domain. Each website should have a document root that contains the website files. The document root can be created anywhere you want.

I am going to create the document root for a website named example.com in /var/www/example.com/public_html.

First create the document root directory, type:

$ sudo mkdir -p /var/www/example.com/public_html

Create an index.html file inside the document root and add the below basic HTML content.

<!DOCTYPE html> 
<html>  
 <head>    
 <meta charset="utf-8">   
  <title>Welcome</title> 
  </head>   
<body> 
<h1>Congrats! The example.com website is working</h1>  
</body>
 </html>

Change ownership of document root directory and all files inside to Nginx user ie www-data.

$ sudo chown -R nginx: /var/www/example.com

Now let's create the Nginx server block which is located by default at /etc/nginx/conf.d directory. Using your favorite editor create a server block file.

$ sudo vi /etc/nginx/conf.d/example.com.conf

Add the below content to the file created, replace example.com with your domain name.

server {
    listen 80;

    server_name example.com www.example.com;

    root /var/www/example.com/public_html;

    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}

Check nginx configuration for any syntax error:

$ sudo nginx -t

Output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To make the changes effective, restart the Nginx service:

$ sudo systemctl restart nginx

Open your browser and navigate to http://example.com to see the web page we created.

example.com test webpage Conclusion

In this tutorial, we learned how to install the LEMP stack on Rocky Linux 8. We also learned how to configure the Nginx server block for your domain.

Thanks for reading, please lets us know your suggestions in the below comment section.

Ref From: linoxide

Related articles