Virtual host

How To Set Up Nginx Virtual Hosts (Server Blocks) on CentOS 8

If you’re looking to host multiple websites on a single server, utilizing Nginx’s virtual hosts, also known as server blocks, is an efficient way to achieve this. In this guide, we will walk you through the steps to set up Nginx virtual hosts on CentOS 8.

Prerequisites

Before we begin, ensure you have the following:

  • A CentOS 8 server with root or sudo privileges.
  • Nginx installed on your server. If you haven’t installed Nginx yet, you can do so with the command:
  sudo dnf install nginx
  • Basic knowledge of working with the command line and text editors.

Step 1: Install Nginx

If you haven’t already installed Nginx, you can easily install it using the package manager. Execute the following command:

sudo dnf install nginx

Once the installation is complete, start the Nginx service and enable it to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

You can verify that Nginx is running by navigating to your server’s IP address in a web browser. If you see the default Nginx welcome page, everything is set up properly.

Step 2: Create Directory Structure

For each website, you will need a separate directory. Let’s create directories for our example sites, example1.com and example2.com:

sudo mkdir -p /var/www/example1.com/public_html
sudo mkdir -p /var/www/example2.com/public_html

Next, set the directory ownership to the Nginx user:

sudo chown -R $USER:$USER /var/www/example1.com/public_html
sudo chown -R $USER:$USER /var/www/example2.com/public_html

You may want to create simple HTML files to test:

echo "<h1>Welcome to Example1.com!</h1>" > /var/www/example1.com/public_html/index.html
echo "<h1>Welcome to Example2.com!</h1>" > /var/www/example2.com/public_html/index.html

Set the proper permissions:

sudo chmod -R 755 /var/www

Step 3: Create Server Block Configuration Files

Nginx server blocks are defined in configuration files stored in the /etc/nginx/conf.d/ directory. Let’s create two configuration files for our examples.

For example1.com:

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

Add the following configuration:

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

    root /var/www/example1.com/public_html;
    index index.html;

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

For example2.com:

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

Add the following configuration:

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

    root /var/www/example2.com/public_html;
    index index.html;

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

Step 4: Test Nginx Configuration

After creating the server block configuration files, it’s crucial to test the Nginx configuration for any syntax errors. Run the following command:

sudo nginx -t

If everything is set up correctly, you should see a message indicating the configuration file is okay.

Step 5: Restart Nginx

Now that the configuration is tested and verified, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 6: Configure DNS

To access your websites via a domain name, ensure that your domain’s DNS settings are pointing to your CentOS server’s IP address. For example1.com and example2.com, you can set the A records in your DNS provider’s dashboard.

Step 7: Access Your Websites

Open your web browser and go to http://example1.com and http://example2.com. You should see the respective welcome messages for each site.

Conclusion

Congratulations! You’ve successfully set up Nginx virtual hosts (server blocks) on CentOS 8. This powerful configuration allows you to host multiple sites efficiently. As you continue to grow your projects, consider exploring SSL setup for secure HTTPS connections, performance tuning, or using a reverse proxy for additional functionalities.

Happy hosting!