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
Sumber:
- http://www.foscode.com/apache-virtual-host-ubuntu/