In today’s digital landscape, having a robust web server is crucial for businesses to establish an online presence. One popular combination of software components that can help achieve this is the LEMP stack. LEMP stands for Linux, Nginx, MySQL (or MariaDB), and PHP. In this article, we will guide you through the step-by-step process of installing and configuring a LEMP stack on Rocky Linux 8.

Prerequisites

Before we dive into the installation process, let’s make sure we have all the necessary prerequisites in place:

  • A server running Rocky Linux 8
  • Root access or a user with sudo privileges
  • Stable internet connection

Step 1: Installing Nginx Web Server

The first component we’ll install is the Nginx web server. Nginx is known for its high performance, scalability, and reliability. To install Nginx, open your terminal and run the following command:

sudo dnf install nginx -y

Once the installation is complete, start the Nginx web server and enable it to start automatically on system boot with the following commands:

sudo systemctl start nginx
sudo systemctl enable nginx

To verify the status of Nginx, run the following command:

sudo systemctl status nginx

You should see a message indicating that Nginx is active and running.

Step 2: Securing Firewall and Allowing HTTP/HTTPS Traffic

To make your web server accessible to the public, you need to configure your firewall to allow HTTP and HTTPS traffic. Run the following commands to achieve this:

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

This will open the necessary ports for web traffic, ensuring that your server can receive HTTP and HTTPS requests.

Step 3: Installing MariaDB Database Server

The next component of the LEMP stack is the database server. We will be using MariaDB, a popular and reliable open-source database management system. To install MariaDB, use the following command:

sudo dnf install mariadb-server mariadb -y

After the installation is complete, start the MariaDB service and enable it to automatically start on system boot by running the following commands:

sudo systemctl start mariadb
sudo systemctl enable mariadb

To check the status of the MariaDB service, use the following command:

sudo systemctl status mariadb

You should see a message indicating that MariaDB is active and running.

Step 4: Securing MariaDB Installation

It is essential to secure your MariaDB installation to protect your data. To do this, run the following command:

sudo mysql_secure_installation

This command will guide you through a series of prompts to set a root password, remove anonymous users, disallow remote root login, and more. Follow the prompts and answer accordingly to secure your MariaDB installation.

Step 5: Installing PHP and PHP-FPM

PHP is a server-side scripting language used to generate dynamic content for websites. PHP-FPM (PHP FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features. To install PHP and PHP-FPM, run the following command:

sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y

Once the installation is complete, start the PHP-FPM service and enable it to automatically start on system boot by running the following commands:

sudo systemctl start php-fpm
sudo systemctl enable php-fpm

To check the status of the PHP-FPM service, use the following command:

sudo systemctl status php-fpm

You should see a message indicating that PHP-FPM is active and running.

Step 6: Configuring PHP-FPM for Nginx

Since we are using Nginx as our web server, we need to make a few configuration changes in the PHP-FPM pool configuration file. Open the file using your preferred text editor:

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

Find the lines that specify the user and group, and change them to the following:

user = nginx
group = nginx

Save the file and exit the text editor. Then, reload the PHP-FPM service to apply the changes:

sudo systemctl reload php-fpm

Step 7: Testing PHP

To verify that PHP is working correctly with Nginx, we will create a simple PHP file and access it through a web browser. Create a new file called info.php in the Nginx web root directory:

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

Restart Nginx and PHP-FPM to apply the changes:

sudo systemctl restart nginx php-fpm

Now, open your web browser and access http://<your_server_ip>/info.php. You should see a page with detailed information about your PHP installation.

Conclusion

Congratulations! You have successfully installed and configured the LEMP stack (Linux, Nginx, MariaDB, and PHP) on Rocky Linux 8. With this powerful web server setup, you can now host and serve dynamic web applications with ease. Remember to regularly update and secure your server to ensure optimal performance and security.

If you need reliable and scalable cloud hosting services, look no further than Shape.host. With their Linux SSD VPS solutions, you can enjoy high-performance hosting with excellent support. Visit Shape.host to explore their hosting options and take your online presence to the next level.