Setting Up Nginx Virtual Hosts on Rocky Linux 9: A Step-by-Step Guide

Introduction:

Nginx is a powerful web server that can host multiple websites or domains on a single server using virtual hosts. Virtual hosts allow you to configure different websites with separate domain names, content, and settings on the same Nginx instance. In this article, we will guide you through the process of setting up Nginx virtual hosts on Rocky Linux 9.

Step 1: Install Nginx:

Before setting up virtual hosts, ensure that Nginx is installed on your Rocky Linux 9 system. If it is not already installed, open a terminal and execute the following command:

sudo dnf install nginx

Step 2: Create Virtual Host Configuration Files:

Nginx stores virtual host configurations in the /etc/nginx/conf.d/ directory. Each virtual host configuration file represents a separate website or domain. Start by creating a new configuration file for your virtual host using a text editor. For example:

sudo nano /etc/nginx/conf.d/example.conf

Inside the configuration file, add the following basic structure:

server {
    listen 80;
    server_name example.com www.example.com;

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

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

Replace example.com with your actual domain name or website. Adjust the root directive to specify the directory where your website files are located.

Save and close the configuration file.

Step 3: Enable the Virtual Host:

To enable the virtual host configuration, create a symbolic link from the /etc/nginx/sites-enabled/ directory to the configuration file in the /etc/nginx/conf.d/ directory. Run the following command in the terminal:

sudo ln -s /etc/nginx/conf.d/example.conf /etc/nginx/sites-enabled/

Step 4: Test Nginx Configuration:

Before restarting Nginx, it’s essential to ensure that the configuration is error-free. Run the following command to validate the configuration files:

sudo nginx -t

If there are no syntax errors, proceed to the next step. If errors are detected, review your configuration file for any mistakes and correct them before proceeding.

Step 5: Restart Nginx:

After confirming that the configuration is correct, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Configure DNS:

To access your website, you need to configure the DNS settings for your domain. Add an A record or CNAME record pointing to the IP address of your Rocky Linux server. This step is typically done in your domain registrar or DNS provider’s control panel.

Step 7: Test Your Virtual Host:

Open a web browser and enter your domain name (e.g., http://example.com). If everything is set up correctly, you should see your website served by Nginx.

Step 8: Add Additional Virtual Hosts:

To set up additional virtual hosts for other websites or domains, repeat steps 2 to 7, creating a new configuration file for each website and enabling it in the /etc/nginx/sites-enabled/ directory.

Conclusion:

Congratulations! You have successfully set up Nginx virtual hosts on Rocky Linux 9. Virtual hosts allow you to host multiple websites on a single Nginx instance, enabling efficient resource utilization and effective management of your web server. By following the step-by-step guide, you can easily configure and host multiple domains or websites on your Rocky Linux server using Nginx.


Related Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *