How To Set Up Nginx Virtual Host (Server Blocks) on CentOS 7

Introduction

How To Set Up Nginx Virtual Host (Server Blocks) on CentOS 7

1. Install Nginx

Before you can set up Nginx virtual hosts, you need to install Nginx on your CentOS 7 server. To do this, open a terminal window and run the following command:

sudo yum install nginx

2. Create the Directory Structure

Once Nginx is installed, you need to create the directory structure for your virtual hosts. By default, Nginx looks for virtual host configurations in the /etc/nginx/conf.d/ directory.

Create the directory structure by running the following command:

sudo mkdir -p /etc/nginx/conf.d/{sites-available,sites-enabled}

3. Create the Virtual Host Configuration File

Now that the directory structure is in place, you can create the virtual host configuration file. This file will contain the configuration settings for your virtual host.

Create the configuration file by running the following command:

sudo nano /etc/nginx/conf.d/sites-available/example.com.conf

4. Add the Virtual Host Configuration

Once the configuration file is created, you need to add the configuration settings for your virtual host.

Add the following configuration to the file:

server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/public_html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}

5. Create the Document Root Directory

Next, you need to create the document root directory for your virtual host. This is the directory where your website files will be stored.

Create the directory by running the following command:

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

6. Create the Index File

Now that the document root directory is created, you need to create an index file. This file will be served when someone visits your website.

Create the index file by running the following command:

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

7. Enable the Virtual Host

Once the virtual host configuration file and document root directory are in place, you need to enable the virtual host. To do this, you need to create a symbolic link from the sites-available directory to the sites-enabled directory.

Create the symbolic link by running the following command:

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

8. Test the Configuration

Once the virtual host is enabled, you need to test the configuration to make sure everything is working correctly.

Test the configuration by running the following command:

sudo nginx -t

9. Restart Nginx

If the configuration test is successful, you need to restart Nginx for the changes to take effect.

Restart Nginx by running the following command:

sudo systemctl restart nginx
[ad_1]

Introduction

Server Blocks, often referred to as Nginx virtual host are a feature of the Nginx web server that allows you to host multiple websites on one server. As opposed to setting up and configuring a server for each domain, hosting a number of websites on a single machine saves both time and money.

Domains are isolated and independent, each having a separate:

  • Directory for site documentation
  • Website security policy
  • SSL certificate

In this tutorial, learn how to set up Nginx server blocks and configure a local host file on CentOS 7.

Tutorial on how to set up NGINX server blocks on CentOS 7.

Prerequisites

  • A CentOS self-managed server or VM
  • Nginx installed on the system
  • A user with sudo privileges

Setting Up Virtual Host / Server Blocks on CentOS

Step 1: Create Directory Structure

The Nginx virtual host can foster multiple websites on a single machine. As each website has individual site documentation, you need to create individual directory structures to store the data.

Each server block should have a directory inside the document root (the /var/www directory).

This guide shows you how to create virtual hosts for two servers (website1.com and website2.com). You can adjust the configuration and tailor for your respective resources.

1. Start by using the following command to create a folder inside the /var/www directory with the command:

sudo mkdir -p /var/www/website1.com/html

2. Then, create one for the second website by typing:

sudo mkdir -p /var/www/website2.com/html
create directory structure for nginx

3. Next, change the ownership of the files so other users can add, delete or modify files in the directories. As a result, both directories will belong to the user you are logged into.

4. Use the chown command change file ownership:

sudo chown -R $USER:$USER /var/www/website1.com/html

5. Repeat the process for the second web directory (making sure to change the domain name) by typing:

sudo chown -R $USER:$USER /var/www/website2.com/html

6. Finally, grant reading permission to all the files inside the /var/www directory using the chmod command:

sudo chmod -R 755 /var/www
change ownership of directory file of virtual host

Step 2: Create a Demo Page for Virtual Host

The following step is to create the content you want to display on the websites hosted on Nginx server blocks. The easiest way to illustrate the process would be to create an index.html page for the two previously mentioned domains.

1. Use a Linux text editor of your choice to create and open the index.html file for the first website. In this example, we used Vi (but Nano or any other works fine as well):

vi /var/www/website1.com/html/index.html

2. Once a blank Vi page opens, press (to insert) and add the following content:

<html>
  <head>
    <title>Welcome to our first website!</title>
  <head>
  <body>
    <h1>Great work! You have created the website1.com server block.</h1>
  <body>
<html>
create demo page for virtual host

3. Then, save and exit the first file.

4. Repeat the steps for the second domain. But, remember to modify the file name to website2:

vi /var/www/website2.com/html/index.html

Also, add content similar to the previous step, but change the specifics to match the second domain:

<html>
  <head>
    <title>Welcome to our second website!</title>
  <head>
  <body>
    <h1>Great work! You have created the website2.com server block.</h1>
  <body>
<html>

Again, save and exit the file.

Step 3: Set Up Environment for Server Block Files

Before we set up virtual hosts for the two domains, we need to create two directories:

  • Sites-available directory to store the server blocks in.
  • Sites-enabled directory that will tell Nginx which links to publish and which blocks share content with visitors.

1. Use the mkdir command to create the new directories using the commands:

sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
create server block directories with mkdir

2. Then, open the Nginx configuration file and modify it by adding the sites-enabled directory:

sudo vi /etc/nginx/nginx.conf

3. Inside the http block, add the following two lines:

