Monday, 13 April 2015

HTTP (httpd) Virtual Host Configuration in RHEL7/CentOS7


Virtual Hosts Configuration in Apache allows you to configure multiple web sites on a single IP Address.

In this tutorial, we will configure 2 web sites 'server2.mycompany.com' and 'www.mycompany.com' on a single IP Address.

Consider the following scenario:

HTTP Server IP Address: 192.168.122.3
                      Host Name: server2.mycompany.com

Server Configuration:

1)  Install httpd
[batul@server2 ~]$ sudo yum -y install httpd

2) Edit the file '/etc/httpd/conf/httpd.conf'.

                  ServerName server2.mycompany.com


3) Make Virtual Host Config file
     3.1) Copy sample file. 
[batul@server2 ~]$ cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d

     3.2) edit the file '/etc/httpd/conf.d/httpd-vhosts.conf'. Comment all the existing lines and  add the following.
             <VirtualHost *:80>
                 ServerName server2.mycompany.com  
                 DocumentRoot "/var/mysite"
           <Virtual Host>

             <VirtualHost *:80>
                 ServerName www.mycompany.com  
                 DocumentRoot "/var/publicsite"
           <Virtual Host>   

           <Directory "/var/mysite">
                  Require all granted 
           </Directory>           

           <Directory "/var/publicsite">
                  Require all granted 
           </Directory>           



4) Make directory for virtual hosts and change SELinux file label.
[batul@server2 ~]$ sudo mkdir -p /var/mysite
[batul@server2 ~]$ sudo chcon -R -t httpd_sys_content_t /var/mysite

[batul@server2 ~]$ sudo mkdir -p /var/publicsite
[batul@server2 ~]$ sudo chcon -R -t httpd_sys_content_t /var/publicsite


5) Create HTML files for both virtual hosts.
[batul@server2 ~]$ sudoedit /var/mysite/index.html

             <html>
             <body>
              <h1> Hello World  , mysite</h1>
              </body>
           </html>



[batul@server2 ~]$ sudoedit /var/publicsite/index.html

             <html>
             <body>
              <h1> Hello World  , publicsite</h1>
              </body>
           </html>


6) Check config file syntax errors
[batul@server2 ~]$ sudo httpd -t
[batul@server2 ~]$ sudo httpd -D DUMP_VHOSTS

7) Open HTTP (Port 80) in firewall
[batul@server2 ~]$ sudo firewall-cmd --zone=public --add-service=http --permanent
[batul@server2 ~]$ sudo firewall-cmd --reload

8)  Start httpd
[batul@server2 ~]$ sudo systemctl start httpd

9)  Enable on boot
[batul@server2 ~]$ sudo systemctl enable httpd


Client Configuration:

1)  Make entry in '/etc/hosts' if DNS Server is not configured

192.168.122.3        server2.mycompany.com www.mycompany.com

 2)  Install command-line browser 'elinks'
[shabbir@meru ~]$ yum -y install elinks

3) Browse web site
[shabbir@meru ~]$ elinks http://server2.mycompany.com

[shabbir@meru ~]$ elinks http://www.mycompany.com

No comments:

Post a Comment