Tuesday, November 5, 2024

Install Nginx on CentOS 9 Stream with a Virtual Host

If you’re new to server administration or web hosting, setting up Nginx with a virtual host in CentOS 9 Stream might seem like a daunting task. But fear not! With a step-by-step approach, you’ll be able to install Nginx on CentOS 9 Stream in no time. Let’s dive in and get started on this exciting journey.

How To Configure Nginx on CentOS 9 Stream

Step 1: Install Nginx on CentOS 9 Stream

Install Nginx First things first, let’s install Nginx on your CentOS 9 Stream server. Open a terminal and run the following command:

sudo dnf install nginx

Sit back and relax while the installation process takes care of everything for you. Once it’s done, you’ll have Nginx ready to go.

Step 2: Configure a Virtual Host on Nginx

Now that Nginx is installed, it’s time to set up your virtual host to host your website. Follow these steps:

  1. Create a new configuration file for your virtual host:
sudo nano /etc/nginx/conf.d/mywebsite.conf
  1. Inside the file, paste the following configuration:
server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/mywebsite;
    index index.html;

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

Replace yourdomain.com with your actual domain name and /var/www/mywebsite with the directory where your website files are located.

  1. Save the file and exit the editor (Ctrl+X, then Y).

Step 3: Test and Apply the Configuration

Before we finalize the setup, let’s test the configuration to ensure everything is in order. Run the following command:

sudo nginx -t

If there are no syntax errors, you should see a message saying “syntax is ok” and “configuration file /etc/nginx/nginx.conf test is successful.” If you encounter any errors, double-check your configuration file for any typos or mistakes.

Once the test is successful, apply the configuration changes by reloading Nginx:

sudo systemctl reload nginx

Step 4: Update DNS or Hosts File

To access your website using the domain name you specified, you need to update your DNS settings or add an entry in your local hosts file. If you’re hosting the website locally for testing purposes, edit the hosts file with the following command:

sudo nano /etc/hosts

Add the following line at the end of the file:

127.0.0.1    yourdomain.com

Save the file and exit the editor.

Step 5: Test Your Website

It’s time to see your website in action! Open your web browser and enter your domain name in the address bar. If everything went smoothly, you should see your website’s content.

Conclusion on Setting Up Nginx

Congratulations! You’ve successfully set up Nginx with a virtual host in CentOS 9 Stream. Now you can start building and hosting your website using the power of Nginx.

Remember, this guide provides a basic setup, and you can further customize Nginx to suit your specific requirements. Exploring Nginx’s extensive configuration options and features will allow you to fine-tune your server and optimize its performance.

Related Articles

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles