Creating Virtual Host on Nginx Web Server with Example in Centos

Actually this is a the continue part of my previous post which explains how to setup a LEMP server but the tutorial never actually teaches the user how to setup a virtual host for each of their website but this should be pretty straight forward and i am going to show you how this can be done.

Setup Virtual Host Directory

Before we go into nginx configuration, we will need to setup the location where we want our website files to be located. In this case, i have decided to just throw them into /var/www (you can do ln -s /var/www /home/www if you want it to be in /home in a later stage) but the folder isn't available so i will have to setup these virtual host folder myself.

sudo mkdir -p /var/www/monitorboard.com/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 i'll explain it later too.

Granting Permission to our Virtual Host Directory

Since we created our directory using a root account, we will have to change it to its rightful owner and in this case, it will be 'nginx' as shown below

sudo chown -R nginx:nginx /var/www/monitorboard.com/html

Additionally, it is important to make sure that everyone is able to read our new files so we have to correct its permission to 755

sudo chmod 755 /var/www

And we are done with permission!

Nginx Virtual Host

To setup your nginx virtual host, you will need to open up nginx virtual file located at /etc/nginx/conf.d/virtual.conf and you will need to update it with the following configuration

server {
    listen       8080;
    server_name  monitorboard.com www.monitorboard.com;
    root   /var/www/monitorboard.com/html;
    index  index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    }
# Logging --
    access_log  /var/www/monitorboard.com/access.log;
    error_log  /var/www/monitorboard.com/error.log;

# serve static files directly
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
            access_log        off;
            expires           max;
    }

    include /etc/nginx/conf.d/global/security.conf;
    include /etc/nginx/conf.d/global/cache.conf;
    include /etc/nginx/conf.d/global/php-fpm.conf;
    include /etc/nginx/conf.d/global/status.conf;
    include /etc/nginx/conf.d/global/wordpress-w3-total-cache.conf;
}

As you can see, i've setup my domain to the folder /var/www/ instead of the default placement of /usr/share/nginx/html which means that this domain will read from the folder /var/www/monitorboard/html instead of anywhere else! Port 8080 is being used because we have varnish installed in our server as shown on our previous tutorial of how to setup a LEMP server.

Lastly all the include file can be get from nicolargo github. This will ensure that all our PHP files can be run off our nginx server but before anything can be run, we will need to restart our nginx server and php-fpm service.

service nginx restart
service php-fpm restart

This will ensure our setup is being updated on our machine.

Fake domain testing

In the case monitorboard.com isn't own by you, you will need to setup your hosts file to reflect your machine to redirect all request to your web server. Assuming your webserver ip is 1.1.1.1, you will need to update your hosts file in your computer/machine located in /etc/hosts and add in the following config

# 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 
1.1.1.1    www.monitorboard.com 

Now, if you open up your browser and hit 'www.monitoboard.com', you should be able to test your nginx configuration that you have done on your web server at 1.1.1.1 🙂