Apache Virtual Hosts in Ubuntu in 5 easy steps

// August 19th, 2011 // Code

avatar

In this tutorial I’ll walk you through setting up 3 Apache virtual hosts running on a single Ubuntu server. An Apache virtual host, as explained by Apache, refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be “IP-based” or “name-based”. By default, Ubuntu already has this capability enabled, so things are much easier to configure these days. Enough with the intro, this is how you do it:

We are going to create 3 sites, site1.com, site2.com and site3.com

1. Create the folders that will host your new sites. By default, Apache in Ubuntu serves from /var/www. So create these:

mkdir /var/www/site1
mkdir /var/www/site2
mkdir /var/www/site3

2. Copy the current default setting found in /etc/apache2/sites-available/default and name it the same as your new site.

cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site1
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site2
cp /etc/apache2/sites-available/default /etc/apache2/sites-available/site3

3. Edit the new config files for each site using your preferred text editor. Add the line ServerName server1 right below the ServerAdmin line and change both DocumentRoot and Directory to point to your new sites.
This is what it should look like (you’ll do exactly the same for each of your 3 new sites, repeat this step for as many new sites as you’ll be creating):

/etc/apache2/sites-available/site1

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName site1

	DocumentRoot /var/www/site1
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	</Directory>
	<Directory /var/www/site1/>
		Options -Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

4. After you have edited the config files for each of the 3 or how many virtual hosts you are creating, just tell Apache to start serving the new domains and stop serving the default:

sudo a2ensite site1
sudo a2ensite site2
sudo a2ensite site3
sudo a2dissite default

5. Now reload apache and you should be able to get to each of your new domains:

sudo /etc/init.d/apache2 reload

Congratulations, you have done it, 3 sites running off of the same Apache server. Hopefully this tutorial helped you. Thank you for stopping by and please share with others, after all, code should be free.foscode.com | because code should be free


12 Responses to “Apache Virtual Hosts in Ubuntu in 5 easy steps”

  1. avatar hugo says:

    Great easy article. I just have 2 problems. 1st I did something not here. I added ” 127.0.0.1 localhost site1 site2 site3″ to /etc/hosts . 2 when i look for “site1/” on firefox it gives me an error something about not being unable to determine IP address. However when i type site1/ on chrome the index file opens up beutifully.

  2. avatar Dan says:

    @Hugo You might want to clear firefox’s cache. It should work, especially since it works from chrome, so you know it isn’t a configuration error.

  3. avatar hugo says:

    Yep clearing firefox’s cache work. Thanks!

  4. avatar Dan says:

    @Hugo Not a problem, I’m glad it worked :-)

  5. avatar Amit says:

    Hello dude, i am newbie and dont know how exactly it works . I have followed this steps and now i want to test it but not sure how ? i have domain something like 127.123.32.232:2107(its not a localhost machine). i have created new site with name vhtest.
    can u help me, and let me know how can i test new host.
    i put something like this when i test other things 127.123.32.232:2107/abc

  6. avatar Dan says:

    @Amit Thanks for stopping by my site. The easiest way for you to achieve this would be creating an entry in your /etc/hosts file and add an entry as 127.123.32.232:2107 vhtest
    then you will be able to reach it by going to http://vhtest
    This would ensure you can reach the server although you don’t have the domain registered, but you’ll obviously only going to be able to reach it from whichever machine you have added the entry above to the /etc/hosts file.

  7. avatar amit says:

    Dan: thanks for the reply and solution. one more question , my server is 127.123.32.232:2107 and my files are located in web/ directory, my problem is i want to remove/hide “web” from url but want to point to the correct file location. i somehow managed to hide “web” from url using htacess but its not poining to correct location i mean it tries to open a file from path like this 127.123.32.232:2107/index.php, actually it should open the path from 127.123.32.232:2107/web/index.php
    I dont know if this is ur area of expertise.

  8. avatar Dan says:

    @Amit If you would like to serve 127.123.32.232:2107/web/index.php you’d have to have these entries in the sites available file for your virtualhost vhtest, then restart apache, (I’m assuming you have apache serving from /var/www)
    (Make sure to remove the double quotes from the code below)
    DocumentRoot /var/www/web
    “< " Directory /var/www/web/ ">”
    Options -Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
    “< "/Directory ">“

  9. avatar Kaylynn says:

    I’m grateful you made the post. It’s caerled the air for me.

  10. avatar Dan says:

    @Kaylynn Thanks for visiting my site, please feel free to come back. I’m glad I was able to help you.

  11. avatar w3t says:

    Really very very intresting article related to Apache Virtual Hosts

  12. [...] got most of this from here, except the hosts file editing, which seemed to be left out of every page giving instructions for [...]

Leave a Reply

*