Adding Virtual Hosts to Ubuntu Apache

Now that I have Ubuntu installed and the Apache server working, I need to configure some Virtual Hosts. I have several names registered at that all point to the same IP. Right after you get Apache going, it defaults to serving from a default location, in this case /var/www. Any http requests coming into the box are all routed to that directory.

But Apache can support multiple virtual hosts. So if I route say and to my IP via, I want them to be each served from their own directory. The default /var/www has system ownership and would need to be changed. So I’m going to create a directory for each site under my user directory. So I’ll create /home/bruce/sites/example1 and /home/bruce/sites/example2 to host the files for each site.

Now we need to make Apache use them. In the default configuration, Apache is already set up to support multiple virtual Hosts. In order to get the new sites working, we need to create a couple of configuration files in the /etc/apache2/sites-available directory, then enable them, and then restart Apache.

If you look in the sites-available, you’ll find a file called default. This represents the default Apache serving configuration. Copy this file twice to serve as a basis for the new configuration files. sudo cp default example1 and sudo cp default example2. You need to make two or three changes to each of these files, and add one line. There’s a line in the file tagged ServerAdmin. If you want, you can make the value a real e-mail address. There are also two lines labeled Document Root and another in a %lt;Directory> statement that have the path to the directory being served – in this case, it should be /var/www/. Change both of those to the directory that will contain the new site files – /home/bruce/sites/example1/ and again with example2.

Almost done. Now after the ServerAdmin line add a new line:
ServerName and again with example2 file. Use whatever actual domain names you’ve registered with or other dynamic name server you’re using.

So now in addition to the /etc/apache2/sites-available/default file, you’ll have two more files – example1 and example2, which have been modified to point to the actual hosting directories, and with the appropriate domain names.

Now we have to tell Apache to serve the new domains. Ubuntu has a couple of convenience commands to enable and disable sites. So, to enable the new sites, enter sudo a2ensite example1 and again sudo a2ensite example2. These commands copy the configuration files you created into the /etc/apache2/sites-enabled directory. We’re almost there.

Finally, you have to restart Apache2 so that it reconfigures to serve the new sites. Simply enter sudo /etc/init.d/apache2 reload.

Be sure there’s an index.html file with some content in each of the
/home/username/sites/example1 and example2 directories. Now if you enter to a browser, you should see the contents of the appropriate file.

I hope I’ve made these steps pretty clear. It really isn’t that hard.

  1. Create directories to host the content of a new virtual site.
  2. Make a copy of the /etc/apache2/sites-available/defaults file with a site appropriate name.
  3. Change the two /var/www/ entries to the actual directory of the new site.
  4. Add the ServerName line with the real domain name you will be hosting.
  5. Use a2ensite to enable the new site, and finally reload Apache2 so it knows about the new site

[Update 4/19/2010] – Don’t know exactly, but these directions seem to not work if you have more than one virtual site plus the default site. I could only get one specific virtual site to work when the “default” site definition was also there. But, if I a2dissite default then all the other sites I specified and a2ensite enabled work. Don’t quite get it, but I guess that’s ok  because a default site isn’t all that useful unless it is your only site.

You can host as many sites as you want, but the free version of only allows up to five names. And if you don’t create a new site configuration file for a domain directed to your Apache server, it will go to the default at /var/www/.

If all this works, it should just take you five or ten minutes for each new site. But it is easy to make a tiny mistake, and things then don’t work. There are plenty of other sources of information about configuring an Apache Virtual Host. I’ve tried to summarize as simply as possible, but if things go wrong, try another set of directions.

Next, we will test the MySql installation, maybe do some configuration. Then we test PHP. Finally, we’ll try to get sendmail working, although that may be the hardest thing to do, and isn’t really essential unless the apps you’re building or testing need to originate e-mail.

[Update 6/16/2011] – I’ve just configured a real, live Centos Linux on a VPS. All this stuff about creating a virtual host seems to have gotten easier, at least on the Centos implementation. Don’t know if all this stuff is still needed on Ubuntu or not, but on Centos, all the servers are defined in the single file: /etc/httpd/conf/httpd.conf. Much easier.

About brucewampler