include /etc/nginx/sites-enabled/*.conf
server_names_hash_bucket_size 64;
nginx configuration file

While the first line instructs Nginx to check the sites-enabled directory, the second increases how much memory is reserved for examining multiple domain names.

4. Save the file and exit.

5. Verify the Nginx configuration file after making any kind of alterations to ensure the syntax is correct. Doing so can help avoid possible errors in the future.

Test the file with the following command:

sudo nginx --t

If the syntax is OK, the output tells you the test was successful, as in the image below.

testing nginx configuration with output that the test is sucessful

However, if it finds an issue in the syntax, the output specifies where the mistake is and how to go back and correct it.

Step 4: Create Server Block Files

The Nginx web server package comes with a default server block under the name default.conf. As you need to create server blocks for each domain, the easiest way to do so would be to copy the existing template and modify the specifications.

1. Create a virtual host for the first website with the cp command to make an exact copy of the file:

sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/website1.com.conf

2. Open the cloned file with a text editor with the following command:

sudo vi /etc/nginx/sites-available/website1.com.conf

The content of the file should appear as in the image below:

cloned nginx default configuration file displayed

3. There are three (3) lines you’ll need to edit in the file:

  • The server_name should correspond to the domain name of your first website. Make sure to include the address with and without the www. prefix. By doing so the server will recognize both types of requests from visitors and redirect them to the same content.

In our example, the server_name would therefore be:

server name website1.com www.website1.com;
  • Modify the root directory to coincide with website1.com using the command:
root /var/www/website1.com/html;
  • Add a try_files command with a 404 error for instances when the server receives requests for untraceable files and directories:
try_files $uri $uri/ =404;

Make sure to save the file before exiting. After all the changes have been made, the configuration file will appear as follows:

nginx configuration file for server block with server name

4. Repeat the process for the second server block while changing the details to correspond to website2.com.

This includes copying the default configuration file with the command:

sudo cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/website2.com.conf

Followed by opening a file with a text editor:

sudo vi /etc/nginx/sites-available/website2.com.conf

Edit the content in the following manner:

server name website2.com www.website2.com;
try_files $uri $uri/ =404;
root /var/www/website2.com/html;
second server block file configuration

Step 5: Enable Server Block Files

To enable the virtual host files, create symbolic links in the sites-enabled directories with the commands:

sudo ln -s /etc/nginx/sites-available/website1.com.conf /etc/nginx/sites-enabled/website1.com.conf
sudo ln -s /etc/nginx/sites-available/website2.com.conf /etc/nginx/sites-enabled/website2.com.conf
create symbolic link for server block in sites-enabled directory

For the changes to take place, make sure to restart Nginx:

sudo systemctl restart nginx

Step 6: Configure Host File

If you’ve used the example domains instead of functioning ones, modify the hosts file to redirect requests to the virtual private server (VPS) you have created.

1. Open the host file in a text edior:

sudo vi /etc/hosts

2. Edit the following two lines under the existing content, while specifying the VPS IP address:

ip_address website1.com
ip_address website2.com

3. Save and exit the file.

Step 7: Verify Server Blocks Setup

To verify the setup of the server blocks, navigate to the websites you’ve created to host on the server blocks.

Open a browser and type in the URL of the first domain:

www.website1.com
screenshot that says welcome to your first website
www.website2.com
website2 successfully setup on virtual host

You should see the content created for the demo page in Step 2.

Conclusion

After reading this article, you should understand the basic principles of installing and setting up Nginx server blocks for hosting multiple domains on CentOS.

Although Nginx is easy to use, it does have a limited number of features. If you require more from your web server, you may want to consider installing Apache on CentOS 7.

[ad_2]

How To Set Up Nginx Virtual Host (Server Blocks) on CentOS 7

Nginx is a powerful web server that can be used to host multiple websites on a single server. It is a popular choice for web hosting due to its scalability, reliability, and performance. In this tutorial, we will show you how to set up Nginx virtual host (server blocks) on CentOS 7.

Prerequisites

  • A server running CentOS 7.
  • A non-root user with sudo privileges.
  • Nginx installed on your server.

Step 1: Create a Directory for Your Virtual Host

First, you will need to create a directory for your virtual host. This directory will contain all the files and directories related to your website. We will create a directory called example.com in the /var/www/ directory.

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

Next, you will need to set the correct permissions for the directory. We will set the owner of the directory to the nginx user and group.

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

Step 2: Create a Sample Page for Your Virtual Host

Now, you will need to create a sample page for your virtual host. We will create a simple HTML page called index.html in the /var/www/example.com/public_html/ directory.

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

Add the following content to the file:

<html>
  <head>
    <title>Welcome to Example.com!</title>
  </head>
  <body>
    <h1>Success! The example.com virtual host is working!</h1>
  </body>
</html>

Save and close the file when you are finished.

Step 3: Create a New Nginx Server Block

Now, you will need to create a new Nginx server block for your virtual host. We will create a file called example.com in the /etc/nginx/conf.d/ directory.

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

Add the following content to the file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/example.com/public_html;
    index index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

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

Save and close the file when you are finished.

Step 4: Test the Nginx Configuration

Before restarting Nginx, you should test the configuration for any syntax errors. You can do this by typing:

sudo nginx -t

If the test is successful, you should see the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 5: Restart Nginx

Once you have tested the configuration, you can restart Nginx to enable the changes. You can do this by typing:

sudo systemctl restart nginx

Step 6: Access Your Website

Now, you can access your website by visiting http://example.com in your web browser. You should see the sample page that you created earlier.

You have successfully set up Nginx virtual host (server blocks) on CentOS 7.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00