RHEL6 -Configuring Apache Name-Based Virtual Hosts the Quick and Easy Way

Ghost_with_a_cellephone_cartoon_TVirtual Hosts allow you to serve up content for more then one website from one Apache instance. In named-based virtual hosting, multiple web sites all point back to one server with one ip address. Apache itself determines which site to serve up depening on the hostname used to reach the site.

Honestly is sounds more exciting than it is.

Note that before we get started you will need to have a DNS entry for both the domain names that you plan to use. In my case my primary webserver is my hostname and the virtual server is a CNAME.

Install Apache

First lets install and configure Apache to start at boot.

#  yum -y  install httpd && chkconfig httpd on && service httpd start

Configure Selinux

Ok lets make a directory for our virtual server under /var/www2

In order to keep things as simple as possible, I am going to configure SELinux now.  As you can see the original web directory of /var/www/ has a different context then our new directory of /var/www2

# ls -dZ /var/www
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www

# ls -dZ /var/www2
drwxr-xr-x. root root unconfined_u:object_r:var_t:s0   /var/www2

So now we must change the context for /var/www2 to match /var/www

# semanage fcontext -a -t httpd_sys_content_t ‘ /var/www2

# restorecon -Rv ‘/var/www2

Ok now thats we have done that lets create some content for our webservers

For testing purposes, I am going to create an index.html in /var/www/html  that contains the text “fatmin01.mydomain”. This will be useful for testing.

Now lets create the directory /var/www2 for our second virtual host. Inside this directory we create an index.html that contains the text “fatmin02.mydomain”.

Because of the fact that we configured SELinux first, any file of directory created under /var/www2 will inherit the SELinux context of its parent directory. What does this mean? Well in a nut shell we dont have to worry about the permissions on our new index.html that we created above.

Configure Apache

Now we etc /etc/http/conf/httpd.conf. Make sure that the following line is uncommented. Its near the bottom of the file.

NameVirtualHost *:80

Now add the two sections below. One fo each virtual server.

<VirtualHost *:80>
ServerName fatmin01.mydomain
DocumentRoot /var/www/html

<VirtualHost *:80>
ServerName fatmin02.mydomain
DocumentRoot /var/www2/html

Boom – Now restart apache and test.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s