I've had a long and varied careen in the computer industry. I taught computer science at the University of New Mexico for nearly 15 years. I founded two successful computer software companies. I wrote the first spelling checker and first grammar checkers for personal computers. I've lived in Glenwood Springs for 10 years. My wife, Trina, grew up here, and I've been coming to Glenwood since 1977. My kids have attended the local public schools, and have had a great experience. My son, Van, will be attending the University of Colorado starting in the fall of 2007, and my daughter is a Sophomore at Glenwood Springs High School. I've been on the Roaring Fork School District Board of Education for almost 5 years. Working with our kids, teachers, schools, principals, administrators, and the Roaring Fork community has been one of the most rewarding experiences in my life.
This entry was posted in LAMP. Bookmark the permalink.

13 Responses to Adding Virtual Hosts to Ubuntu Apache

  1. bertjerred says:

    Just what I needed! Thank you for such a concise explanation.

  2. Richard says:

    Thanks, this was very helpful and well explained.

  3. Nakh says:

    Thanks. Really helpful.

    One point to add though – after making all these changes I think you will need to add to /etc/hosts.conf

  4. Bernard says:

    Thanks a lot…very good example

  5. Tom says:

    Fantastic step by step guide. First one I’ve seen that worked straight away.

  6. vivek says:

    I have another site in sites-available “mysite”. (Like you just tutored)
    Then I a2dissite default reloaded apache, a2ensite mysite, & then reloaded apache2.
    I get success messages at each step.
    But when i open localhost, index.html of default opens.

    Any help is greatly appreciated…


  7. dfsq says:

    Steps are really simple but I wrote simple script to automate the process.


    # Check if user is root
    if [ $UID -ne $ROOT_UID ]
    then echo “You must be root to run this script.”
    exit $NOTROOT

    # New host name
    read -p “Enter new host name: ” newhost

    # 1. Create new host directory
    mkdir -p -v $WWW_ROOT$newhost”/www”
    chmod -R 777 $WWW_ROOT$newhost

    # 2. Make a copy of the new host configuration file
    cp $VHOST_AVAILABLE”default” $VHOST_AVAILABLE$newhost

    # 3. Write host path in the file
    sed -i “s/${_s1}/${_r1}/g” $VHOST_AVAILABLE$newhost

    # Error log path
    sed -i “s/${_s2}/${_r2}/g” $VHOST_AVAILABLE$newhost

    # Server name
    _r3=”ServerName $newhost\n\tDocumentRoot”
    sed -i “s/DocumentRoot/${_r3}/g” $VHOST_AVAILABLE$newhost

    # 4. Enable new host
    a2ensite $newhost

    # 5. Update host file
    echo -e “$HOST_IP\t$newhost” >> /etc/hosts

    # 6. Restart apache
    /etc/init.d/apache2 reload

    echo “Host $newhost created.”


  8. Matt says:

    Very clear instructions and very good explanation of the steps. I’ve used this article twice so far when I forgot the steps. Awesome.

  9. DustinA says:

    A minor nitpick: the a2ensite command doesn’t copy the file to the “available” directory, but rather creates a symbolic link. It’s a fairly major distinction because if you think you’ve made a copy, you might think you can edit one file without affecting the other.

  10. Sundar says:

    This is a useful article. I was able to get this to work locally. I access by typing the domain names in the browser on any other machine in the LAN (after editing the local /etc/hosts file). However, when I attempt to do port forwarding through the router (so I can access these sites from outside), it goes to the default directory. I am using registered domain names (same names used in the internal sites-available directory config files) and domain forwarding from GoDaddy. I am assuming that the domain name gets transmitted as part of the http connection request and the Apache server should be able to resolve to the correct directory, just as in an internal LAN request. However, this is not happening. I am missing a step somewhere. What is it?

  11. Pingback: Making the change from and Windows to php and Linux | KimIT

  12. AnthonyR says:

    This was the best tutorial that I seen on this subject-You did a good job, works perfectly and easily. Just a point to note, that if you are doing virtual hosting by IP instead of name.After you get IP aliasing setup use this tutorial and then, simply change the to

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 )

Google photo

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

Connecting to %s