Name-Based Virtual Host in Apache
The term Virtual Host refers to the practice of running more than one web site on a single machine. Virtual hosts can be “IP-based”, meaning that you have a different IP address for every web site, or “Name-based”, meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
To use name-based Virtual Host come through the next steps:
- Click Config button for Apache server in your environment.
Navigate to the /etc/httpd/conf/httpd.conf file.
Before you actually start configuring your virtual host make sure you have the NameVirtualHosts line uncommented:
Then find < VirtualHost > block. You should create < VirtualHost > block for each different host that you would like to serve. Note, that the argument to the < VirtualHost > directive must match a defined NameVirtualHost directive (in our case it is *:80). Inside each < VirtualHost > block, you will need at minimum 2 directives:
- ServerName directive to uniquely identify a virtual host. Preferably enter here the main hostname of your site.
- DocumentRoot directive to show where in the filesystem the content for that host lives.
If many servers want to be accessible by more than one name, you can use the ServerAlias directive, placed inside the < VirtualHost > section. You can enter there all the names which people can use to see your web site. Entering this names you can also use “*” and “?” wildcards characters.
Finally, you can fine-tune the configuration of the virtual hosts by placing other directives inside the < VirtualHost > containers. Most directives can be placed in these containers and will then change the configuration only of the relevant virtual host.
To find out if a particular directive is allowed follow the link. Configuration directives set in the main server context (outside any < VirtualHost > container) will be used only if they are not overridden by the virtual host settings.