• Digital accessories
  • Server
  • Digital life
  • Privacy policy
  • Contact us
  1. Home
  2. Article
  3. How to Configure Apache Virtual Hosts on CentOS 7 | Liquid Web

How to Configure Apache Virtual Hosts on CentOS 7 | Liquid Web

Rsdaa 13/11/2021 1844

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 httpdsystemctl 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:

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/domain.com/ and one at /var/www/domain.net.

mkdir -p /var/www/domain.netmkdir -p /var/www/domain.com

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/domain.netmkdir -p /var/www/domain.com

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/domain.com.conf:

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 domain.com.error.log.

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 VirtualHostAlso, 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 domain.com, make another one for domain.net the same way.

Step 4: Test And Restart Apache

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

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

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.

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: 127.0.0.1:80host.server.com (/etc/apache2/conf/httpd.conf:305) 127.0.0.1:443 host.server.com (/etc/apache2/conf/httpd.conf:3726) 67.227.197.126:80is a NameVirtualHost default server host.server.com (/etc/apache2/conf/httpd.conf:327)port 80 namevhost host.server.com (/etc/apache2/conf/httpd.conf:327)port 80 namevhost domain.com (/etc/apache2/conf/httpd.conf:390)alias mail.domain.comalias www.domain.com

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 domain.com and domain.net. 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 support@liquidweb.com, 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!


PREV: Server Problems That Get You Down (and Solutions!) | Udemy Blog

NEXT: How to Fix Destination Host Unreachable Error?

Popular Articles

Hot Articles

Navigation Lists

Back to Top