VHosts and Directives, Part 2: Create a new domain with a new vHost file in nginx

How to create a new domain with a new vHost file to a different directory in Linux / Unix.

In this tutorial we are going to create a new domain using the local IP address with a new vHost configuration file in nginx

1.) First edit host file and add local ip address

vim /etc/hosts www.myexample.local

2.) Use ping command to check the ip

ping www.myexample.local
PING www.myexample.local ( 56(84) bytes of data.
64 bytes from www.myexample.local ( icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from www.myexample.local ( icmp_seq=2 ttl=64 time=0.033 ms
64 bytes from www.myexample.local ( icmp_seq=3 ttl=64 time=0.042 ms
64 bytes from www.myexample.local ( icmp_seq=4 ttl=64 time=0.082 ms

If you don’t get anything, then your dns is set to look for external dns.

3.) Create a new directory in /var/www/html/

mkdir /var/www/html/myexample
echo "myexample.local" > /var/www/html/myexample/index.html

4.) Create a new .conf file in /etc/nginx/vhost.d/

cd /etc/nginx/vhost.d/

vim www.myexample.local.conf

add following code

server {

listen 80;
root /var/www/html/myexample;
index index.html index.htm index.php;
server_name www.myexample.local myexample;


5.) Make sure that you haven’t made any typos or added any invalid entries. For this use following command:

nginx -t

At this point all we need to do reload our configuration which will now pick up the ability for it to serve effectively two types of domains localhost and www.myexample.local

6.) Restart the Service

systemctl restart nginx 

Video Tutorial

That’s all for now. Have questions, post them in the comments.

Add Comment