Setup name based virtual hosts in Apache

Setting up name based virtual hosts in Apache is fairly easy. Create a new file in /etc/httpd/conf.d, which on Red Hat will be loaded automatically by /etc/httpd/conf/httpd.conf.

Using the vi editor, I will create the following file.
vi /etc/httpd/conf.d/virtualhosts.conf

The file contents:

Options Indexes FollowSymLinks
AllowOverride FileInfo
Order allow,deny
Allow from all

NameVirtualHost *:80


ServerAdmin webmaster@sample.com
DocumentRoot “/www/docs/www.sample.com”
ServerName www.sample.com
ErrorLog logs/www.sample.com-error_log
CustomLog logs/www.sample.com-access_log common


ServerAdmin webmaster@sample.com
DocumentRoot “/www/docs/www2.sample.com”
ServerName www2.sample.com
ErrorLog logs/www2.sample.com-error_log
CustomLog logs/www2.sample.com-access_log common


ServerAdmin webmaster@sample.com
DocumentRoot “/www/docs/www3.sample.com”
ServerName www3.sample.com
ErrorLog logs/www3.sample.com-error_log
CustomLog logs/www3.sample.com-access_log common

The above configuration file will create three virtual hosts in the /www/docs directory.

If you have selinux enabled, use the following command to make this directory tree readable by Apache–“-R” means recursive.

chcon -R system_u:object_r:httpd_sys_content_t /www

Verify the selinux permissions:
ls -Z /www

Leave a Reply