Setting Up Apache Virtual Hosts

Articles by Bob Peers from < />


Named based virtual hosting enables Apache to serve different content (different websites) for the same IP address depending on the domain
name used. I use this on my laptop to set up development version of this website that I can change without fear of breaking the master files.
I also have 2 other development sites set up this way.

Note that my set up if purely for web development, this IS NOT a live configuration for internet access from remote users. As the server can only
be accessed from localhost this does not take security into account. If you wish to set up Apache as a live web server you would need to check
other security considerations first.

You can read the
Apache Virtual Hosts Documentation for further information. Or if you already run Apache you can view the documents on you own web server by
going to http://localhost/manual/vhosts/

Create the directory structure

First set up the directory to hold the files, I call my directory public_beta in my home folder. I normally create a new directory in my home
directory for each virtual host, but you could create sub directories under a main web folder.

$ mkdir ~/public_beta

Next give the folder and contents permissions of 755 so Apache can read the contents.

$ chmod -R 755 public_beta

Change the permissions of $HOME

Also make sure you home directory has permissions set to 711 so Apache can access your home directory, replace user_name with your login username.

$ cd /home
$ chmod 711 user_name

If you are running SELinux

By default SELinux will not serve pages from your home directory but it’s easy to change the security context of the directory to allow this without
turning off SELinux protection for the HTTPD daemon. All commands must be run as root, assuming the folder is called public_beta in your home

$ /usr/sbin/setsebool -P httpd_enable_homedirs 1
$ chcon -R -t httpd_sys_content_t public_beta

The -R option makes this recursive so all files and folders inside get the same security context.

To allow cgi scripts to run under SELinux run this command.

$ /usr/sbin/setsebool -P httpd_enable_cgi 1

If you aready have content to be added to this directory make sure you copy the content and not move it. If you copy the content it will inherit
the security context from the parent directory but if you move it then it will retain it’s original security context and you will have to
re-run the chcon command after the move is complete.

For a fuller description of SELinux and Apache see this
Fedoraproject SELinux Guide.

Create a <Directory> entry in httpd.conf for the virtual host directories.

If you don’t already have a <Directory> entry in your httpd.conf (located at /etc/httpd/conf/httpd.conf) then add someting like the
following to it.

<Directory /home/*/public_*>
AllowOverride Options FileInfo AuthConfig Limit
Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
Order allow,deny
Allow from all
Order deny,allow
Allow from all

This will cover any directory starting public_ in your home directory, if you always make directories called public_something then this cover them
all. If you need different configurations for different virtual hosts then of course you will need to make multiple entries.

Even though we are serving pages from our home directory you DO NOT need to enable the ‘UserDir’ entry in httpd.conf, mine looks like this but I
am still able to serve pages from my home directory.

<IfModule mod_userdir.c>
UserDir disable
#UserDir public_html

Create the Virtual Hosts file for Apache

Next we need to create a file to tell Apache about our virtual hosts. One place is in the main httpd.conf file located in
/etc/httpd/conf/httpd.conf, this is the file that contains all Apache’s settings so it’s not really a good place to put them. If you look at the
bottom of the httpd.conf file there is already an example entry added.

A better idea is to create a blank file in /etc/httpd/conf.d/, this directory contains data that Apache loads on startup and so in my case also
includes a file called php.conf telling Apache what modules to load and which extensions to use to serve PHP files.

To ensure the new virtual hosts file is read by Apache check your httpd.conf file has the following entry uncommented out.

# Load config files from the config directory “/etc/httpd/conf.d”.
Include conf.d/*.conf

In my case I’ll call the virtual hosts file vhosts.conf.The name is not important as long as it ends .conf. Create this file as root using
your text editor of choice, vi in my case.

$ vi /etc/httpd/conf.d/vhosts.conf

Next we need to enter the virtual hosts information, my vhosts file looks like this:


DocumentRoot /home/username/public_html

DocumentRoot /home/username/public_beta

The first line simply tells Apache that the address is a name based virtual host so the correct entry is then
matched by the ServerName of the request.

Here the entry points to my development area and points to my master files I use to upload to the website. I can make
changes to the website on my local machine in the public_beta folder and view these changes by looking at in my browser.

When I’m sure they are OK I can copy the files to my public_html folder before uploading them to my web host.

Add the ServerNames to the hosts file

In order for to point to the correct location we also need to add a new entry to our hosts file. Open the file /etc/hosts
as root and add the following line under the ‘    localhost’ line. This ensures that a request for will be directed to or localhost and your Apache server will get the request and then serve the associated virtual hosts.

Make sure you enter a carriage return (press enter) after the last entry or you hosts file may not work as expected.

I also make sure  to use top level domains (.com, .net) that do not exist. If I added pointing to localhost then requests from
my laptop to my live hosted website would be redirected to my local machine. I use .dev and .live for this reason but feel free to use anything you
wish as long as it matches the virtual host entry.

Save and close the file. Now when you start Apache and open a browser at the files in /home/username/public_beta will be
served, going to will serve the files located in /home/username/public_html.

Read more  at


Leave a Reply

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

You are commenting using your 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