0

I'm learning how to create virtual hosts on apache2. I have Ubuntu 16.04 operating system. I also have WSL (Ubuntu 16.04) in Windows 10.

Now, here's what I observed when I made a virtual host on Ubuntu OS:

I created following VH:

#Did work
<VirtualHost *:80>
ServerName xyzvk.com
ServerAlias www.xyzvk.com
DocumentRoot /var/www/xyzvk.com
</VirtualHost>

#Didn't work
<VirtualHost *:80>
ServerName xyzvk.com
ServerAlias www.xyzvk.com
DocumentRoot /var/www/yolo
</VirtualHost>

and added both name and alias in /etc/hosts

When I accessed the url in browser. I worked fine.

Now when I changed the DocumentRoot and restarted server, and tried again, it simply didn't work. It sent me to default root, i.e., /var/www/html

Using WSL

I did the same procedures, except the hosts file, which I edited in Windows hosts file as changing in Ubuntu's hosts file didn't work (maybe because it's Windows OS).

The things worked both way this time, irrespective of the directory name.

So my question is: Is it just a convention to name folder same as domain name (if yes, what can be issues that it didn't work in Ubuntu OS) OR it's mandatory?

Vikas
  • 97
  • 1
  • 5

2 Answers2

2

It is only a convention that normally simplify the live of the sysadmin as it makes everything straightforward.

You can put any path you like in DocumentRoot as long as Apache has relevant rights to go there, and that the rest of the configuration is adapted to that.

When you changed your DocumentRoot and did not had the expected behaviour, you should study Apache logfiles to learn what happened.

Patrick Mevzek
  • 9,921
  • 7
  • 32
  • 43
0

This is probably repeated many times elsewhere, but with 2.2 when defining multiple virtualhosts with the same ip:port combo you must add an additional directive "once" and "in server config" context like this in your case in order to be able to "land" in the second virtualhost:

NameVirtualhost *:80

<VirtualHost *:80>
ServerName xyzvk.com
ServerAlias www.xyzvk.com
DocumentRoot /var/www/xyzvk.com
</VirtualHost>

<VirtualHost *:80>
ServerName xyzvk.com
ServerAlias www.xyzvk.com
DocumentRoot /var/www/yolo
</VirtualHost>
Daniel Ferradal
  • 2,415
  • 1
  • 8
  • 13
  • `NameVirtualHost` is needed until Apache 2.3 and then it became deprecated. Cf http://httpd.apache.org/docs/2.4/mod/core.html#namevirtualhost : "Prior to 2.3.11, NameVirtualHost was required to instruct the server that a particular IP address and port combination was usable as a name-based virtual host. In 2.3.11 and later, any time an IP address and port combination is used in multiple virtual hosts, name-based virtual hosting is automatically enabled for that address." – Patrick Mevzek Apr 19 '18 at 13:28