Create an Apache Virtual Host
Posted by: John Little
Creating Apache virtual hosts allow you to use a single IP address for many web servers. I use mine for general web serving as well as secure WebDav over HTTPS. The Apache web server is arguably the most popular web server on the internet and has been since 1996.
Ok let’s get started setting up your Apache virtual host. I am using Centos 5.x for our operating system. The apache server is the stock install using YUM.
Edit the /etc/httpd/conf/httpd.conf file. The virtual hosts section is towards the bottom.
Uncomment the NameVirtualHost *:80 directive If you want virtual hosts accessed over SSL you will need to add the NameVirtualHost *:443 directive as well.
For virtual hosts over SSL the virtual host containers should be added to the /etc/httpd/conf.d/ssl.conf file.
Almost any Apache directive may go into a VirtualHost container. Following is a sample virtual host container. Use the auth directives if you want authentication for your host. The users are set up with the htpasswd command. See man htpasswd for more info.
When apache receives a web site request on the IP address it looks to the configuration files
to determine if it has the host. If it has the host it then looks to the DocumentRoot of the host to determine what pages and directories are available and serves them to the client.
ServerAdmin webmaster at www.luvlinux.net #email address on error pages
DocumentRoot /var/www/vhosts/luvlinux #where apache looks for web site documents
ServerName www.luvlinux.net #dns name of server (Web site host name)
ErrorLog logs/www.luvlinux.net-error_log #logs are located relative to serverroot
CustomLog logs/www.luvlinux.net-access_log common
#put options in here
Options Indexes Multiviews #shows an index of files if no index.html
# AuthType Basic #authentication type
# AuthName “My this site” #name that shows on login dialogue
# AuthUserFile /etc/httpd/webpass #name of the password file
# Require user engineer1 #name of authorized user(s)
ServerAdmin webmaster at www.example.net
DocumentRoot /var/www/vhosts/example.net
ServerName www.example.net
ErrorLog logs/www.example.net-error_log
CustomLog logs/www.example.net-access_log common
Options Indexes Multiviews
Use the following command to check your virtual host configuration:
httpd -D DUMP_VHOSTS
You will get output similar to the following indicating that everything is ok.
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443 myhost.example.com (/etc/httpd/conf.d/ssl.conf:82)
*:80 is a NameVirtualHost
default server www.example.net (/etc/httpd/conf/httpd.conf:993)
port 80 namevhost www.example.net (/etc/httpd/conf/httpd.conf:993)
Syntax OK
Use the following to check the general syntax of your configuration files:
httpd -t
Syntax OK
Congratulations! You should now have your Virtual Host setup. Don’t forget to make your hosts and/or DNS entries for accessing your web server. Enjoy!
-j


