Virtual Hosting in Apache Web Server On Debian

Posted by jun auza On 2/14/2011
When setting up web servers, most have chosen to use Linux not only because it is free, open-source, and made for networking but also because it works best with this “little” web server software that more than half of the world's websites depend upon. I'm talking about Apache, which some of the most popular GNU/Linux distributions (Debian, Ubuntu, Centos, Red Hat, Fedora, SUSE, Mandriva, etc) have included in their respective applications repositories and is pretty much packaged to work well to corresponding distros.

Note: This tutorial doesn't discuss the installation of Apache web server since I assumed that you already know how to use your package manager to install it. I'm using Debian Sid/Unstable, so if you are running Apache on any Debian derivative you can more or less follow the instructions here.

Most Linux users are satisfied with just installing the Apache web server and use these methods/configurations for accessing the websites made:

1. Default site configuration found in /etc/apache2/sites-available/default. Web site directories and files reside in /var/www/. You need root account to add, delete and modify website directories and files.
2. Activating the UserDir directive (covered in another article)

The first set up, which is the default setup in Debian, lets you access the website thru any of these addresses:

1. http://localhost
2. http://<your_computer_name_here> example http://foo_pc
3. http://127.0.0.1
4. http://<your_computer_assigned_ip_address> example http://192.168.1.2

The second set up mentioned above, UserDir directive kind of personalize websites for each user listed in the computer. Websites can be accessed by adding /~<user_name> from the list above. Like so:

1. http://localhost/~<user_name> example http://localhost/~foo
2. http://<your_computer_name_here> example http://foo_pc/~bar
3. http://127.0.0.1/~<user_name> example http://127.0.0.1/~foobar
4. http://<your_computer_assigned_ip_address>/~<user_name> example http://192.168.1.2/~barfoo
While this set up is sufficient enough to run any or all web sites you can think of, it kind of fall short of feeling that you are using the real thing. One will notice that those URLs/addresses don't look like addresses you would see in the actual World Wide Web. Even if you are in a development stage and you don't intend to open your computer for public access, it feels a little short. What about doing some website for LAN, an intranet site – you don't expect users to memorize numbers or use localhost for that will definitely discourage users from accessing your website. What if you have a lot of website to host for different reasons and content? Most modern and not so modern computers can host a lot of websites for Apache web server because they are programmed to handle more than one websites.

This article covers a tutorial on setting up your GNU/Linux Debian Sid computer to host plenty of websites with their own website address, much like those found in the real world wide web. This tutorial does not cover public access and is limited to local area network (LAN) access only. My purpose for setting this up includes the following reasons but not limited to:
* Intranet services with URLs that looks like those found in World Wide Wide (WWW)when accessed from LAN.
* Staging area of websites that looks like WWW URLs
* Setting up many different websites in your web server with their own server/website names.
In order for other computers to access your server(s)' local web sites, it is assumed that your LAN has a local DNS server where you can register all of your web sites' names.

Step 1. Decide on where you would place your web site files and directories. Although this is not mandatory, it is strongly recommended for efficient file management. I'm using /home/<user_name>/webdocs/. Replace <user_name> with your own username. All of your web sites will look like the following:

* /home/bencanlas/webdocs/site01
* /home/bencanlas/webdocs/site02
* /home/bencanlas/webdocs/site03
* /home/bencanlas/webdocs/site04
* … and so on
Step 2. Make another directory inside webdocs and call it 'logs'. This is where we will place log files. You can specify another directory for this but take note of it.

Step 3. Decide on what name to call/access your website from the browser. For this example, we will call a web site officeblogs. You can name your website any name you wish. It won't leave your local area netwok. Create a directory in /home/bencanlas/webdocs and call it officeblogs.
Step 4. Log into root mode and edit /etc/hosts. Enter the following line:

# your computer's IP address
192.168.1.2
# name of your website
officeblogs

Step 5. Make a specific Apache configuration file for this particular website. You can name the file any name but for this tutorial we will call it officeblogs.conf. Save this in your regular document file. Later, we will copy it into the Apache web server's configuration directories. You can copy the listing below or type it:

#
# officeblogs (/etc/apache2/sites-available/officeblogs.conf)
#
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName officeblogs
ServerAlias officeblogs

# Indexes + Directory Root.
DocumentRoot /home/bencanlas/webdocs/officeblogs/
<Directory /home/bencanlas/webdocs/officeblogs/>
DirectoryIndex index.php index.html index.htm
Options FollowSymLinks
AllowOverride None
Order allow,deny
allow from all
</Directory>

# Logfiles – a separate log files only for this website.
ErrorLog /home/bencanlas/webdocs/logs/officeblogs.error.log
CustomLog /home/bencanlas/webdocs/logs/officeblogs.access.log combined
</VirtualHost>

Step 6. Log into root mode again and do the following:

a. copy officeblogs.conf into /ect/apache2/sites-available.
d. Execute a2ensite officeblogs.conf to enable this site.
c. Execute apache2ctl configtest to check for syntax error.
d. Execute apache2ctl restart to restart Apache webserver and include officeblog as one of the sites it is serving.

Step 7. To test your newly configured website open up a web browser and enter the URL http://officeblogs to access this website. Of course you need to put some web pages into the officeblogs directory for your site contents.
To add another web site, go back to "Step 3".

Just a reminder, this is not the only way to configure a web server when hosting many websites. If you know an easier or more efficient method, you may share it with us via comment.

References:
* http://greatstatistics.com/
* http://www.apache.org/
* http://localhost/manual/en/vhosts/index.html – official manual for virtual hosting


This article was written by Benjamin Canlas for Tech Source.

Do you want to become a TechSource article contributor or writer? Feel free to CONTACT US.

Bookmark and Share

Related Posts:



Archive


"Action is the real measure of intelligence" ~Napoleon Hill

Twitter

Tumblr

Google +