AmazonApache 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 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 directive 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.chtoen.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.chtoen.com which is /home/httpd/vhosts/www.chtoen.com/httpdocs/.
4. When an HTTP request is made to www.mensfashionforless.com or www.chtoen.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.chtoen.com is served by /home/httpd/vhosts/www.chtoen.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 distributions is /etc/httpd/conf/httpd.conf) or put them in other files and have the main Apache configuration file include those files.
How NameVirtualHost Directive Works!
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 virtual hosts and therefore 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
How VirtualHost Directive Works!
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.chtoen.com then use /home/httpd/vhosts/www.chtoen.com/httpdocs/ as the document root. The following is the complete configuration script:
NameVirtualHost *:80
<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.chtoen.com:80
ServerAlias www.chtoen.com
DocumentRoot /home/httpd/vhosts/www.chtoen.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!
One Minute Information - by
Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael
ADVERTISING WITH US - Direct your advertising requests to Michael