Now that I have Ubuntu installed and the Apache server working, I need to configure some Virtual Hosts. I have several names registered at dyndns.com 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 example1.dnsalias.com and example2.dynalias.com to my IP via dyndns.com, 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/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
Almost done. Now after the
ServerAdmin line add a new line:
ServerName example1.dynalias.com and again with
example2 file. Use whatever actual domain names you’ve registered with dyndns.com 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 –
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
example2 directories. Now if you enter
http://example1.dnsalias.com 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.
- Create directories to host the content of a new virtual site.
- Make a copy of the
/etc/apache2/sites-available/defaultsfile with a site appropriate name.
- Change the two
/var/www/entries to the actual directory of the new site.
- Add the
ServerNameline with the real domain name you will be hosting.
a2ensiteto 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 dyndns.com 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
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.