Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to setup vhosts in apache
#1

after a lot of trial and error and valuable help on IRC I finally got vhosts to work in apache !

 

first of all, I am assuming you have compiled apache as per this apache how-to

 

secondly, the examples below are for a system that is based behind a NAT so local IP addresses (LAN) are used rather than domain names throughout, so if your apache server is NOT behind a NAT, then obviously replace the 100.0.0.3 IP address with your domain names or if you want, the WAN ip address.

 

ok...

 

let's say that for this example, that your server name is www.linux-noob.com and you want to have a vhosts called anyweb.kicks-ass.net and anyweb.homedns.org (heh, yup these are vhosts i have setup, and this is a WORKING example).

 

I am also assuming that you have already setup the DNS for the domains above to point to the WAN ip of your apache server,

 

if your apache server is behind a NAT then it will have a LOCAL ip address like 192.168.0.3 or something, if not, it will have the WAN ip address. In my case, my apache server is behind a NAT and gets a local IP from the firewall, the local ip for my apache server is 100.0.0.3 (you'll need to know that later).

 

so, lets open our apache conf file, scroll right down to the end of the file to section 3 shown below.

 



Code:
vi /usr/local/apache/conf/httpd.conf




 

Quote:### Section 3: Virtual Hosts#

# VirtualHost: If you want to maintain multiple domains/hostnames on your

# machine you can setup VirtualHost containers for them. Most configurations

# use only name-based virtual hosts so the server doesn't need to worry about

# IP addresses. This is indicated by the asterisks in the directives below.

#

# Please see the documentation at <URL:http://www.apache.org/docs/vhosts/>

# for further details before you try to setup virtual hosts.

#

# You may use the command line option '-S' to verify your virtual host

# configuration.

 

#

# Use name-based virtual hosting.

#

#NameVirtualHost *:80

 

#

# VirtualHost example:

# Almost any Apache directive may go into a VirtualHost container.

# The first VirtualHost section is used for requests without a known

# server name.

#

#<VirtualHost *:80>

#    ServerAdmin webmaster@dummy-host.example.com

#    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>
 

ok, now lets add the VHOST sections, please note, that once you add a VHOST, even just one, and if your servername is www.linux-noob.com then after you have added the VHOST, apache will default to the VHOST server name, this is important to note and because of this you must also add your 'current' servername as the 'first' VHOST.

 

 

 



Code:
NameVirtualHost 100.0.0.3:80

<VirtualHost 100.0.0.3:80>
ServerAlias www.linux-noob.com
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/htdocs/
ServerName linux-noob.com
</VirtualHost>

<VirtualHost 100.0.0.3:80>
# ServerAlias anyweb.kicks-ass.net
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/websites/kicks-ass/
ServerName anyweb.kicks-ass.net
</VirtualHost>

<VirtualHost 100.0.0.3:80>
# ServerAlias anyweb.homedns.org
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/websites/homedns/
ServerName anyweb.homedns.org
</VirtualHost>




 

Ok in the above CODE, I have 3 VHOSTS listed, the first is my 'real' servername www.linux-noob.com, the second and third, are my actual 'virtual domain names'.

 

If we take the third example, anyweb.homedns.org and examine the lines, we see that the first line is

 



Code:
<VirtualHost 100.0.0.3:80>


.

 

That tells apache that we have a virtual host on the following ip address 100.0.0.3 and it's on port 80.

 

Lines with # infront are commented out and ignored.

 

The next line that interests us is this one

 



Code:
DocumentRoot /usr/local/apache/websites/homedns/




 

This line tells apache that the virtual host called anyweb.homedns.org is situated locally in the directory /usr/local/apache/websites/homedns/.

 

Now, we have to tell apache what this virtual server 'domain name' is actually called,

 



Code:
ServerName anyweb.homedns.org




 

the line above, tells apache to look for any requests to anyweb.homedns.org and point them to the DocumentRoot we specified above that.

 

That's it, pretty much done, now just close the section with this

 



Code:
</VirtualHost>




 

so that your new VHOST section now reads something like

 



Code:
<VirtualHost 100.0.0.3:80>
# ServerAlias anyweb.homedns.org
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/websites/homedns/
ServerName anyweb.homedns.org
</VirtualHost>




 

That in itself should work provided that you do indeed have a domain name called anyweb.homedns.org pointing to your WAN (internet) ip address (not the local IP address) and that you have created a directory locally called /usr/local/apache/websites/homedns/ and that you have chmod 755 that directory and chmod 644 any files in that directory.

 

If it doesn't work make sure that your BindAddress sedtion in httpd.conf is correctly pointing to your LOCAL ip (or WAN ip if no NAT involved) such as in my example

 



Code:
BindAddress 100.0.0.3




 

ok to finish off and test your VHOST you should copy/paste your REAL server name details above the virtual host code above and make a minor change so that it looks like this:

 



Code:
NameVirtualHost 100.0.0.3:80

<VirtualHost 100.0.0.3:80>
ServerAlias www.linux-noob.com
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/htdocs/
ServerName linux-noob.com
</VirtualHost>

<VirtualHost 100.0.0.3:80>
# ServerAlias anyweb.homedns.org
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/websites/homedns/
ServerName anyweb.homedns.org
</VirtualHost>




 

In the Code above, I have listed www.linux-noob as the FIRST vhost (and it is the 'default' website). If you do not list your default website in the VHOSTS section then all requests to it will fail.

 

Also note that the line

 



Code:
NameVirtualHost 100.0.0.3:80




 

appears above the FIRST VHOST. This instructs apache to understand that it's using named virtual hosts, and they can be IP address (local or WAN) or DOMAIN NAMES (www.linux-noob.com).

 

Whatever you have in NameVirtualHost must correspond with what you have after the VirtualHost statement, in my case above, it is 100.0.0.3:80.

 

Also to note, in the first VHOST i have listed (the default website/linux-noob.com) i use the following line

 



Code:
ServerAlias www.linux-noob.com




 

which instructs apache to give the server alias = what you specify. This is only required ONCE in the VHOST and it should generally be the 'default' website that you are hosting.

 

ok, still with me ? if so, now you want to test that it all worked, save your httpd.conf file and stop and then restart apache by doing as follows:

 



Code:
[root@www root]# /usr/local/apache/bin/apachectl stop
[root@www root]# /usr/local/apache/bin/apachectl start




 

That's it, now test your VHOSTS !

 

here's three i've done already ;-)

 

