Open Source Software and Linux

Sep 8 2008   1:00AM GMT

Create an Apache Virtual Host

John Little Profile: Xjlittle


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 #email address on error pages
DocumentRoot /var/www/vhosts/luvlinux #where apache looks for web site documents
ServerName #dns name of server (Web site host name)
ErrorLog logs/ #logs are located relative to serverroot
CustomLog logs/ 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)

DocumentRoot /var/www/vhosts/
ErrorLog logs/
CustomLog logs/ common

Options Indexes Multiviews

Use the following command to check your virtual host configuration:

You will get output similar to the following indicating that everything is ok.

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443 (/etc/httpd/conf.d/ssl.conf:82)
*:80 is a NameVirtualHost
default server (/etc/httpd/conf/httpd.conf:993)
port 80 namevhost (/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!


 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: