How To Install Nginx Php Mariadb (LEMP Stack) On Almalinux 9

Stepping into the world of web hosting? The LEMP stack—consisting of Linux, Nginx, MySQL/MariaDB, and PHP—is a powerful, dynamic setup to serve your sites and applications with efficiency and speed.

Unlike its cousin, the LAMP stack, LEMP uses the ultra-fast Nginx instead of Apache, powering high-traffic websites worldwide. Paired with AlmaLinux, a robust and community-driven successor to CentOS, you’re set up for a rock-solid hosting experience.

This guide walks you through the process of setting up Nginx, PHP, and MariaDB on AlmaLinux 9. Whether you’re spinning up a simple blog or crafting complex websites, mastering this setup is a key skill in your web development toolkit.

Let’s dive in and empower your server with the cutting-edge features of the LEMP stack!

Installing and Setting Up Almalinux 9 for LEMP Stack

To kick things off, you’ll need a fresh installation of AlmaLinux 9. Begin by downloading the AlmaLinux 9 ISO from the official site and follow the standard process to install it on your server or virtual machine. Once installed, you’ll have a clean slate to build your LEMP stack.

Preparing Almalinux 9 Environment

Before diving into the stack components, let’s fine-tune AlmaLinux to ensure everything runs smoothly. Start by updating your system packages. Open a terminal and run:

sudo dnf update -y

Next, it’s a good idea to secure your server. Set up a strong password policy, and confirm that the firewalld service is up and running:

sudo systemctl enable firewalld --now

Check your firewall’s status to keep an eye on what’s being allowed in and out:

sudo firewall-cmd --state

With AlmaLinux’s environment now prepped and secured, you’re ready to start installing the components of the LEMP stack, beginning with Nginx.

Installing Nginx on AlmaLinux 9

First, let’s get Nginx up and running on your AlmaLinux 9. Start by making sure your system’s package manager is up-to-date:

sudo dnf update -y

Now, install Nginx using the package manager:

sudo dnf install nginx -y

Once installed, enable and start the Nginx service to ensure it launches at boot:

sudo systemctl enable nginx --now

Verify that Nginx is active and running:

sudo systemctl status nginx

Great! Nginx is now installed and operational.

Configuring Nginx Server Blocks

Server blocks in Nginx allow you to host multiple domains on a single server, similar to virtual hosts in Apache.

First, navigate to the Nginx configuration directory:

cd /etc/nginx/

Within this directory, create a new configuration file for your domain:

sudo nano /etc/nginx/conf.d/yourdomain.com.conf

In this file, define your server block:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

Save and close the file, then test the configuration for any syntax errors:

sudo nginx -t

Finally, reload Nginx to apply the changes:

sudo systemctl reload nginx

You’ve successfully configured Nginx server blocks, paving the way for hosting multiple sites efficiently!

Setting Up PHP for Server-Side Processing

To enable dynamic content on your server, adding PHP is key. Start by installing PHP and the necessary modules with this command:

sudo dnf install php php-fpm php-mysqlnd -y

These packages allow PHP to interact with MariaDB and handle requests efficiently.

After installation, ensure that PHP-FPM (FastCGI Process Manager) is enabled and running:

sudo systemctl enable php-fpm --now

Check the status to confirm:

sudo systemctl status php-fpm

With PHP set up, your server can now process PHP scripts, transforming static web pages into dynamic, interactive experiences.

Configuring PHP for Nginx

PHP and Nginx need a little guidance to work together seamlessly. Start by modifying the /etc/php-fpm.d/www.conf file:

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

Locate the line with listen = 127.0.0.1:9000 and change it to listen = /var/run/php-fpm/www.sock for enhanced performance with Unix socket communication.

Ensure the ownership and permissions are set correctly, then configure Nginx to process PHP scripts. Open your server block configuration file:

sudo nano /etc/nginx/conf.d/yourdomain.com.conf

Add the following within the location / block:

location ~ .php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php-fpm/www.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Save the file, then test the configuration:

sudo nginx -t

Finally, reload Nginx:

sudo systemctl reload nginx

You’ve now configured PHP to work with Nginx, readying your server for handling dynamic content effectively!

Installing and Configuring MariaDB on AlmaLinux 9

Begin your database setup with MariaDB, a reliable, open-source alternative to MySQL. Install it using the following command:

sudo dnf install mariadb-server -y

Once installed, start and enable the MariaDB service so it starts automatically after a reboot:

sudo systemctl enable mariadb --now

Verify the service is running:

sudo systemctl status mariadb

With the service up and running, you’re ready to configure MariaDB for optimal performance and management of your data.

Securing the MariaDB Installation

The default MariaDB setup isn’t fully secure, so let’s lock it down. Run the built-in security script:

sudo mysql_secure_installation

You’ll be prompted to set a root password and remove anonymous users. Say “yes” to disallow remote root login and remove the test database to enhance security.

Each prompt guides you through critical steps so your data remains protected. Taking these measures ensures that your MariaDB installation is not unnecessarily exposed to threats, keeping your data secure and private.

Conclusion

Congratulations on successfully setting up the LEMP stack on AlmaLinux 9! With Nginx serving as your web server, PHP handling server-side processing, and MariaDB managing your databases, you’re equipped to deploy dynamic websites efficiently.

This configuration lays the groundwork for endless possibilities—from hosting personal projects to scaling full-fledged enterprise applications. The combination of Nginx and MariaDB offers performance and reliability, while PHP provides the flexibility needed for modern web development.

As you venture into hosting your sites, remember to regularly update your stack components to maintain security and performance. Explore further optimizations, such as caching mechanisms or additional PHP modules, tailored to your project’s needs.

Your journey into web hosting with the LEMP stack doesn’t end here—consider this just the beginning!

spot_img

Related Articles

Google Plans Two-Week Release Schedule For Chrome

If you're the kind of web user who's always eager to explore the latest features, Chrome's got great news for...
Read more
Security vulnerabilities can be as elusive as they are dangerous. Yet again, we've been reminded of this reality with the...
Managing web servers and applications doesn't have to be a daunting task, especially with tools designed to simplify the process....