In today’s digital landscape, having a robust and efficient web server is crucial for businesses. A LEMP stack, which includes Linux, Nginx, MariaDB, and PHP, is a powerful combination of open-source software that can help you build a high-performance web server. In this article, we will guide you through the process of setting up a LEMP stack on AlmaLinux 8, ensuring that your web server is up and running smoothly.
1. Introduction
Before we dive into the installation process, let’s briefly understand what each component of the LEMP stack represents:
- Linux: AlmaLinux 8 is a Linux distribution that serves as the foundation for the LEMP stack. It provides the operating system on which the other components will be installed.
- Nginx: Nginx is a lightweight and high-performance web server that will handle incoming HTTP requests and serve static content efficiently.
- MariaDB: MariaDB is a popular open-source relational database management system. It will store and manage your website’s data.
- PHP: PHP is a server-side scripting language that will enable dynamic content generation and interact with the MariaDB database.
Now, let’s get started with the installation process.
2. Installing Nginx Web Server
The first step is to install the Nginx web server. To do this, open your terminal and run the following command:
yum install nginx -y
Once the installation is complete, you can start and enable Nginx to ensure it starts automatically upon system boot. Use the following commands:
systemctl start nginx systemctl enable nginx
To verify that Nginx is running, you can check its status:
systemctl status nginx
3. Verifying the Nginx Installation
After installing Nginx, it’s essential to verify its installation. You can do this by checking the Nginx version using the following command:
nginx -v
Make sure the output displays the version number, confirming the successful installation.
4. Configuring Firewall Rules
To make your web server accessible to the public, you need to allow HTTP and HTTPS requests through your firewall. AlmaLinux 8 uses firewalld as the default firewall management tool. Run the following commands to add the necessary firewall rules:
firewall-cmd --permanent --zone=public --add-service=http firewall-cmd --permanent --zone=public --add-service=https firewall-cmd --reload
These commands will enable the HTTP and HTTPS services on your firewall, ensuring that your web server can receive incoming requests.
5. Granting Ownership to Nginx
By default, the web directory is owned by the root user. To ensure that Nginx has the necessary permissions to serve your web pages, you need to change the ownership to the Nginx user. Run the following command to grant ownership:
chown nginx:nginx /usr/share/nginx/html -R
This command recursively changes the ownership of the web directory to the Nginx user and group.
6. Installing MariaDB Server
Next, let’s install the MariaDB server, which will handle the storage and management of your website’s data. Run the following command to install MariaDB:
yum install mariadb-server mariadb -y
Once the installation is complete, start and enable MariaDB using the following commands:
systemctl start mariadb systemctl enable mariadb
To verify that MariaDB is running, check its status:
systemctl status mariadb
7. Securing MariaDB Installation
Securing your MariaDB installation is essential to protect your data. The mysql_secure_installation
command will guide you through the process of securing MariaDB. Run the following command to begin:
mysql_secure_installation
Follow the prompts to set a root password, remove anonymous users, disallow remote root login, remove the test database, and reload privilege tables. This process will enhance the security of your MariaDB installation.
8. Testing the MariaDB Installation
After securing MariaDB, you can connect to the database server and verify that it is functioning correctly. Use the following command to list the existing databases:
mysql -e "SHOW DATABASES;"
Enter your root password when prompted, and you should see a list of databases, including the information_schema
, mysql
, and performance_schema
databases.
9. Installing PHP
PHP is a crucial component of the LEMP stack, enabling dynamic content generation and interaction with the MariaDB database. To install PHP and its necessary modules, run the following command:
yum install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring php-json -y
This command will install PHP and modules required for common web applications.
10. Configuring PHP-FPM
PHP-FPM (FastCGI Process Manager) is responsible for handling PHP requests. By default, PHP-FPM runs as the Apache user, but since we are using Nginx, we need to change the user and group to Nginx. Open the www.conf
file using a text editor:
vi /etc/php-fpm.d/www.conf
Find the lines that specify the user and group and change them as follows:
user = nginx group = nginx
Save the file and reload PHP-FPM:
systemctl reload php-fpm
11. Testing PHP
To ensure that PHP is working correctly, you can create a simple info.php
file and access it through your web browser. Use the following command to create the file:
echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php
Then, restart Nginx and PHP-FPM:
systemctl restart nginx php-fpm
You can now access the info.php
file in your browser by entering your server’s IP address followed by /info.php
. If PHP is configured correctly, you should see a page displaying detailed information about your PHP installation.
12. Conclusion
Congratulations! You have successfully set up a LEMP stack on AlmaLinux 8. By combining the power of Linux, Nginx, MariaDB, and PHP, you now have a reliable and high-performance web server. Remember to regularly update and maintain your server to ensure optimal performance and security.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host. They offer Linux SSD VPS services that can support your LEMP stack and provide top-notch performance and security.