How To Set Up Apache Virtual Hosts on CentOS 5/6/7

The Apache web server is the most prevalent method for serving web content on the Internet. Every domain that is configured will guide the visitor to a particular directory holding that site’s data, while never showing that the same server is also responsible for other sites. This plan is expandable with no product limit, the length of your server can deal with the traffic that all of the sites attract.

apache-logo

See Also:

  • Types of Virtual Web Hosting in Apache
  • How to create Multiple Virtual Host in CentOS/RHEL 5/6/7
  • 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: Install Apache WebServer

    Apache must be installed and running on your virtual server before you set out to set up Apache Virtual Hosts on CentOS, If it’s not install run below command:

    # yum install httpd
    

    Step #2: Create DocumentRoot

    First you need to create a directory where you will keep the new website code.

    # mkdir -p /home/techoism/public_html
    

    Step #3: Set Permissions

    We have to grant ownership for directory to the user, rather than simply keeping it on the root system.

    # chown -R techoism:techoism /home/techoism/public_html
    

    Moreover, it is important to verify that everybody will have the able to read our new files.

    # chmod -R 755 /home/techoism/public_html
    

    Step #4: Create the Page

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

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

    Step #5: 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/public_html
        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>
    

    Step #6: 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 #7: Verify the Configuration

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

    http://virtual.techoism.com
    

    httpd

    Enjoy it!

    No Responses

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

    The reCAPTCHA verification period has expired. Please reload the page.