Mar 8, 2012

f Comment

How to Run Virtual Hosts in Apache Web Server?

Amazon Suppose my server's IP address is 114.3.27.89 and I own http://example.t4c.com.tw. I configure my web host's DNS settings correctly so that http://example.t4c.com.tw resolves to 114.3.27.89. Here's what I want to achieve. If someone goes to http://example.t4c.com.tw I'd like the document root to be /home/ubuntu/repository/trunk. If someone goes to http://114.3.27.89/ I'd like the document root to be /home/ubuntu/repository/trunk2. Sounds totally simple and doable right? The answer is yes if you read on and follow my instructions

Solution
Your Apache configuration is either httpd.conf or in /etc/apache2/sites-available/ depending on your Apache's version. But the configuration lines are the same. Without further ado let's look at the configuration that would make it happen.
NameVirtualHost *

<VirtualHost *>
        ServerName example.t4c.com.tw
        DocumentRoot /home/ubuntu/repository/trunk

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

</VirtualHost>

<VirtualHost *>
        ServerName 114.3.27.89
        DocumentRoot /home/ubuntu/repository/trunk2
</VirtualHost>
You can easily follow the logic of these lines. ServerName specifies either the IP address or a host name. DocumentRoot specifies where to serve content once ServerName is matched. So it's easy to see that any request that goes to example.t4c.com.tw is served by showing contents in /home/ubuntu/repository/trunk, and any request that goes to 114.3.27.89 is served by /home/ubuntu/repository/trunk2.

Any questions? Let me know!

Mysteries
So I don't really get the whole deal with NameVirtualHost and VirtualHost. They just don't work as described by Apache's official documentation AT ALL! The following are examples of me following Apache's documentation or guide and the bad results thereof.

For example if I include "Listen 80" per http://httpd.apache.org/docs/2.0/vhosts/examples.html I will not able to restart Apache for some reason:

ubuntu@ip-10:~$ sudo /etc/init.d/apache2 start
* Starting web server apache2 [Sun Feb 13 03:14:41 2012] [warn] NameVirtualHost *:80 has no VirtualHosts
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
[fail]
ubuntu@ip-10:~$

This is weird because I used 'sudo lsof -i:80' to find that no process is bound to that port. I don't even want to bother myself with reading the log.

My server's IP address is 114.3.27.89. If I use the following configuration per http://httpd.apache.org/docs/2.0/mod/core.html#virtualhost:
NameVirtualHost 114.3.27.89

<VirtualHost 114.3.27.89>
        ServerName example.t4c.com.tw
        DocumentRoot /home/ubuntu/repository/trunk

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

</VirtualHost>

<VirtualHost 114.3.27.89>
        ServerName 114.3.27.89
        DocumentRoot /home/ubuntu/repository/trunk2
</VirtualHost>
And restart the web server. Then I get a 404 Not Found error when I go to http://114.3.27.89/ or http://example.t4c.com.tw/.

As you can see Apache's configuration is very peculiar despite its seemingly comprehensive documentation and tutorials all over the internet. At http://httpd.apache.org/docs/2.0/mod/core.html#virtualhost it clearly says that VirtualHost can be used with the IP address of the virtual host, A fully qualified domain name for the IP address of the virtual host. But neither of them works! Only * works. WHY?
Anyway hope you at least know how to set up VirtualHost directives so that they do what you want to achieve!

Any questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael