Linux.com

Feature: Apache & Web Servers

How to set up Apache virtual hosting

By James Lees on August 15, 2007 (9:00:00 AM)

Share    Print    Comments   

Managing one site on a Web server can be tough enough, and the job is even harder if you have to host multiple client sites on a badly configured setup. If you're running Apache, you can make things easier by setting up virtual hosts, which let you control multiple domains on one IP address, allowing you to specify URLs like http://clientsdomain.com/file_name.html instead of http://yoursite.com/hosted/clients_directory/file_name.html, and letting you forgo setting up domain forwarding with a /srv/www/htdocs/hosted/clients_directory file.

If Apache is installed correctly, you can set up virtual hosting easily by editing Apache configuration files. The main .conf files in /etc/apache2 include httpd.conf, error.conf, server-tuning.conf, and a bunch more. These files are set for read-only access for normal users; you must gain root access with the sudo or su command to change them.

Edit /etc/apache2/httpd.conf and find this line:

Include /etc/apache2/vhosts.d/*.conf

Replace it with a line that specifies a .conf file we will create and call vhost.conf:

Include /etc/apache2/vhosts.d/vhost.conf

When you've made the change, save the file. Now, again as root, create the vhost.conf by renaming the vhost.template file in /vhosts.d/:

mv vhost.template vhost.conf

Now edit the file to set it to allow multiple name-based Web sites on a single IP address. First add in the virtual host directive with NamevirtualHost *. The asterisk is a wild card character that allows any address to be set. Under that directive you can add your first virtual host:

NamevirtualHost *

<VirtualHost *>
ServerAdmin youremail@yoursite.com
DocumentRoot /srv/www/htdocs/directory_of_your_choice/
ServerAlias yourdomain.com *.yourdomain.com
<virtualHost>

The first part is the opening tag for the defined virutal host block. ServerAdmin lets you display the administrator email address when an error such as a 404 occurs. DocumentRoot is the root directory for the defined site; for example, sites I have hosted in the past all had accounts set up within /srv/www/htdocs/hosted/sitename/. Next is the ServerAlias or ServerName, which controls what names people can use to see and access a site. You could set this to a directory within the main NameVirtualHost or the domain of the site so the server can display the content associated with the domain name. The reason for the two ServerAlias entries above is that servers are often called from more than one ServerName, so you set up a ServerAlias with more than one address listed. The wild card allows for any domain request with anything in front of .yourdomain.com.

What if you want to have multiple virtual hosts set up under two different NameVirtualHost blocks, with one being the server's main IP address and the other being for the localhost? The code would look like this:

NameVirtualHost *

<VirtualHost *>
ServerAdmin admin@mixfevers.com
DocumentRoot /srv/www/htdocs/
ServerAlias mixfevers.com *.mixfevers.com
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@snapnshare.com
DocumentRoot /srv/www/htdocs/snapnshare/
ServerAlias snapnshare.com *.snapnshare.com
</VirtualHost>

<VirtualHost *>
ServerAdmin admin@boomerwebzine.com
DocumentRoot /srv/www/htdocs/~/boomer/
ServerAlias boomerwebzine.com *.boomerwebzine.com
</VirtualHost>

NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
   DocumentRoot /srv/www/htdocs/
   ServerName localhost
</VirtualHost>

Once you've saved the configuration file, you need to restart Apache as root in order for your change to take effect:

# /etc/init.d/apache2 restart

The setup above works well for a small company hosting multiple sites on one server, or for a hobbyist who designs sites and wants to host them. It allows you point your domains at a static or dynamic IP address, letting the server figure out what shows up for the domain.

Apache provides more information on setting up and maintaining different setups of virtual host in its documentation.

James Lees is a Web site developer who hopes to go to college for network engineering and Web development. He is a die-hard Linux user.

Share    Print    Comments   

Comments

on How to set up Apache virtual hosting

Note: Comments are owned by the poster. We are not responsible for their content.

Proofreading?

Posted by: Anonymous [ip: 212.247.10.231] on August 15, 2007 10:35 AM
"forwarding with a /srv/www/htdocs/hosted/clients_directory/ file" - my files usually don't end with "/"...

"create the vhost.conf by renaming the vhost.template file in /vhosts.d/:
mv vhost.conf vhost.template" - this is a little bass-ackwards....

#

How to set up Apache virtual hosting

Posted by: Anonymous [ip: 212.178.96.46] on August 15, 2007 01:52 PM
First of all, it's not advised to change that line to vhost.conf, but edit the 00_default_vhost.conf to keep the security up or even add vhost confs in the directory so they will be included automaticly. Second, /srv/www/htdocs/? Most people have that under /var. Third, it's /var/www/DOMAIN/htdocs instead of directly /var/www/htdocs/. And last but not least, the document doesn't say much about virtual hosting at all, you just pasted your config and what the terms mean, something we can all find in the apache httpd docs too.

#

Re: How to set up Apache virtual hosting

Posted by: Anonymous [ip: 209.78.90.242] on August 15, 2007 03:36 PM
Your first point is somewhat valid, but your second and third are contrived and demonstrate your lack of depth in various distribution's file structures. Perhaps the author should have prefaced his article with something about the distribution he is using like "I will be using my OpenSUSE server for this demonstration" or whatever but jumping his shit because he wasn't using whatever your favorite is, makes me dismiss your criticism as the rant of a troll.

#

Re: How to set up Apache virtual hosting

Posted by: Anonymous [ip: 24.80.34.124] on August 15, 2007 03:50 PM
"Second, /srv/www/htdocs/? Most people have that under /var"




That would depend on what distro you are using.

#

Re: How to set up Apache virtual hosting

Posted by: Anonymous [ip: 148.142.21.245] on August 15, 2007 05:11 PM
Lighten up, Francis.

#

How to set up Apache virtual hosting

Posted by: Anonymous [ip: 68.126.200.152] on August 16, 2007 12:12 AM
Cool. How would this be done in lighttpd?

#

a2ensite & a2dissite

Posted by: Anonymous [ip: 128.229.69.78] on August 18, 2007 12:01 AM
Apache2 virtual hosting has always confused. What about the ./sites-enabled/ and ./sites-available/ directories in combination with the a2ensite and a2dissite commands?



The following article on <a href="http://www.debian-administration.org/articles/101">apache upgrading</a> discusses this alternate method:

#

But what if...

Posted by: Anonymous [ip: 82.182.32.28] on August 19, 2007 11:39 PM
I have a few pointers to this.

1. Why "mv vhost.template vhost.conf". I bet the idea with the template was to be a template for single vhost files such as domain.com.conf and mydomain.com.conf etc.
This way you by issuing a ls command know what vhosts you are serving, and adding a new is just to drop a new file in dir.
No clobbering or issues with corrupt editing of a huge vhost file. The "Include /etc/apache2/vhosts.d/*.conf" does that, so no editing is required of /etc/apache2/httpd.conf.

2. I avoid editing original conf files, as httpd.conf, because it makes monitoring and upgrades a lot smoother. Most often by default there is a "Include /etc/apache2/*.conf" or "Include /etc/httpd/conf.d/*.conf" of some sort. So why not create a "server.conf" where you add your server settings for this server. After an upgrade you know what was "your settings" without diffing old files.

3. /srv/www/<DOMAIN> would be the correct place according to FHS standards (Filesystem Hirarchy Standard, http://www.pathname.com/fhs/).

4. Is not ServerName prefered for each vhost??

#

virtual host

Posted by: Anonymous [ip: 213.207.222.167] on September 02, 2007 11:00 AM
how we can set up the server with different virtual hosts???

#

How to set up Apache virtual hosting

Posted by: Anonymous [ip: 70.182.168.211] on September 06, 2007 05:27 PM
Back the bus up! What if someone were to open his httpd.conf file and find it to be blank?
Send him 127.0.0.1 with his nose bleeding?

#

Re: How to set up Apache virtual hosting

Posted by: Anonymous [ip: 203.171.121.69] on September 27, 2007 10:50 AM
I found the tutorial very useful. My only other comment would be don’t insult the trolls.

#

How to access virtual domain from client machine

Posted by: Anonymous [ip: 203.124.58.42] on October 01, 2007 03:54 AM
Hello, I have made a virtual domain in the server and try to access it from client but cannot access. I have made an entry in host file in client & server also. But prob. is still here.

Regards,
M.Sohail
sohail_is_online@yahoo.com
PHP Developer
http://www.seomaterial.com

#

How to set up Apache virtual hosting

Posted by: Anonymous [ip: 194.27.186.39] on December 17, 2007 01:57 PM
<a href="http://bul-arkadasara.blogspot.com">arkadaş ara</a> <a href="http://aspscriptler.blogspot.com">asp script indir</a> <a href="http://resim-hayvan-resimleri.blogspot.com">hayvan resimleri</a> <a href="http://aspscriptler.blogspot.com">asp indir</a> <a href="http://phpscriptler.blogspot.com">php indir</a> <a href="http://bil-kazan.blogspot.com">bil kazan</a> <a href="http://seriilan-seriilanlar.blogspot.com">seri ilanlar</a> <a href="http://asksevgiresimleri.blogspot.com">aşk sevgi resimleri</a> <a href="http://yariskazan.blogspot.com">yarış kazan</a> <a href="http://kavakyelleridizisi-izle.blogspot.com">kavak yelleri dizisi</a> <a href="http://bilgisayarguvenligi.blogspot.com">bilgisayar güvenliği</a> <a href="http://gencotvdizisi-izle.blogspot.com">genco dizisi</a> <a href="http://aspscript-indir.blogspot.com">asp script indir</a> <a href="http://avrupayakasidizisiizle.blogspot.com">avrupa yakası dizisi</a> <a href="http://ilanlarver.blogspot.com"> ilan ver</a> <a href="http://antiviruskaspersky.blogspot.com">kaspersky antivirüs</a> <a href="http://koprutvdizisi.blogspot.com">köprü dizisi</a> <a href="http://hepsibirdizisiizle.blogspot.com">hepsi bir dizisi</a> <a href="http://phpindir.blogspot.com">php indir</a> <a href="http://resim-resimler.blogspot.com">aşk sevgi resimleri</a> <a href="http://resim-resimler.blogspot.com">resimler</a> <a href="http://pusatdizisi-pusatdizisiizle.blogspot.com">pusat dizisi</a> <a href="http://sevgiliara-sevgilibul.blogspot.com">sevgili ara</a> <a href="http://yaprakdokumudizisi-izle.blogspot.com">yaprak dökümü dizisi</a> <a href="http://phpscriptindir.blogspot.com">php script indir</a> <a href="http://kurtlarvadisipusudizisiizle.blogspot.com">kurtlar vadisi pusu dizisi</a> <a href="http://bilgi-yarismasi.blogspot.com">bilgi yarışması</a>

#

How to set up Apache virtual hosting

Posted by: Anonymous [ip: 194.27.186.39] on December 17, 2007 02:23 PM
<a href="http://bul-arkadasara.blogspot.com">arkadaş ara</a> <a href="http://aspscriptler.blogspot.com">asp script indir</a> <a href="http://resim-hayvan-resimleri.blogspot.com">hayvan resimleri</a> <a href="http://aspscriptler.blogspot.com">asp indir</a> <a href="http://phpscriptler.blogspot.com">php indir</a> <a href="http://bil-kazan.blogspot.com">bil kazan</a> <a href="http://seriilan-seriilanlar.blogspot.com">seri ilanlar</a> <a href="http://asksevgiresimleri.blogspot.com">aşk sevgi resimleri</a> <a href="http://yariskazan.blogspot.com">yarış kazan</a> <a href="http://kavakyelleridizisi-izle.blogspot.com">kavak yelleri dizisi</a> <a href="http://bilgisayarguvenligi.blogspot.com">bilgisayar güvenliği</a> <a href="http://gencotvdizisi-izle.blogspot.com">genco dizisi</a> <a href="http://aspscript-indir.blogspot.com">asp script indir</a> <a href="http://avrupayakasidizisiizle.blogspot.com">avrupa yakası dizisi</a> <a href="http://ilanlarver.blogspot.com"> ilan ver</a> <a href="http://antiviruskaspersky.blogspot.com">kaspersky antivirüs</a> <a href="http://koprutvdizisi.blogspot.com">köprü dizisi</a> <a href="http://hepsibirdizisiizle.blogspot.com">hepsi bir dizisi</a> <a href="http://phpindir.blogspot.com">php indir</a> <a href="http://resim-resimler.blogspot.com">aşk sevgi resimleri</a> <a href="http://resim-resimler.blogspot.com">resimler</a> <a href="http://pusatdizisi-pusatdizisiizle.blogspot.com">pusat dizisi</a> <a href="http://sevgiliara-sevgilibul.blogspot.com">sevgili ara</a> <a href="http://yaprakdokumudizisi-izle.blogspot.com">yaprak dökümü dizisi</a> <a href="http://phpscriptindir.blogspot.com">php script indir</a> <a href="http://kurtlarvadisipusudizisiizle.blogspot.com">kurtlar vadisi pusu dizisi</a> <a href="http://bilgi-yarismasi.blogspot.com">bilgi yarışması</a>

#

forum

Posted by: Anonymous [ip: 85.106.146.29] on January 01, 2008 12:12 AM
<a href="http://www.forumgezer.com/" title="forum" target="_blank">forum</a> <a href="http://www.forumgezer.com/" title="forum" target="_blank">forum</a>


www.forumgezer.com

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya