Minggu, 30 Januari 2011

How To Set Up Virtual Hosts On Apache

Apache is such a well known web server that it needs no introduction to any webmaster in the world. Despite its abundant resources in development and documentation many of its features are still confusing to newcomers to using Apache web server. For example, how do you set up multiple hosts on a single instance of Apache? You may know as much as using NameVirtualHost and VirtualHost directives but it's incredibly hard to find on the web easy-to-understand step-by-step instructions on setting up those directives. So I decided to write a post to tell you how those directives work and how to achieve our goal.

While going through this tutorial if you have additional questions regarding the directives simply look it up on Apache's website at http://httpd.apache.org/docs/1.3/mod/core.html.

Let's create a real life example!
1. I own two web hosts: www.mensfashionforless.com and www.oneminuteinfo.com, and they both point to one machine whose IP is 221.148.47.99.

2. I configured the DNS for both hosts to go to 221.148.47.99 correctly. This depends on the registrar you use to register those hosts. Typically they provide you with an admin user interface to manage the relevant settings for configuring your hosts such as DNS and MX record.

3. On 221.148.47.99 machine I create one web root directory for www.mensfashionforless.com which is /home/httpd/vhosts/www.mensfashionforless.com/httpdocs/ and I create one web root directory for www.oneminuteinfo.com which is /home/httpd/vhosts/www.oneminuteinfo.com/httpdocs/.

4. Now when an HTTP request is made to www.mensfashionforless.com or www.oneminuteinfo.com it's resolved to 221.148.47.99, and Apache would use the correct web root directory for the incoming request. Any request made to www.mensfashionforless.com will be served by /home/httpd/vhosts/www.mensfashionforless.com/httpdocs/ and any request made to www.oneminuteinfo.com is served by /home/httpd/vhosts/www.oneminuteinfo.com/httpdocs/.

You should be able to do step 1, 2, 3 (if not let me know). The hard part is step 4. Essentially you can have as many hosts pointing to the same machine as you like, and Apache simply uses the requested host name as a hint to know which local directory should be used to serve that request. First let's look at how the relevant directives work. You can put these directives in the Apache configuration file (default location on most Linux machines is /etc/httpd/conf/httpd.conf) or put them in other files and have the main Apache configuration file include those files.

How NameVirtualHost and VirtualHost work
NameVirtualHost: This directive defines what host or IP and/or port are considered to be virtual hosts. Hosts/IPs not matching it will not be considered and virtual hosts and thereby not treated as such.

In our example I simply use wildcard to match any IP but limit it to port 80, as follows:

NameVirtualHost *:80
VirtualHost: This directive defines host configurations for host/IP matching some specified virtual host/IP. In our example I use wildcard to match any IP and restrict to port 80, and specify that if server name is www.mensfashionforless.com use /home/httpd/vhosts/www.mensfashionforless.com/httpdocs/ as the document root. If server name matches www.oneminuteinfo.com then use /home/httpd/vhosts/www.oneminuteinfo.com/httpdocs/ as the document root, as the following:

<VirtualHost *:80>
ServerName www.mensfashionforless.com:80
ServerAlias www.mensfashionforless.com
DocumentRoot /home/httpd/vhosts/www.mensfashionforless.com/httpdocs
# ... additional config parameter such as DirectoryIndex, CustomLog, ErrorLog, etc.
</VirtualHost>

<VirtualHost *:80>
ServerName www.oneminuteinfo.com:80
ServerAlias www.oneminuteinfo.com
DocumentRoot /home/httpd/vhosts/www.oneminuteinfo.com/httpdocs
# ... additional config parameter such as DirectoryIndex, CustomLog, ErrorLog, etc.
</VirtualHost>

You MUST use fully qualified domain name in the value of ServerName and ServerAlias! For example do NOT use mensfashionforless.com:80 as the value of ServerName! As I mentioned you may put these directives in the Apache configuration file (default location on most Linux machines is /etc/httpd/conf/httpd.conf) or put them in other files and have the main Apache configuration file include those files. Restart your Apache server and try it! Typical command to do that is /etc/init.d/httpd restart :-)

Again any questions let me know!

0 komentar:

 
support by: infomediaku.com