Reading Time: 4 minutes

Today, we will be reviewing how to configure Apache virtual hosts on a CentOS 7 VPS server or Dedicated server. If you host websites, chances are you are hosting more than one. If so, knowing how and why these virtual hosts work should allow you to better understand why they are needed. By default, Apache can host only one document root for all requests, which likely isn’t what you want to happen.

We can use VirtualHost blocks to translate named domains into their appropriate document roots, with new settings per-block as needed. But, what goes into a valid VirtualHost? Where should it be stored?

Pre Flight Check

For this article, we will be using a Core-Managed CentOS 7 VPS, which comes with a clean installation of Apache 2.4. Instructions will be similar for CentOS 6, and also for Unmanaged CentOS 7 servers, with the same Apache version. We’re SSH'd to our server as the root user.

Step 1: Set Up Apache

Apache will be configured to host multiple sites out of the gate, so there is not much to do here. Historically (that is, in Apache 2.2), NameVirtualHost was necessary to tell Apache that we wanted to use Virtual Host blocks to host multiple sites. Since this is a prevalent option, it is now on by default in Apache 2.4, so we don’t have any additional configuration to do. But, we do want to make sure that Apache is enabled and will start at boot.

systemctl enable httpd
systemctl start httpd

These two commands should have no output. If they do have output, there was a warning or error, which you can see details of by running:

systemctl status httpd

Apache is now up and running and ready to host sites!

Step 2: Add Document Roots

We now need some domains to add to Apache. Let’s set up two new document root folders, one at /var/www/ and one at /var/www/

mkdir -p /var/www/
mkdir -p /var/www/

Now, you can start to add unique content to each folder. Adding a basic index.html file that says what domain you are loading as this will help us check our work later.

mkdir -p /var/www/
mkdir -p /var/www/

If you have secured Apache to run under a different user, you should update the ownership and permissions. Otherwise, Apache will run as root with root file ownership.

Step 3: Add Virtual Host Files

Now that we have sites to host, we need to tell Apache about them. We will make a new file for each domain, so that we can enable or disable (or add and remove) down the line. Write the following to a file at /etc/httpd/conf.d/


Let’s break these lines down.

The very first line opens our VirtualHost block and defines what IP address and port we will listen on for this directive. The asterisk identifies all IPs on the server, and port 80 as the non-SSL port since we don’t have an SSL yet.

The only two lines you are required to have in a VirtualHost block are ServerName and DocumentRoot. These tell Apache what inbound domain requests to listen for, and what folder to serve those requests.

The ServerAlias can describe other domains that should serve the same document root, which can be handy, in say a WordPress multi-site installation.

The ErrorLog line tells Apache where to store errors related to loading this domain. A single unique file can separate logs per domain, such as

CustomLog with the ‘combined’ option will log all access requests for this domain. This log is also especially useful when troubleshooting issues that happen when a request is made. Also, this can be very useful for keeping track of what IPs are accessing your site.

Other Virtualhost Options

There are other helpful lines you could add to your VirtualHost block:

  • ServerAdmin declares the email address of the webmaster and is provided to visitors if an error is encountered. Other options can be set here as well; Like in a .htaccess file, we can turn off index pages or symlinks.
  • RewriteEngine can be enabled in a VirtualHost
  • Also, other conditions and rules for rewrites

Almost anything you can put in a .htaccess file can also go in a VirtualHost configuration file. Now that we have a VirtualHost in place for, make another one for the same way.

Step 4: Test And Restart Apache

Once the files are written, we can run a config test:

httpd -t

If there are no problems seen, this command will return the following output.

Syntax OK

If there are issues, the httpd command should tell us what line, as well as what file caused the concern. Now we can reload the configuration into Apache.

systemctl reload httpd

Again, if that reload is successful, this command will have no output. We can confirm that we loaded the VirtualHosts by running:

root@host [~]# httpd -S
 VirtualHost configuration: (/etc/apache2/conf/httpd.conf:305) (/etc/apache2/conf/httpd.conf:3726)  is a NameVirtualHost 
default server (/etc/apache2/conf/httpd.conf:327)
port 80 namevhost (/etc/apache2/conf/httpd.conf:327)
port 80 namevhost (/etc/apache2/conf/httpd.conf:390)

The output of this command will show Apache's current running configuration, including the VirtualHost blocks that are loaded.

You should see two entries that say ‘*:80’ at the beginning of the line, which will be and You will also see the configuration file that the VirtualHost was loaded from, in parentheses.

If you have set up the DNS entries for the domains, you can now visit the domains in your browser, and you will see the two index.html files we wrote earlier. If you don’t have DNS set up yet, you can test using hosts file modification on your workstation.

Finished! Now that our VirtualHosts are done and running, we can continue configuring our websites, securing our server, or setting up SSLs for the newly installed domains.

We Are Here To Help!

Do you have other thoughts or concerns? If so, the Liquidweb support team can help. Simply open a ticket with us today at [email protected], give us a call at 1-800-580-4985 or, drop us a line via a chat to speak to one of our Level 3 Support Admins today!

Avatar for Andrej Walilko

About the Author: Andrej Walilko

Andrej Walilko (RHCE6) is a seasoned Linux Administrator, and he is a Migration Project Manager at Liquid Web, developing specialized processes for complex migration types. He enjoys doing woodworking, home improvement, and playing piano in his free time.

Latest Articles

Using a Cloudflare Argo Tunnel with load balancing

Read Article

Subdomain takeover — protect your website against it!

Read Article

Controlling PHP settings with a custom php.ini file

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article