Virtual hosting allows you to host multiple websites, accessed by unique domain names, from the same IP address (same server). I use this technique on my web server to host multiple websites for myself and a few others. Setting up virtual hosting is not difficult, and can be done with a stock Ubuntu 8.04 server installation and multiple domain names.
What you’ll need:
* Configured Server: Install and configure Ubuntu Server 8 at least to the specifications mentioned in my installation and domain name configuration tutorials. You must have command line access to the server (physical or via ssh).
* Domain Names: In order for virtual hosting to be effective, you obviously need more than one domain name. If you do not own multiple domains, you can create sub-domains (”sub.domain.com”). This can be done in your Namecheap domain manager on the Domain Registration page. You can modify the sub-domains once you create them on the All Host Records Page. When a new domain or sub-domain is added, it will take up to two days to process before it can be accessed. Note: All sub-domains will have the same Dynamic DNS password as their parent domain.
Server Configuration:
* Setup User Accounts: Each of the virtual sites that you setup will have an author with a user account on the server. These users must have access to a home directory to store their website. The user can then transfer files to and from the server via SSH/SFTP (look for a FTP server tutorial in the future). To setup these accounts, type the following into the server command line, replacing newuser with the desired username of the web author, and password with the desired initial password of the web author:
sudo useradd newuser -d /home/newuser
sudo mkdir /home/newuser
sudo passwd newuser password
sudo chown newuser /home/newuser
Repeat this for each website (with related author) you plan to add to your server. Provide the new user with the password you just created for them, and instruct them to change it when they login (SSH) by typing passwd into the server command line.
* Setup Site Directory: In my installation tutorial, we moved the web directory for the server’s default website to /home/yourusername/www. This is fine for a simple, single-website server, but since we will be setting up new virtual sites anyway, we might as well set up an organized website directory structure. Your web directory should consist of three parts, a documents folder where your pages are stored, a cgi-bin folder where your cgi scripts are stored, and a logs folder where your access and error logs are stored. Since you will have different sites with different owners, replicate this structure in the home folder of each site’s author. To do this, type the following into the server command line, replacing username with the username of the web author:
sudo mkdir /home/username/htdocs
sudo chown username /home/username/htdocs
sudo mkdir /home/username/cgi-bin
sudo chown username /home/username/cgi-bin
sudo mkdir /home/username/logs
sudo chown username /home/username/logs
Repeat this for each web author. Note: If you already have an established website for yourself in /home/yourusername/www folder such as mentioned in my installation tutorial, just rename the folder to “htdocs”. Type the following into the server command line, replacing yourusername with your username:
sudo mv /home/yourusername/www /home/yourusername/htdocs
* Enable Virtual Hosting:The first step to setting up virtual hosting is to enable virtual hosts in Apache. This is done by creating a virtual hosts configuration file. Type the following into the server command line:
sudo nano /etc/apache2/conf.d/virtual.conf
Add the following line to the new file:
NameVirtualHost *
Press ctrl+x to quit, y to save changes, then enter to confirm.
* Setup Virtual Sites: Apache makes managing multiple virtual sites easy with its modular structure. It stores each sites configuration file in the /etc/apache2/sites-available/ directory, and allows the administrator to enable or disable them individually with a single command. First, we need to disable and delete the configuration for the default site. Type the following into the server command line:
sudo a2dissite default
sudo /etc/init.d/apache2 reload
sudo rm /etc/apache2/sites-available/default
Now that the default site is gone, create a virtual site for each web author. Type the following into the server command line, replacing sub.domain.com with the [sub]domain name of your site:
sudo nano /etc/apache2/sites-available/sub.domain.com
Now paste the following into the new file, replacing the italicized portions with the appropriate values discussed previously:
#
# sub.domain.com (/etc/apache2/sites-available/sub.domain.com)
#
ServerAdmin youremailaddress
ServerName sub.domain.com
ServerAlias sub.domain.com
# Indexes + Directory Root.
DirectoryIndex index.html index.htm index.php
DocumentRoot /home/authorsusername/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/authorsusername/cgi-bin/
Options +ExecCGI
# Logfiles
ErrorLog /home/authorsusername/logs/error.log
CustomLog /home/authorsusername/logs/access.log combined
Press ctrl+x to quit, y to save changes, then enter to confirm. Repeat this step for each virtual website.
* Enable site: Now that you have setup your virtual websites, all that is left to do is enable them. Enable each virtual website, one at a time by typing the following into the server command line, replacing sub.domain.com with the names of sites you added in the previous step:
sudo a2ensite sub.domain.com
Note: you can disable a site by typing the similar command into the server command line:
sudo a2dissite sub.domain.com
That’s it! Now, just restart the Apache server and your sites should be online. Type the following into the server command line:
sudo /etc/init.d/apache2 reload
Now you can access separate websites on the same server via unique [sub]domain names. Please comment with any suggestions or additions to these instructions.
from: http://www.corey-m.com/blog/?p=315