How to install and set up virtual hosts in apache on Centos 6

A lot of people like to use Apache because of its wide spread usage and popularity. Of course with large adoption comes large support, so you are usually never left on a question if you have one. While I prefer nginx to apache, sometimes applications only support apache or only have instructions for apache based servers.

I will walk you through setting up apache and setting up virtual hosts with it, so you can host more than one domain or have subdomains. That is what virtual hosts do.


We will need to have a user with “root” privileges for this. So if you haven't made a user with root privileges yet issue the following commands to do so.


Now it will prompt you to type this users password. When you type nothing will appear, so don't fret if you don't see anything as you type.

Once you have typed your password in twice you will need to add this user to the “sudoers” group. The way I like to do this is just editing the sudoers file. Personally I hate “vi” so I always change the default editor on my servers to “nano”.

To do this, execute the following:

export EDITOR=nano && sudo -E visudo

This will change your editor to “nano” so you can always edit your /etc/sudoers file with nano instead of “vi”.

While in here, scroll down until you see the line:

## The COMMANDS section may have other options added to it.
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

..and change it to

## The COMMANDS section may have other options added to it.
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
user    ALL=(ALL)       ALL

Changing “user” to your username we just created. Now press CTRL+O and then CTRL+X

All done! We now have created a new user with root privileges. Now log out of “root” and log in using your new username and password.

The reason we had to make a new user, is because running web apps and programs accessible by the net, using the root user, is a security risk, and a very serious one at that. So we have to practice due diligence.

Now we can install Apache if you don't already have it installed. Execute the following:

sudo yum install httpd

This will install apache for you.

Now we can mov on to the thicker, fun stuff.

Creating our working directory

When we are creating our virtual host, we need to first create a directory that we can keep all of the new website's information in.

We are going to set this up in the Apache virtual configuration file as our Document Root soon. By adding “-p” to the line of our command, it will automatically generate all of the parents for our new directory.

sudo mkdir -p /var/www/

You will need to set a FQDN (Fully Qualified Domain Name) or an IP address. For our testing purposes we will use “”.

Granting proper permissions

Having the proper permissions set on any file is important. It is important because allowing a file to have too high of access can lead to issues and could compromise the integrity of your systems security. So let's get started!

Ownership of the directory needs to be granted to us. Incase you didn’t install everything with the new user we made and did it all within root. We will need to change that.

sudo chown -R user:user /var/www/

Now we need to grant proper permissions to our files so they are visible on our website:.

sudo chmod 755 /var/www

That should do it for the permissions. But please always remember that even though we are setting up a basic installation here, some web apps and other applications tend to need more attention when it comes to the permissions. So be weary and don't be afraid to google for help if you need, or are uncertain of something.

Creating our first web page

We need to create a test page to place in our config directory. This will be html, as we didn’t install php yet. That will be for another tutorial.


sudo nano /var/www/

Now you will be in the nano editor with the text “New File” near the bottom.

Here, you can type whatever you want, it is just a test so I usually type something like:

     <h1> YAY! IT WORKS!!</h1>
           Alright, now let the fun begin

So do something along those lines and then hit CTRL+O and then CTRL+X to save and exit.

Moving on…

Turn on Virtual Hosting

We need to edit the apache configuration file this time. Execute:

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

Scroll down and make sure your text matches the ones below:

Listen 80

Scroll down to the very bottom of the document we are in, to the section called “Virtual Hosts”. It will look something like this:

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.

# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
<VirtualHost *:80>
     DocumentRoot /var/www/
     ErrorLog /var/www/
     CustomLog /var/www/

The main thing you want to concentrate on are the lines that have NameVirtualHost, Virtual Host, Document Root and Server Name in them. We will go through them one at a time.

- Uncomment the line with NameVirtualHost in it, without changing anything. The star by the “:80” just means that any IP address going through port 80 will be a Virtual Host. In the future if you see the asterisk followed by something, it most likely means “all” or “everything”.

- Since we are only using one IP address on our server this is not a problem. You could change the asterisk to your IP, but it really doesn't matter.

- Next, you can leave the rest commented (the “#” sign) until you get to <VirtualHost *:80> . Uncomment everything there.

- You can leave the <VirtualHost *:80> the way it is. Its details must match those in the “NameVirtual Host” section.

- Document root is what we are after. For this, write in the directory we made in the beginning (/var/www/ If the Document Root is incorrect or not there, you will not be successful in creating the Virtual Host.

- Server Name is important as well, in it lies the host's domain ( Make sure the domain name is fully spelled out, including the “www” for this part.

- ServerAlias is the new addition to the configuration file that is not defaulted. Adding it will give you the ability to list a few variants of our domain name, such as “” without the “www”.

The rest of the lines here in this section are not required to start a virtual host. But if you would still like to learn as to what they do, then read along.

- Server admin is for the webmasters email. A lot of hosting providers and even domain providers will give you some type of free email for your domain. If not it is not too difficult to set up your own mail-server.

- The error logs and custom logs keep a record of problems and errors on the server.

Now make sure that <VirtualHost> is not commented, and hit CTRL+O and then CTRL+X to save and exit.

Rebooting apache and finalizing

Most of our changes are complete, but they will not take effect until apache is restarted.

sudo apachectl -k stop

That will shut apache down


sudo /etc/init.d/httpd start

You can also use the “service command”

service httpd restart

Testing our new host

Once everything is set up and apache is restarted, lets give it a test run. In your browser of choice type your IP address, or your domain name.

If you see the index.html files text we set earlier, then you are victorious!

Now pat yourself on the back!


To add more virtual hosts, for new domains, subdomains or whatever else, you can just re-do the process above, creating a new document root for this domain each time, and if it is a sub-domain, then you can make it inside your main domains document root if you want, or you can give it a whole new directory. Then once you have the document root done, just copy and paste the Virtual Host info into the Apache configuration. Here is a dummy one you can use for this.

<VirtualHost *:80>
     DocumentRoot /var/www/
     ErrorLog /etc/var/www/
     CustomLog /var/www/
<VirtualHost *:80>
     DocumentRoot /var/www/
     ErrorLog /var/www/
     CustomLog /var/www/

Have fun!

How To | Reference

QR Code
QR Code how_to_install_apache_and_set_up_virtual_hosting (generated for current page)