Knowledge Base
MilesWeb / VPS FAQ

How to Configure Apache Virtual Hosts on CentOS7?

Approx. read time : 3 min

Using Apache web server, you can host more than one domain on the same server using virtual hosts. Virtual hosting can host multiple websites on one web server. You can even specify the site document root (the directory that contains the website files), create a different security policy for every site, using SSL certificates for every site and much more.

In this tutorial, we will get to know the steps to configure the steps to Configure Apache Virtual Host on CentOS 7.

Introduction:

Apache is one of the most commonly used, free and open-source web server software that is used for running website/applications. Nearly, around 40% websites use Apache web server.

Prerequisites:

  • You need to have a domain name which is pointing to your public server IP. Here we are using test.com.
  • You are logged in as a user with sudo privileges.
  • You have Apache installed already.

Steps to Configure Apache Virtual Hosts on CentOS7.

The first step is to create a directory structure. DocumentRoot is the directory where all the website files for a domain name are stored and served in response to the requests. You can set the document root to the location you need.

After creating the directory structure, you then have to create the virtual host file.

Creating Virtual Host File

There are different ways through which you can set up a virtual host. You can even make separate file for every Virtual Host Directive or you can add all the Virtual Host Directives in a single file. It is also suggested to make separate files for every domain due to its maintainability.

By default, Apache is configured to load all the configurations ending with .conf from the /etc/httpd/conf.d/ directory.

Now, to create a virtual host for a specific website open the editor of your choice and create the following basic Virtual Host Configuration file.

/etc/httpd/conf.d/test.com.conf

<VirtualHost *:80>

    ServerName test.com

    ServerAlias www.test.com

    ServerAdmin milesweb@test.com

    DocumentRoot /var/www/test.com/public_html

    <Directory /var/www/test.com/public_html>

        Options -Indexes +FollowSymLinks

        AllowOverride All

    </Directory>

    ErrorLog /var/log/httpd/test.com-error.log

    CustomLog /var/log/httpd/test.com-access.log combined

</VirtualHost>

ServerName: This should be your domain name and need to match with the virtual host configuration.

ServerAlias: All the other domains or subdomains should match for this virtual host in addition with, usually the www subdomain.

DocumentRoot: It’s the directory from which the Apache will serve the domain files.

Options: This directive controls the server features that are available in a specific directory.

  • Indexes: Prevents the directory listings.
  • FollowSymLinks: It tells your web server to follow the symbolic links.

AllowOverride: Specifies whether the directives declared in the .htaccess file canoverride theconfiguration devices.

ErrorLog, CustomLog: It specifies the location for the log files.

It’s very important that the configuration file name ends with the .conf. Any names can be given to your configuration file, but it’s recommended to use the domain name as the name of the virtual host configuration file.

Now check the syntax by typing the command:

$ httpd -t

It will give you the below output if there are no errors.

Output

Syntax OK

Now, to activate the newly created virtual host, restart the Apache service with:

$ sudo systemctl restart httpd

You can verify by opening your http://test.com to your web browser and then it will show you the following message.

Success! test.com set up completed!

Conclusion

In this tutorial, we have explained the steps to create a Apache Virtual host on CentOs. You can repeat the steps given above and then create an additional virtual host for all your domains.

Nehal is an ardent content writer. She's passionate about crafting content that's simple but adds value. Her insatiable interest in writing has allowed her to explore her skills. She is adept and can write for different types of content formats.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna