February 14, 2009
How-to: Configure Apache HTTP 2.2.3 Virtual Hosts on CentOS 5
This may be applicable to all Apache 2 versions.
Using one IP to create multiple virtual web sites.
In Section 3 near the end of the httpd.conf file
#NameVirtualHost *:80 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. # # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # #<VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com # DocumentRoot /www/docs/dummy-host.example.com # ServerName dummy-host.example.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common #</VirtualHost>
Example
To add the www.example1.com virtual web site:
- Back up the httpd.conf file
- Run from the command line:
# system-config-securitylevel
Check Secure WWW(HTTPS) as Trusted services
# vi /etc/httpd/conf/httpd.conf
- Uncomment #NameVirtualHost *:80 to be as:
NameVirtualHost *:80
- Copy the following container and paste it at the end of the file
<VirtualHost 127.0.0.2:80> DocumentRoot /var/www/html2 ServerName www.example1.com </VirtualHost> - Save the file
- Test the syntax
# httpd -t
# httpd -D DUMP_VHOSTS
# mkdir /var/www/html2
# cd /var/www/html2
# vi index.html
- Insert(type i) the following line:
<html><h1>Virtual Host Test</h1></html>
- Save(press <Esc> then then type :wq) the file
# chmod 705 /var/www/html2
# chcon -R -u system_u /var/www/html2/
# chcon -R -t httpd_sys_content_t /var/www/html2/
vi /etc/hosts
- Insert
127.0.0.2 www.example1.com
- Save the file
# apachectl restart
- Browse http://www.example1.com


