Virtual Hosting Refresher
The Debian installation of Apache2 comes all set up do “name-based” virtual hosting right out of the box. This is perfect if you have more than one domain to host on your server, or plan to in the future.
Name-based virtual hosting is when you have multiple domains running from one IP address, and is what we will be using here. This means that the DNS A records (or CNAME records) for www.company1.com and www.company2.com can both point to the one IP address of your server, and Apache will sort out which site to serve up based on the domain that was requested. If someone points their browser at the IP address of your server, e.g. http://123.456.123.45, they will be served the first site that you have configured. Unless you change it, this will be the Apache default site as it is installed by Debian’s Apache2 package.
For more info on virtual hosting, read the Apache docs.
Getting Familiar With Apache
Setting up a name-based virtual site is easy once you understand the layout a bit. I suggest you scan the file /etc/apache2/README. It’s pretty short and clear. For the impatient, here are just the things we are going to touch to get one name-based virtual site up. I’ll use “mydomain.com” as the virtual domain in all the examples.
This directory is the document root for the web server. This is where your web sites go, this is not mandatory, and is simply good practice.
A directory you create to contain all files related to the site mydomain.com. This directory is not publicly accessible.
A directory you create to contain the publicly accessible files for mydomain.com.
An html file you create to test your configuration.
This file you create containing the Apache configuration directives to define the virtual domain mydomain.com. Each file in the sites-available directory defines a different virtual domain.
This is a symlink you create with the handy little command a2ensite mydomain.com. The presence of a symlink in the sites-enabled directory has the effect of “enabling” a site defined in the sites-available directory.
Setting Up a Web Site
First, create a subdirectory of /var/www/ to contain the site. It makes sense to name this directory after the domain that will live there.
$> mkdir /var/www/mydomain.com
Now create the subdirectory /var/www/mydomain.com/docs/. This will be the only directory that is publicly accessible.
$> mkdir /var/www/mydomain.com/docs
Now create /var/www/mydomain.com/docs/index.html, something simple like the following:
Now create /etc/apache2/sites-available/mydomain.com. We use the
ServerAdmin [email protected]
Allow from all
# Don't show indexes for directories
Now, to enable the site you use the a2ensite command (Note:use a2dissite to disable). The following creates a symlink /etc/apache2/sites-enabled/mydomain.com, which enables the site.
$> a2ensite mydomain.com
You need to reload Apache to see the site…
$> /etc/init.d/apache2 reload
If DNS is working, you should now be able to point your browser at www.mydomain.com see the index.html page you created above.