CentOS - Apache Virtual Hosts #1

Now we have Apache installed and running, we can configure it to serve multiple domains using Virtual Hosts.

Do note the layout used in these articles is explained here - feel free to use the directories of your choice.


Create the Directory Layout

In this example we'll be creating two domains, domain1.com and domain2.com

As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Apache access to this folder as well:

chmod 755 /home/demo

OK, now we need to create the directory structure for our sites.

In your home directory create a 'public_html' folder:

cd ~
mkdir public_html

Now, for each domain we want to host, create a folder with a standard set of sub-folders:

mkdir -p public_html/domain1.com/{public,private,log,cgi-bin,backup}

and

mkdir -p public_html/domain2.com/{public,private,log,cgi-bin,backup}

That will create the folders public, private, log, cgi-bin and backup for each of our domains (domain1.com and domain2.com).

index.html

The content of the public folder is, entirely, up to you but for this example I am going to use a very simple HTML file so we can check that the virtual hosts work correctly:

For each domain let's create the index.html file:

nano public_html/domain1.com/public/index.html

add the following to the file:

<html>
  <head>
    <title>domain1.com</title>
  </head>
  <body>
    <h1>domain1.com</h1>
  </body>
</html>

Repeat the process so you have a similar file for domain2.com (simply replace all instances of 'domain1.com' with 'domain2.com).

Now that we have a basic structure for our two domains we can look at defining the two virtual hosts.

NameVirtualHosts

With the virtual hosts, one thing to note that often catches people off guard is the NameVirtualHost setting.

For each port that Apache listens to, we need to define a NameVirtualHost. The issue that people sometimes overlook lies in the fact that you can only define it once per port.

Be careful that you do not add the same NameVirtualHost twice as adding another one will cause warnings and errors.

Let's go ahead and uncomment the generic NameVirtualHost in the Apache configuration.

Navigate to the /etc/httpd/conf directory and open the main Apache configuration file (httpd.conf):

sudo nano httpd.conf

Towards the bottom of this file you will want to uncomment out the generic NameVirtualHost as follows:

# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.

Now we can restart Apache to initiate the changes:

sudo /etc/init.d/httpd restart

The following warning will be displayed as we still need to add our VirtualHosts

Stopping httpd:                                            [  OK  ]
Starting httpd: [Thu Dec 11 02:06:13 2008] [warn] NameVirtualHost *:80 has no VirtualHosts
                                                           [  OK  ]

Please keep in mind that this is only a warning and will not appear once we complete the following section.

Let's move on.

Custom Virtual Hosts

We've setup the basics and now we're ready to add our own virtual hosts so that we can start to serve our domains.

Let's create the vhost for domain1:

sudo nano /etc/httpd/conf/httpd.conf

At the bottom of the httpd.conf file, we need to add the following:

# Place any notes or comments you have here
# It will make any customization easier to understand in the weeks to come

# domain: domain1.com
# public: /home/demo/public_html/domain1.com/

<VirtualHost *:80>

  # Admin email, Server Name (domain name) and any aliases
  ServerAdmin webmaster@domain1.com
  ServerName  domain1.com
  ServerAlias www.domain1.com


  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html
  DocumentRoot /home/demo/public_html/domain1.com/public


  # Custom log file locations
  LogLevel warn
  ErrorLog  /home/demo/public_html/domain1.com/log/error.log
  CustomLog /home/demo/public_html/domain1.com/log/access.log combined

</VirtualHost>

OK good, now we need to reload Apache:

sudo /etc/init.d/httpd reload

Navigate

Now navigate to your site:

http://domain1.com

You should now see the contents of public/index.html being shown:

Domain1 Home Page

ServerAlias

Note that in the vhost file, we set a ServerAlias. Providing you have the DNS set up correctly you can also use that address:

http://www.domain1.com

Repeat as necessary

To create and enable domain2.com simply go through the process again:

sudo nano /etc/httpd/conf/httpd.conf
...
# Enter the details for domain2.com as per the example shown above

Then reload Apache:

sudo /etc/init.d/httpd reload

Finally, navigate to your second domain:

http://domain2.com
or
http://www.domain2.com

All being well, you will see the 'domain2.com' index file.

Log Files

As defined in your vhost in the Apache configuration, each domain has its own log files, lets take a quick look:

ls /home/demo/public_html/domain1.com/log/

The output is exactly as expected:

access.log  error.log

This makes for much easier analysis as each set of logs is self contained.

Default Vhost

Remember that although we created a vhost for domain1.com and domain2.com, if someone enters the IP address of the Slice they are served the contents of the domain1.com vhosts.

Why are they served from that vhost?

Apache searches the enabled vhosts from the top down. Therefore, once it finds a matching vhost for the IP address entered, the contents of that domain are displayed. As we setup domain1.com initially in this example, the contents for this domain will be shown if you enter your slice's IP address in a browser.

This is something to keep in mind when planning your websites. Do you want a particular domain to be the default? Do you want the IP address to have completely different content?

If you want the IP address to have separate content than your domains, you will need to create an additional vhost and use the IP of your slice as the ServerName.

Summary

We've gone into some detail here but, overall, setting up a virtual host is relatively easy. Of course, there are many settings and configurations to take into account but you should have your site up and running in no time.

The next virtual host article will look in more detail at some of the settings that are available and what they mean.

Mark

Article Comments:

Bala Paranj commented Tue Dec 16 02:02:04 UTC 2008:

For the second domain setup does it go in its own <virtualhost> tags or is it inside the same one used for domain1.com ?

Kursat Unlukara commented Mon Feb 02 20:19:34 UTC 2009:

Hi Bala Paranj, You have to create new <virtualhost> tags for every domain you want to add to the server.

Amartya commented Mon Sep 14 19:26:08 UTC 2009:

Hi, do I need any other settings? like root derectory or any thing else. because using that domain name it is showing "this page can not be displayed" and using the IP it is showing the first domain only which is obvious.

Sivaprakash commented Tue Oct 27 09:24:38 UTC 2009:

Forbidden

You don't have permission to access /test/ on this server.

I cannot create any virtual host under root which means there is SElinux enabled on apache.. I disabled it still its not working

Any Ideas...

Please

Ben commented Sun Jan 24 18:49:50 UTC 2010:

I'm not a slicehost customer, but these tutorials are excellent and got my CentOS VPS set up perfectly. Thank you!

Greg Pederson commented Tue Aug 03 22:58:39 UTC 2010:

I followed this guide to add a subdomain and was getting this error when I navigated to the new subdomain url: You don't have permission to access / on this server.

I then checked the error.log and was seeing: client denied by server configuration:

After googling around I found a suggestion to add the following to your vhosts conf file:

<directory> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </directory>

After this an apache reload and voila, my page was now displayed.

Tanvir commented Thu Jan 06 07:30:29 UTC 2011:

Hi... I'm also agree with Amartya.. same problem i faced in my server.

Amartya commented Mon Sep 14 19:26:08 UTC 2009:

Hi, do I need any other settings? like root derectory or any thing else. because using that domain name it is showing "this page can not be displayed" and using the IP it is showing the first domain only which is obvious.

Jered commented Fri Jan 07 21:40:51 UTC 2011:

Tanvir, the settings listed should usually be enough, so long as the virtual host config is being included in the main config, the DNS is pointing to the right IP address, and all that. You might take a look at an updated version of this article in case it covers some aspect of the process that changed in a more recent version of CentOS. Otherwise you could try popping into support chat, or posting in the forums, or just posting a link to a pastebin copy of your virtual host config and I can see if I can tell what the problem might be.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)