How to create Multiple Virtual Host in CentOS/RHEL 5/6/7

Share It!

Using Apache Virtual Host, you can run several websites on the same server. In this article, we are going to talk about one more feature of Apache which permits us to have more than one site on a single Linux machine.

apache-logo

See Also:

  • How To Set Up Apache Virtual Hosts on CentOS 5/6/7
  • Types of Virtual Web Hosting in Apache
  • How to create Apache Virtual Host on Ubuntu
  • How to Password Protect your Website using .htaccess
  • How to Install LAMP on CentOS/RHEL 7
  • How to Install LAMP on CentOS/RHEL 5/6
  • Step #1: Create DocumentRoot

    Before creating an virtual host, you need to create document root where you will keep the new website’s information.

    # mkdir /home/techoism/virtual-one 
    # mkdir /home/techoism/virtual-two
    

    Step #2:Create the Page

    Create a sample index file for testing purpose in document root.

    # cd /home/techoism/virtual-one
    # vim index.html
    
    <html>
      <head>
        <title>www.techoism.com</title>
      </head>
      <body>
        <h1>You have set up a Virtual Host successfully</h1>
      </body>
    </html>
    
    # cd /home/techoism/virtual-two
    # vim index.html
    
    <html>
      <head>
        <title>www.techoism.com</title>
      </head>
      <body>
        <h1>You have set up a New Virtual Host successfully</h1>
      </body>
    </html>
    

    Step #3: Turn on Virtual Host

    Now Edit the apache configuration file and make the below changes.

    # vim /etc/httpd/conf/httpd.conf
    

    Add the below lines at the end of the page:

    <VirtualHost *:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/techoism/virtual-one
        ServerName virtual.techoism.com
        ErrorLog logs/virtual.techoism.com-error_log
        CustomLog logs/virtual.techoism.com-access_log common
    <Directory "/home/techoism/public_html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin dennis.r@techoism.com
        DocumentRoot /home/techoism/virtual-two
        ServerName virtualnew.techoism.com
        ErrorLog logs/virtual.techoism.com-error_log
        CustomLog logs/virtual.techoism.com-access_log common
    <Directory "/home/techoism/public_html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    </VirtualHost>
    
    

    Step #4: Restart Apache WebServer

    Now restrat apache webservice.

    For CentOS/RHEL 5/6:
    # /etc/init.d/httpd restart
    For CentOS/RHEL 7:
    # systemctl restart httpd
    

    Step #5: Verify the Configuration

    Now you can verify the configuration on your browser by running below domain:

    http://virtual.techoism.com
    

    httpd

    http://virtualnew.techoism.com
    

    httpd_new

    Enjoy it!

    Leave a Reply

    Your email address will not be published.