How to Configure Virtual Hosts On Nginx With CentOS 7

Nginx server blocks also known as Nginx virtual hosts are configurations for setting up multiple websites on a single server.

If you have more than one domain but you don’t want to incur additional costs on VPS plans, you may utilize the Nginx server blocks feature.

Although Nginx is a free and an open-source web server, it can handle more concurrent connections than Apache.

in this guide, we will show you how to set up multiple websites on Nginx with CentOs 7 as the operating system.

Prerequisites

Before you begin, make sure you have the following:

Step 1: Installing Nginx on CentOs 7 Server

We will start off by installing Nginx web server. The package is available on the EPEL repository. So, we need to add the information on the server:

$ sudo yum install epel-release

Then, install Nginx:

$ sudo yum install nginx

Press Y and hit Enter when prompted to confirm the installation.

Once the Nginx web server is installed, start it by typing the command below:

$ sudo systemctl start nginx

Then, enable Nginx to start at boot.

$ sudo systemctl enable nginx

Then, allow the HTTP and HTTPS traffic from CentOs firewall.

$ sudo firewall-cmd --zone=public --permanent --add-service=http
$ sudo firewall-cmd --zone=public --permanent --add-service=https
$ sudo firewall-cmd --reload

You can then confirm if the installation was successful by entering the IP address associated with your server on a browser like Google Chrome.

http://192.0.0.1

You should see the default Nginx home page as shown below:

Step 2: Setting Up Nginx Virtual Host on the First Domain

Next, we are going to configure the first virtual host on Nginx. We will use the example.com as the first domain.

The default Nginx configuration file can be located at /etc/nginx/nginx.conf

Also, the root of the default Nginx website points to  /usr/share/nginx/html.

Since the above directory setup can be a little bit confusing for hosting multiple websites on Nginx, we will create a structure which is more understandable.

First, let’s create the directory to hold our example.com websites data.

$ sudo mkdir -p  /var/www/example.com/public_html

The -p option tells Linux to create any parent directory on the way.

The directory we created above is owned by the root user. However, we want our regular user to be able to modify files on it, so let’s change the directory ownership.

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

Next, we need to set up the appropriate file permissions to allow Nginx to read and execute the files from the public_html directory.

$ sudo chmod -R 755 /var/www/example.com/public_html

Once the directory structure is in place we can create a sample web page on the root of the example.com directory

$ sudo nano /var/www/example.com/public_html/index.html

Then, paste the information below on the file

<html>
  <head>
    <title>Sample web page on example.com website</title>
  </head>
  <body>
    <h1>Nginx server block 1</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.com
  </body>
</html>

Save and close the file by pressing CTRL+X, Y, and Enter

Next, we need to create a server block for the example.com website.

All Nginx virtual host files must end with .conf

The server block configuration files must be placed in the /etc/nginx/conf.d directory.

So let’s create the first Nginx block for the example.com website using nano text editor.

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

Then, enter the information below on the file

server {
    listen  80;
    server_name example.com;

    location / {
        root  /var/www/example.com/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Step 3: Setting Up Nginx Virtual Host on the Second Domain

Our server block for the first host is set up. Next, we can go ahead and configure the second virtual host for example.net domain.

First, we create the directory structure.

$ sudo mkdir -p /var/www/example.net/public_html

Then, change the directory ownership.

$ sudo chown -R $USER:$USER /var/www/example.net/public_html

Next, let’s create a sample web page on the example.net root directory.

$ sudo nano /var/www/example.net/public_html/index.html

We can now add the information below to render the HTML web page

<html>
  <head>
    <title>Sample web page on example.net website</title>
  </head>
  <body>
    <h1>Nginx server block 2</h1>
    This sample web page confirms that the first Nginx virtual host 
   or server block is working for example.net
  </body>
</html>

Save and close the file.

We can now go ahead and create a server block for the example.net website.

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

Then, enter the information below on the file

server {
    listen  80;
    server_name example.net;

    location / {
        root  /var/www/example.net/public_html;
        index  index.html index.htm;
    }

    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}

Restart the Nginx web server for the configurations to take effect:

$ sudo systemctl restart nginx

Step 4: Testing Nginx Server Blocks Configuration

To test the configuration add example.com and example.net on your hosts file together with the associated IP address of your VPS machine.

If you are on Windows, locate the file from C:\Windows\System32\drivers\etc\hosts

Then, add the two virtual hosts information at the end as show below.

# Copyright (c) 1993-2009 Microsoft Corp.
...
#	127.0.0.1       localhost
#	::1             localhost

..

149.28.188.94      example.com
149.28.188.94      example.net

Then, example.com on a web browser. You should see a page similar to the one shown below.

You can also visit example.net website which should render the below web page.

If you have reached this far, congratulations! You just learnt how to host multiple websites on Nginx web server running on Centos 7 operating system.

Conclusion

In this guide, we have shown you how to configure multiple websites on CentOs 7 with Nginx using server blocks or virtual hosts.

Sign up with Digital Ocean today and get $100 to host as many websites as you want on one VPS machine.

Ads by Google

Leave a Reply

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