Install and Configure Nginx on AlmaLinux 9

By Raman Kumar

Updated on Aug 06, 2024

In this tutorial, we'll explain, how to install and configure Nginx on AlmaLinux 9.
 
Nginx is a high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. It is renowned for its stability, rich feature set, simple configuration, and low resource consumption. 
 
Originally created by Igor Sysoev, Nginx has grown in popularity due to its ability to handle a high number of concurrent connections, making it an excellent choice for modern web applications. It can serve static content efficiently, handle dynamic content by acting as a reverse proxy, and manage load balancing, HTTP caching, and more.

Prerequisites

  • A AlmaLinux 9 installed dedicated server or KVM VPS
  • A root user access or normal user with administrative privileges
  • Knowledge of basic Linux commands

Step 1: Update Your System

First, ensure your system is up-to-date by running the following commands:
sudo dnf update -y

Step 2: Install Nginx

Nginx is available in the default AlmaLinux repositories. Install it using:
sudo dnf install nginx -y
Start the Nginx service and enable it to start on boot:
sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Configure Firewall

Allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

Hosting a Website with Nginx on AlmaLinux 9

Step 1: Create a Directory for Your Website

Create a directory to store your website files:
sudo mkdir -p /var/www/mywebsite

Step 2: Set Permissions

Set the correct permissions for the directory:
sudo chown -R nginx:nginx /var/www/mywebsite

Step 3: Create a Sample HTML File

Create an index.html file to test your website:
nano /var/www/mywebsite/index.html
Add following content:
<html>
<head>
  <title>Welcome to My Website</title>
</head>
<body>
  <h1>Success! Your website is working!</h1>
</body>
</html>

Step 4: Create an Nginx Server Block Configuration File

Create a configuration file for your website:
sudo nano /etc/nginx/conf.d/mywebsite.conf
Add the following content to the file:
 
Note: Replace mywebsite.com and directory path mywebsite with your domain name. 
 
server {
  listen 80;
  server_name mywebsite.com;

  root /var/www/mywebsite;
    index index.html;

    location / {

      try_files $uri $uri/ =404;
  }
}

Step 5: Restart Nginx

Test the Nginx configuration for syntax errors:
sudo nginx -t
Restart Nginx to apply the changes:
sudo systemctl restart nginx

Step 6: Configure SELinux

If you have SELinux enabled in your system, you need to configure SELinux. Execute following command:
sudo chcon -R -t httpd_sys_content_t /var/www/mywebsite
Note: Replace the path with your path.

Optional: Enabling SSL with Let's Encrypt

Step 1: Install Certbot

Install Certbot, the tool to obtain SSL certificates from Let’s Encrypt:
sudo dnf install epel-release -y
sudo dnf install certbot python3-certbot-nginx -y

Step 2: Obtain SSL Certificate

Run Certbot to obtain and configure the SSL certificate:
sudo certbot --nginx -d mywebsite.com

Step 3: Automatic Renewal

Set up a cron job for automatic renewal of the SSL certificate:
echo "0 0,12 * * * root python3 -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew" | sudo tee -a /etc/crontab > /dev/null

Access Your Website

Open a web browser and navigate to your domain (e.g., https://mywebsite.com). You should see the sample HTML page you created earlier.

Conclusion

You've successfully installed and configured Nginx on AlmaLinux 9 and hosted a website. This tutorial covered basic installation, configuration, and enabling SSL for secure connections. For more advanced configurations, refer to the Nginx documentation.