简体   繁体   中英

How to specify directory name to create virtual host in apache2?

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, ie, /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?

For creating new virtual host, the best practice is that you have separate directories for each VH. It seems to me that when you changed the VH Document Root, the new path does not exists. For getting more information, check your error logs. For default configuration it's located

/var/log/apache2/error.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM