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:

  1. Back up the httpd.conf file
  2. Run from the command line:
    # system-config-securitylevel
  3. Check Secure WWW(HTTPS) as Trusted services
  4. # vi /etc/httpd/conf/httpd.conf
  5. Uncomment #NameVirtualHost *:80 to be as:
    NameVirtualHost *:80
  6. 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>
  7. Save the file
  8. Test the syntax
    # httpd -t
    # httpd -D DUMP_VHOSTS
  9. # mkdir /var/www/html2
  10. # cd /var/www/html2
  11. # vi index.html
  12. Insert(type i) the following line:
    <html><h1>Virtual Host Test</h1></html>
  13. Save(press <Esc> then then type :wq) the file
  14. # chmod 705 /var/www/html2
  15. # chcon -R -u system_u /var/www/html2/
  16. # chcon -R -t httpd_sys_content_t /var/www/html2/
  17.  vi /etc/hosts
  18. Insert
    127.0.0.2 www.example1.com
  19. Save the file
  20. # apachectl restart
  21. Browse http://www.example1.com
RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302) (Certification Press)

Spread the word

del.icio.us Digg Furl Reddit Help

Permalink • Print

Related Entries

Made with WordPress and a healthy dose of Semiologic • Sky Gold skin by Denis de Bernardy