[/url]https://www.linux-noob.com

http://anyweb.kicks-ass.net

[url=http://anyweb.homedns.org]http://anyweb.homedns.org

 

cheers

 

anyweb

Reply
#2

Don't forgot about your virtual host's access log files! Its a good idea to keep all of your virtual host logs seperated.

This can easily be done by adding "CustomLog /path/filename" to your Vhost entry.

 

In my Fedora Core 3 setup, my apache log files are located /var/log/httpd/. I'll use that path for the example below.

 



Code:
NameVirtualHost 100.0.0.3:80

<VirtualHost 100.0.0.3:80>
ServerAlias www.linux-noob.com
ServerAdmin anyweb@linux-noob.com
DocumentRoot /usr/local/apache/htdocs/
ServerName linux-noob.com

CustomLog /var/log/httpd/www.linux-noob.com_accessLog combined

</VirtualHost>




 

I *think* the default httpd.conf setup will allow the CustomLog syntax to work.

Hope this helps...

Reply
#3

Virtual hosting with Apache is neat. :) I admin a box that hosts 25-30 sites and keeping the logs stored on a per-host basis works out very well, especially when providing web log analysis on a per site basis.

 

"Mass Virutal Hosting" is also neat, if you need to host sites without any special requirements. With "Mass Virtual Hosting", you can define sites by uploading the site to the server in a particular spot, registering the domain, and setting up the IP address assignment. No Apache restart required! :)

 

This is a great thread. :)

 

Peace...

Reply
#4

Nice post anyweb,

 

I also noticed you did not mention SELinux :)this make security even more secure.

it can clamp down on people trying to abuse your CGI scripts and inject system commands

in. if a user did successfully inject a command they would only be able to see the web dir

and not browse directorys like /etc/.

 

Have a look at SELinux with apache

Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)