Warning |
---|
The main part of this article has been moved but the sub pages still need to be completed. |
What is Virtual Hosting?
Virtual hosting configures Apache to be able to host more than one website on the same computer. As an example, let's say this Apache Web Server will host both www.earth.com and www.krypton.com. Here is how the process works,
...
Note that there is a disadvantage with specifying specific log files per virtual hosts because you can run out of file descriptors. The pro of course is simplicity and easy separation of your logs. Ther may be alternatives but don't hold your breath for me to write about thisfind a solution and publish it. My clients rarely keep more than 3 virtual sites on the same machine.
...
Enable Virtual Host
Just because you created the virtual host does not mean it is enabled. To enable the virtual hosting,
Code Block | ||
---|---|---|
| ||
sudo a2ensite www.krypton.com # enable a virtual host
sudo a2ensite www.earth.com # enable a virtual host |
Info |
---|
As an side note, a2ensite is a shortcut command which creates a symbolic link. It is exactly the same things as doing this, cd /etc/apache2/sites-enabled/ |
You will then be promoted to execute sudo /etc/init.d/apache2 reload to reload the Apache configuration file for changes to take effect. This command is useful because it does not affect users currently browsing your other sites. However, I have found this sometimes does not work for me. In that case, I usually issue a full restart,
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 start
Type in your browser, www.krypton.com. Because directory listing is enabled and there is no default html page usually index.html you should see an directory page listing the contents of /home/serveradmin/www.krypton.com/.
Disable Virtual Host
You can disable your reference using the equivalent sudo a2dissite .Again you must must restart Apache for the changes to take effect.
Under the Covers
The Ubuntu packaging enabled certain directives for you. If you are using a vanilla implementation of Apache you might need to do some more work namely using the Listen and NameVirtualHost directives.
Ubuntu already added these directives as shown,
tpham@krypton:/etc/apache2$ find . -type f | xargs grep -i listen
./ports.conf:Listen 80
./ports.conf: Listen 443
tpham@krypton:/etc/apache2$ find . -type f | xargs grep -i namevirtualhost
./ports.conf:NameVirtualHost *:80
./ports.conf: # NameVirtualHost statement here
tpham@krypton:/etc/apache2$
Next
There's much more to Apache than this. For example, we could set up public and private Online Shares. If you are serving real traffic you might want to read the next step about configuring logging. If you are just playing around then you can skip to setting up an application server.
Resources
http://httpd.apache.org/docs/2.0/vhosts/examples.html - official examples from Apache
http://mail-archives.apache.org/mod_mbox/httpd-users/200603.mbox/%3C200603161214.10191.mymaillists@gmx.at%3E - good working example of how to do virtual hosting with different ports.