Tutorial

How To Set Up nginx Virtual Hosts (Server Blocks) on CentOS 6

Published on June 8, 2012
How To Set Up nginx Virtual Hosts (Server Blocks) on CentOS 6

Status: Deprecated

This article covers a version of CentOS that is no longer supported. If you are currently operating a server running CentOS 6, we highly recommend upgrading or migrating to a supported version of CentOS.

Reason: CentOS 6 reached end of life (EOL) on November 30th, 2020 and no longer receives security patches or updates. For this reason, this guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other CentOS releases. If available, we strongly recommend using a guide written for the version of CentOS you are using.

The following DigitalOcean tutorial may be of interset, as it outlines setting up Nginx Server Blocks on a CentOS 7 server:


About Virtual Hosts

Virtual Hosts are used to run more than one website or domain off of a single virtual private server.Note: according to the nginx website, Virtual Hosts are called Server Blocks on nginx. However, for the sake of easy comparison with Apache, I'll refer to them as virtual hosts throughout this tutorial.

Intro

Make sure that nginx is installed on your VPS. If it is not, you can quickly install it with 2 steps.

Install the EPEL repository:

 su -c 'rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm'

Install nginx

yum install nginx

Step One— Create a New Directory

The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.

This location will be your Document Root in the Nginx virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.

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

You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name.

However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Six.

Step Two—Grant Permissions

We need to grant ownership of the directory to the right user, instead of just keeping it on the root system. You can replace the "www" below with the appropriate username.

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

Additionally, it is important to make sure that everyone is able to read our new files.

sudo chmod 755 /var/www

Now you are all done with permissions.

Step Three— Create the Page

We need to create a new file called index.html within the directory we made earlier.

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

We can add some text to the file so we will have something to look at when the the site redirects to the virtual host.

<html>
  <head>
    <title>www.example.com</title>
  </head>
  <body>
    <h1>Success: You Have Set Up a Virtual Host</h1>
  </body>
</html>

Save and Exit

Step Four—Set Up the Virtual Host

The next step is to enter into the nginx configuration file itself.

sudo vi /etc/nginx/conf.d/virtual.conf

The virtual host file is already almost completely set up on your virtual server. To finish up, simply match the following configuration, modifying the server name and file location as needed:

#
# A virtual host using mix of IP-, name-, and port-based configuration
#

server {
    listen       80;
#    listen       *:80;
    server_name  example.com;

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

Save and exit.

Step Five—Restart nginx

We’ve made a lot of the changes to the configuration. Restart nginx and make the changes visible.

/etc/init.d/nginx restart

Optional Step Six—Setting Up the Local Hosts

If you have been using an actual domain or IP address to test your virtual servers, you do not need to set up local hosts. However, if you are using a generic domain that you do not own, this will guarantee that, on your computer only, you will be able to customize it.

For this step, make sure you are on the computer itself, not your VPS.

To proceed with this step you need to know your computer’s administrative password, otherwise you will be required to use an actual domain name or your IP address to test the virtual hosts.

Assuming that you do have admin access (gained by typing su and entering the correct password) here is how you can set up the local hosts.

On your local computer, type:

nano /etc/hosts

You can add the local hosts' details to this file, as seen in the example below. As long as line with the IP address and server name is there, directing your browser toward, say, example.com will give you all the virtual host details for the corresponding IP address that you designated.

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost

#Virtual Hosts 
12.34.56.789    www.example.com 

However, it may be a good idea to delete these made up addresses out of the local hosts folder when you are done to avoid any future confusion.

Step Seven—See Your Virtual Host in Action

Once you have finished setting up your virtual host, you can see how it looks online. Point your browser to your domain name or IP address, and you should see that the page displays, "Success—You Have Set Up a Virtual Host"

Adding More Virtual Hosts

To create additional virtual hosts, you can just repeat the process above, being careful to set up a new document root with the appropriate new domain name each time. Then just copy and paste the new Virtual Host information into the nginx Config file, as shown below

#
# A virtual host using mix of IP-, name-, and port-based configuration
#

server {
    listen       80;
#    listen       *:80;
    server_name  example.com;

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


server {
    listen       80;
#    listen       *:80;
    server_name  example.org;

    location / {
        root   /var/www/example.org/public_html/;
         index  index.html index.htm;
    }
}
By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I followed each step of the diaspora install and I went to my domain and it only showed “It Works” page. So I tried to use this manual to set up a virtual host, now I am getting this error my homepage:

403 Forbidden

nginx/1.0.15

nginxgenerator.com help create nginx virtual host file easily!

Just incase anyone was following this on ubuntu, (maybe CentOS as well) If you have the root and index directives inside of the [location] bracket your virtual servers won’t work. Only the default site will be returned.

Instead of: server { … location / { root /var/www/example.com/public_html/; index index.html index.htm; }

It should be:

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

By default nginx is configured (nginx.conf) with “user nginx;” But this user does’nt have right permissions How to choose and set the right user for nginx on centos 7?

Simple WAY. For Example:Site2.com

  1. Create folder …/html/Site2.com using VSFTP

  2. Create file /etc/nginx/conf.d/virtual.conf

server {
    listen       80;
#    listen       *:80;
    server_name  site2.com;

    location / {
        root   /usr/share/nginx/html/site2.com/;       
         index index.php index.html index.htm;
    }
	location ~ .php$ {
        root           /usr/share/nginx/html/site2.com/;   
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Reboot server.

  1. Go to : https://cloud.digitalocean.com/domains.

Add site2.com to Droplet ( ADD DOMAIN)

NAME : @              IP ADDRESS: your droplet IP

  1. Change NAME SERVER of site2.com to :
NS1.DIGITALOCEAN.COM

NS2.DIGITALOCEAN.COM

NS3.DIGITALOCEAN.COM

That’s it: http://bestaroundtheweb.com

This comment has been deleted

    Please note that this article is a couple years old… I was looking for a few things related to nginx when I came across this article. Another article to set up nginx, links to this for vhost setup, https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-6

    However, just by reading the text of this article, it’s a copy and paste hack job of someone trying to put an article together who has no experience with what they are doing. And they create nothing but a mess. The article text also refers to Apache in a few places… There is no Apache config with Nginx…

    This is also not the correct way to set of multiple websites… Search google for articles that show you how to use /nginx/sites-available and /nginx/sites-enabled to do it the right way…

    Hmmm | am still having issues with the PHP files being downloaded…

    In this tutorial (https://www.digitalocean.com/community/articles/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-6), I installed nginx, php & mysql and was able to access my website through my IP address AND see my info.php page OK (the PHP file there works till now).

    However the php files in my VPS just don’t work. I have restarted both nginx and php fpm, and here is my virtual.conf file:

    server { listen 80;

    listen *:80;

    server_name  website.com www.website.com;
    
    location / {
        root   /var/www/website.com/public_html/;
         index  index.php index.html index.htm;
    }
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    

    }

    On the other hand, here is my default.conf which displays PHP files OK:

    server { listen 80; server_name 000.00.000.000;

    location / {
        root   /usr/share/nginx/html;
        index index.php  index.html index.htm;
    }
    
    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    

    }

    If I want to install a CMS for these virtual hosts, although these clients is never going to have directly access to to server itself (through ssh or ftp/sftp) only through a file archive in the CMS, would it then still be considered more secure and a best practice to create a new user for each of these virtual hosts? Would that make it easier to keep an eye on disk usage for each of these in the future? or just complicate stuff as I’m not trying to reinvent PLESK.

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 8, 2014

    @yazirarafath: It doesn’t matter – you can store it wherever you want. You should probably move it to another file just so it’s clear where every site’s settings are stored.

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.