Read in 4 minutes

last updated 

How to Set Up Apache Virtual Hosts on CentOS 7

Apache Virtual Hosts allows multiple websites to run on one Web server. With virtual hosts, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.

In this tutorial, we’ll provide a step by step instructions about how to set up Apache Virtual Hosts on a CentOS 7 server.

Prerequisites

Make sure you met the following prerequisites before continuing with this tutorial:

Creating Directory Structure

DocumentRoot is the directory where the website files for a domain name are stored and served in response to requests. We can set the document root to any location we want but in this guide we will use the following directory structure:

/var/www/
├── example.com
│   └── public_html
├── example-1.com
│   └── public_html
├── example-2.com
│   └── public_html

Basically we are creating a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, we will create a public_html directory that will store the domain website files.

Create the root directory for the domain example.com:

sudo mkdir -p /var/www/example.com/public_html

For testing purposes we will create an index.html file inside the domain document root directory. Open your editor and create a HTML file with the following contents:

/var/www/example.com/public_html/index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

All commands are executed as sudo user, so the newly created files and directories are owned by the root user. To make sure there are no permission issues, change the ownership of the domain document root directory to the apache user :

sudo chown -R apache: /var/www/example.com

Creating Virtual Host File

There are a few ways to set up a virtual host. You can either add all Virtual Host Directives in a single file or create a new configuration file for each Virtual Host Directive. Personally I prefer the second approach because it is more maintainable.

By default, Apache is configured to load all configuration files that ends with .conf from the /etc/httpd/conf.d/ directory.

To create a virtual host for specific website open your editor of choice and create the following basic Virtual Host configuration file:

/etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
  • ServerName: The domain that should match for this virtual host configuration. This should be your domain name.
  • ServerAlias: All other domains that should matchfor this virtual host as well, such as the www subdomain.
  • DocumentRoot: The directory from which Apache will serve the domain files.
  • Options: This directive controls which server features are available in a specific directory.
    • -Indexes: Prevents directory listings.
    • FollowSymLinks: This option tells your web server to follow the symbolic links.
  • AllowOverride: Specifies which directives declared in the .htaccess file can override the configuration directives.
  • ErrorLog, CustomLog: Specifies the location for log files.

Edit the file according to your needs and save it.

It is important that the configuration file name ends with .conf. You can name the configuration file as you like but the best practice is to use the domain name as the name of the virtual host configuration file.

Test the configuration file syntax with:

sudo apachectl configtest

If there are no errors your will see the following output:

Syntax OK

To activate a newly created virtual host, restart the Apache service with:

sudo systemctl restart httpd

Verify that everything is working as expected, by opening http://example.com:

Conclusion

In this tutorial we have shown you how to create an Apache virtual host configuration. You can repeat the steps we outlined above and create additional virtual hosts for all your domains.

If you are facing any problem, feel free to leave a comment.

This post is a part of the Install LAMP Stack on CentOS 7 series.
Other posts in this series:

How to Set Up Apache Virtual Hosts on CentOS 7