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 Khatri
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.