Apache breaks down its functionality and components into individual units that can be customized and configured independently. The basic unit that describes an individual site or domain is called a virtual host. Virtual hosts allow one server to host multiple domains or interfaces by using a matching system. This is relevant to anyone looking to host more than one site off of a single Server.
Each domain that is configured will direct the visitor to a specific directory holding that site's information, without ever indicating that the same server is also responsible for other sites. This scheme is expandable without any software limit, as long as your server can handle the traffic that all of the sites attract.
In this guide, we will walk through how to set up Apache virtual hosts on a CentOS 7 Server. During this process, you'll learn how to serve different content to different visitors depending on which domains they are requesting.
PrerequisitesBefore you begin with this guide, there are a few steps that need to be completed first.
You will need access to a CentOS 7 server with a non-root user that has
sudoprivileges. If you haven't configured this yet, you can run through the CentOS 7 initial server setup guide to create this account.
You will also need to have Apache installed in order to configure virtual hosts for it. If you haven't already done so, you can use
yumto install Apache through CentOS's default software repositories:
sudo yum -y install httpd
Next, enable Apache as a CentOS service so that it will automatically start after a reboot:
After these steps are complete, log in as your non-root user account through SSH and continue with the tutorial.
sudo systemctl enable httpd.service
Note: The example configuration in this guide will make one virtual host for
example.comand another for
example2.com. These will be referenced throughout the guide, but you should substitute your own domains or values while following along.
If you do not have any real domains to play with, we will show you how to test your virtual host configuration with dummy values near the end of the tutorial.
Step One — Create the Directory StructureFirst, we need to make a directory structure that will hold the site data to serve to visitors.
Our document root (the top-level directory that Apache looks at to find content to serve) will be set to individual directories in the
/var/wwwdirectory. We will create a directory here for each of the virtual hosts that we plan on making.
Within each of these directories, we will create a
public_htmldirectory that will hold our actual files. This gives us some flexibility in our hosting.
We can make these directories using the
mkdircommand (with a
-pflag that allows us to create a folder with a nested folder inside of it):
sudo mkdir -p /var/www/example.com/public_html sudo mkdir -p /var/www/example2.com/public_html
Step Two — Grant PermissionsWe now have the directory structure for our files, but they are owned by our
rootuser. If we want our regular user to be able to modify files in our web directories, we can change the ownership with
sudo chown -R $USER:$USER /var/www/example.com/public_html sudo chown -R $USER:$USER /var/www/example2.com/public_html
$USERvariable will take the value of the user you are currently logged in as when you submit the command. By doing this, our regular user now owns the
public_htmlsubdirectories where we will be storing our content.
We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory, and all of the files and folders inside, so that pages can be served correctly:
sudo chmod -R 755 /var/www
Your web server should now have the permissions it needs to serve content, and your user should be able to create content within the appropriate folders.
Step Three — Create Demo Pages for Each Virtual HostNow that we have our directory structure in place, let's create some content to serve.
Because this is just for demonstration and testing, our pages will be very simple. We are just going to make an
index.htmlpage for each site that identifies that specific domain.
Let's start with
example.com. We can open up an
index.htmlfile in our editor by typing:
In this file, create a simple HTML document that indicates the site that the page is connected to. For this guide, the file for our first domain will look like this:
Welcome to Example.com!
Success! The example.com virtual host is working!
Save and close the file when you are finished.
We can copy this file to use as the template for our second site's
cp /var/www/example.com/public_html/index.html /var/www/example2.com/public_html/index.html
Now let's open that file and modify the relevant pieces of information:
Welcome to Example2.com!
Success! The example2.com virtual host is working!
Save and close this file as well. You now have the pages necessary to test the virtual host configuration.
Step Four — Create New Virtual Host FilesVirtual host files are what specify the configuration of our separate sites and dictate how the Apache web server will respond to various domain requests.
To begin, we will need to set up the directory that our virtual hosts will be stored in, as well as the directory that tells Apache that a virtual host is ready to serve to visitors. The
sites-availabledirectory will keep all of our virtual host files, while the
sites-enableddirectory will hold symbolic links to virtual hosts that we want to publish. We can make both directories by typing:
sudo mkdir /etc/httpd/sites-available sudo mkdir /etc/httpd/sites-enabled
Note: This directory layout was introduced by Debian contributors, but we are including it here for added flexibility with managing our virtual hosts (as it's easier to temporarily enable and disable virtual hosts this way).
Next, we should tell Apache to look for virtual hosts in the
sites-enableddirectory. To accomplish this, we will edit Apache's main configuration file and add a line declaring an optional directory for additional configuration files:
sudo nano /etc/httpd/conf/httpd.conf
Add this line to the end of the file:
Save and close the file when you are done adding that line. We are now ready to create our first virtual host file.
Create the First Virtual Host FileStart by opening the new file in your editor with root privileges:
sudo nano /etc/httpd/sites-available/example.com.conf
Note: Due to the configurations that we have outlined, all virtual host files must end in
First, start by making a pair of tags designating the content as a virtual host that is listening on port 80 (the default HTTP port):
Next we'll declare the main server name, www.example.com. We'll also make a server alias to point to example.com, so that requests for www.example.com and example.com deliver the same content:
ServerName www.example.com ServerAlias example.com
Note: In order for the
wwwversion of the domain to work correctly, the domain's DNS configuration will need an A record or CNAME that points
wwwrequests to the server's IP. A wildcard (
*) record will also work.
Finally, we'll finish up by pointing to the root directory of our publicly accessible web documents. We will also tell Apache where to store error and request logs for this particular site:
ServerName www.example.com ServerAlias example.com DocumentRoot /var/www/example.com/public_html ErrorLog /var/www/example.com/error.log CustomLog /var/www/example.com/requests.log combined
When you are finished writing out these items, you can save and close the file.
Copy First Virtual Host and Customize for Additional DomainsNow that we have our first virtual host file established, we can create our second one by copying that file and adjusting it as needed.
Start by copying it with
sudo cp /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-available/example2.com.conf
Open the new file with root privileges in your text editor:
sudo nano /etc/httpd/sites-available/example2.com.conf
You now need to modify all of the pieces of information to reference your second domain. When you are finished, your second virtual host file may look something like this:
ServerName www.example2.com DocumentRoot /var/www/example2.com/public_html ServerAlias example2.com ErrorLog /var/www/example2.com/error.log CustomLog /var/www/example2.com/requests.log combined
When you are finished making these changes, you can save and close the file.
Step Five — Enable the New Virtual Host FilesNow that we have created our virtual host files, we need to enable them so that Apache knows to serve them to visitors. To do this, we can create a symbolic link for each virtual host in the
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
sudo ln -s /etc/httpd/sites-available/example2.com.conf /etc/httpd/sites-enabled/example2.com.conf
When you are finished, restart Apache to make these changes take effect:
sudo apachectl restart
Step Six — Set Up Local Hosts File (Optional)If you have been using example domains instead of actual domains to test this procedure, you can still test the functionality of your virtual hosts by temporarily modifying the
hostsfile on your local computer. This will intercept any requests for the domains that you configured and point them to your server, just as the DNS system would do if you were using registered domains. This will only work from your computer, though, and is simply useful for testing purposes.
Note: Make sure that you are operating on your local computer for these steps and not your CentOS server. You will need access to the administrative credentials for that computer.
If you are on a Mac or Linux computer, edit your local
hostsfile with administrative privileges by typing:
sudo nano /etc/hosts
If you are on a Windows machine, you can find instructions on altering your hosts file here.
The details that you need to add are the public IP address of yourCentOS Server followed by the domain that you want to use to reach that Server:
127.0.0.1 localhost 127.0.1.1 guest-desktop server_ip_address example.com server_ip_address example2.com
This will direct any requests for
example2.comon our local computer and send them to our server at
Step Seven — Test Your ResultsNow that you have your virtual hosts configured, you can test your setup easily by going to the domains that you configured in your web browser:
You should see a page that looks like this:
Likewise, if you visit your other domains, you will see the files that you created for them.
If all of the sites that you configured work well, then you have successfully configured your new Apache virtual hosts on the same CentOS server.
If you adjusted your home computer's
hostsfile, you may want to delete the lines that you added now that you've verified that your configuration works. This will prevent your hosts file from being filled with entries that are not actually necessary.