how to setup apache virtual host configuration
TRANSCRIPT
-
7/31/2019 How to Setup Apache Virtual Host Configuration
1/8
How To Setup Apache Virtual Host Configuration (WithExamples)
byRAMESH NATARAJAN on JULY 27, 2011
Using Apache Virtual Host, you can run several websites onthe same server.
For example, I can run both thegeekstuff.com andtop5freeware.com on a single physical server that has oneApache webserver running on it.
Fig: Apache Virtual Host (Multiple websites, one Apache)
There are two types of Apache virtual host configurations: 1)IP-Based Virtual Host and 2) Name-based Virtual Host.Name-based virtual host is recommended for most scenarios.
-
7/31/2019 How to Setup Apache Virtual Host Configuration
2/8
IP-Based Virtual Host
In this configuration, when you are pointing two websites (withdifferent ip-address) to the server that runs Apache, that
physical server should have two different ip-addressconfigured.
This means that the server should have two ethernet cards,each one of them configured to the ip-address of thecorresponding website that Apache virtual host will beserving. So, this is not practical for most aspects, and youshould not be using this.
In the following example, the server contains two NIC cards,one is configured with 192.168.101.1 ip-address forthegeekstuff.com, another is configured with 192.168.102.1for top5freeware.com. Both these ip-address are served by asingle Apache webserver running on that server using IP-Based virtual host.
-
7/31/2019 How to Setup Apache Virtual Host Configuration
3/8
Fig: Apache IP-Based Virtual Host
Name-Based Virtual Host
In this configuration, when Apache webserver receives a
request, it looks for the hostname in the HTTP header, anddepending on the hostname, it servers different websites. Thisis very easy, as you need only one ip-address on that physicalserver; but, you update the DNS with multiple website namespointing to the same ip-address. For all practical purpose,youll be using only Name-based virtual host configuration.
In the following example, the server contains only one NIC
card, which is configured with 192.168.101.1 ip-address. TheDNS entry for both thegeekstuff.com and top5freeware.comwebsite points to 192.168.101.1 ip-address. When Apacherecives a request, it looks for the hostname entry in the HTTPheader, and serves the corresponding website.
-
7/31/2019 How to Setup Apache Virtual Host Configuration
4/8
Fig: Apache Name-Based Virtual Host
1. Uncomment httpd-vhosts.conf in httpd.conf
If youveinstalled Apache 2 from source, by default, the
following line will be commented in the httpd.conf file.Uncomment this line.
# vi /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf
2. Setup virtual hosts
Modify the httpd-vhosts.conf as shown below to setup named-based virtual host setting for two hosts.
NameVirtualHost *:80 Indicates that all the name-basedvirtual hosts will be listening on the default port 80
http://www.thegeekstuff.com/2011/03/install-apache2-ssl/http://www.thegeekstuff.com/2011/03/install-apache2-ssl/http://www.thegeekstuff.com/2011/03/install-apache2-ssl/http://www.thegeekstuff.com/2011/03/install-apache2-ssl/ -
7/31/2019 How to Setup Apache Virtual Host Configuration
5/8
Enclose all the apacheconfiguration parameters for each and every virtual hostbetween these VirtualHost tags. Any apache directives canbe used within the virtualhost container.
In the following example, we are setting up virtual host forthegeekstuff.com and top5freeware.com listening on thesame port 80. So, there will be two , one for each website.
When you go to thegeekstuff.com, the files under/usr/local/apache2/docs/thegeekstuff will be served byApache; and the access_log and error_log for this site will
go under /usr/local/apache2/logs/thegeekstuff# vi /usr/local/apache2/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/docs/thegeekstuff"
ServerName thegeekstuff.com
ServerAlias www.thegeekstuff.com
ErrorLog "logs/thegeekstuff/error_log"
CustomLog "logs/thegeekstuff/access_log" common
-
7/31/2019 How to Setup Apache Virtual Host Configuration
6/8
ServerAdmin [email protected]
DocumentRoot "/usr/local/apache2/docs/top5freeware"
ServerName top5freeware.com
ServerAlias www.top5freeware.com
ErrorLog "logs/top5freeware/error_log"
CustomLog "logs/top5freeware/access_log" common
3. Check VirtualHost Configuration Syntax
Verify virtual configuration syntax using httpd -S as shownbelow. When everything is setup properly, it just displaysSyntax OK.
# /usr/local/apache2/bin/httpd -S
VirtualHost configuration:
Syntax OK
-
7/31/2019 How to Setup Apache Virtual Host Configuration
7/8
When something is not configured properly, it will displaywarning message, including directory does not exit messageas shown below.
# /usr/local/apache2/bin/httpd -S
Warning: DocumentRoot
[/usr/local/apache2/docs/top5freeware] does not exist
Warning: ErrorLog [/usr/local/apache2/logs/thegeekstuff]
does not exist
Syntax OK
4. Restart the Apache and test
# /usr/local/apache2/bin/apachectl restart
Now, when you go to thegeekstuff.com (orwww.thegeekstuff.com), the apache will serve the files from
/usr/local/apache2/docs/thegeekstuff directory.
When you go to top5freeware.com (orwww.top5freeware.com), the same apache running on thesame server will serve the files from
/usr/local/apache2/docs/top5freeware directory.
Just to reiterate, for the name-based virtual host to workproperly, the DNS entry for both these websites should bepointing to the same external ip-address of the physical serverwhere the Apache webserver is running.
-
7/31/2019 How to Setup Apache Virtual Host Configuration
8/8