Search

Friday, January 21, 2011

Setup Multiple Virtual Hosts on Apahe, Linux Fedora 14

objective : setup virtual hosts for http://test1.local and http://test2.local
environment : Linux Fedora 14 with Apache 2.2.16 web server built in

Setup Fedora 14 to handle dummy hosts
Open console, login as super user. Edit /etc/hosts to define (dummy) hostnames routing list. The word 'dummy' is included because this routing list only happened in this particular machine. In windows environment this file should be located in c:/windows/system32/drivers/etc/host.
# vi /etc/hosts 
To test, open web browser and go to http://test1.local and http://test2.local. Both web page should display your current default web server page.

Setup Apache's virtual hosts
Make sure apache is loading all configuration files (ended with .conf) in etc/httpd/conf.d directory.
Open httpd.conf file and check the following statement.
# Load config files from the config directory "etc/httpd/conf.d"
Include conf.d/*.conf 
There isn't configuration file for virtual host in my machine, so create text file named vhosts.conf. In this example we edit vhosts.conf file.
# vi etc/httpd/conf.d/vhosts.conf
Type lines below :
NameVirtualHost 127.0.0.1
<Virtual Host 127.0.0.1>
DocumentRoot path/for/test1
ServerName test1.local
</Virtual Host>

<Virtual Host 127.0.0.1>
DocumentRoot path/for/test2
ServerName test2.local
</Virtual Host>
Change path/for/test1 and path/for/test2 to directories where DocumentRoot will be located, eq : /var/www/hmlt/test1.
NameVirtualHost 127.0.0.1 tells Apache that the address 127.0.0.1 is a name based virtual host so the correct entry is then matched by the ServerName of the request.


Create index.html in both DocumentRoot. Each index.html should contain unique content so both hostnames can be tested. In my case I will write <h1>test1</h1> for /var/www/html/test1/index.html and <h2>test2</h2> for the other one.
Test both hostnames by typing http://test1.local in web browser. It should show test1. If succeed type http://test2.local, in which will show test2.



-- related reading : install DNS server on Linux Fedora 14


Refs :
http://bobpeers.com/linux/apache_virtual_hosts

No comments:

Post a Comment