Adding a Name-Based Virtual Host in Apache - gHacks Tech News

Adding a Name-Based Virtual Host in Apache

If you are an Apache user for the hosting of either internal or external web sites then you know how flexible this web server can be. But did you know you can host more than one site on that server? You can thanks to Virtual sites. You can host virtual sites based on either IP address or Name. If IP addresses are in short supply the best choice, of course, is name-based virtual hosting.

In this article I will show you how to add name-based virtual hosts in your Apache web server. This will require you to edit the /etc/httpd/conf/httpd.conf file so be prepared for some command-line action. NOTE: This file can be located in various locations

If you've never edited your httpd.conf file don't worry, it's not that difficult. Just make sure you use good comments so you know what you did and where you did it. Otherwise your virtual host section could get lost in the other sections.

The section of Apache where your virtual hosts will go is called, aptly, Virtual Hosts. The format of an entry looks like this:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>

Before you actually start configuring your virtual host you need to make sure you have the NameVirtualHosts line uncommented out. The line will look like:

NameVirtualHosts *

You can find the line by using the grep command like so:

grep -n NameVirtualHosts /etc/httpd/conf/httpd.conf

(NOTE: Edit the path to your httpd.conf file to reflect your setup.)

The grep command will return to you which line the NameVirtualHost entry is on. Now open up the Nano editor and scroll down a bit. You can hit the key combination Ctrl-c to find out what line you are on. Once you find the line you will remove the "#" character (no quotes).

With that out of the way you are ready.

To illustrate how this is done I will show you how to create a virtual server for mail. The following assumes the doc root is /var/www/. Add the following entries to your httpd.conf file:

<VirtualHost *:80>
DocumentRoot /www/yourcompany
ServerName www.yourcompany.com
# Other directives here
</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/mail
ServerName mail.yourcompany.com
# Other directives here
</VirtualHost>

NOTE: Where yourcompany.com is the actual FQDN you use.

NOTE: If you need to change your port you can change it to suit your needs from the default port 80.

The above is the bare minimum configuration for your virtual servers. There are a LOT of possible directives you can use. For example, you might want to add error logging to these virtual hosts for debugging purposes. To add error logs to your virtual mail host? you would add the lines:

ErrorLog /var/log/httpd/mail-error_log common
CustomLog /var/log/httpd/mail-access_log common

To your mail virtual host entry (above the </VirtualHost> line.)

Once you have these entries complete you will need to save the httpd.conf file and restart Apache. To restart Apache issue one of the following commands:

/etc/rc.d/init.d/httpd restart

or

/etc/init.d/apache2 restart

You're done. You should now be able to hit the new virtual hosts.

Final Thoughts

Creating virtual hosts is a quick way to expand the capabilities of your Apache server, cut costs, and save IP addreses. Have you ever deployed virtual hosts with Apache? If so, share your experiences.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. Michele said on March 21, 2009 at 4:41 pm
      Reply

      There’s a missing ‘p’ in the first occurrence of the file /etc/httpd/conf/httpd.conf

      1. Martin said on March 21, 2009 at 4:43 pm
        Reply

        Thanks a lot for spotting that missing character. Corrected the path.

    2. Joshua said on April 2, 2011 at 9:57 pm
      Reply

      Hello,

      I have followed this tutorial, and I am getting a mysterious error. Requests for http://www.mydomain.com work fine, but requests for http://mydomain.com do not work properly. In Firefox, they redirect to http://www.mydomain.com, and in Chrome they result in an “Unable to contact server” message from the browser.

      What’s going on? I have created master DNS entries for all the websites I am hosting, but I have basically left the default settings Linode gives us alone.

      Thanks in advance!

      Joshua

    Leave a Reply