The Apache web server is the most widely used method of serving web content on the Internet. It serves more than half of all active websites on the Internet and is extremely powerful and flexible.
Apache’s functionality and components are divided into individual units that can be customised and configured independently. A virtual host is the basic unit that describes an individual site or domain. Using a matching system, virtual hosts enable a single server to host multiple domains or interfaces. This is important for anyone looking to host multiple websites on a single VPS.
Each domain configured will direct visitors to a specific directory containing information about that site, without ever indicating that the same server is also responsible for other sites. This scheme can be expanded indefinitely as long as your server can handle the traffic generated by all of the sites.
This guide will walk you through the process of configuring Apache virtual hosts on a CentOS 7 VPS. Throughout this process, you will learn how to serve different content to different visitors based on the domains they request.
There are a few steps that must be completed before you begin with this guide.
You will need to connect to a CentOS 7 server as a non-root user with sudo privileges
You must also have Apache installed in order to configure virtual hosts. If you haven’t already, use yum to install Apache from the CentOS default software repositories:
Next, configure Apache as a CentOS service so that it starts automatically after a reboot:
After you’ve completed these steps, use SSH to log in as your non-root user account and continue with the tutorial.
Note: Please keep in mind that the example configuration in this guide creates one virtual host for example.com and another for example2.com. These will be mentioned throughout the guide, but you should use your own domains or values while reading.
If you don’t have any real domains to play with, near the end of the tutorial we’ll show you how to test your virtual host configuration with dummy values.
Step 1: Establish the Directory Structure
First, we must create a directory structure that will hold the site data that will be served to users.
Individual directories in the /var/www directory will be set as our document root (the top-level directory that Apache searches for content to serve). We’ll make a directory here for each of the virtual hosts we intend to build.
We will establish a public_html directory within each of these directories to house our actual files. This offers us some leeway in terms of hosting.
We may create these directories with the mkdir command (with the -p argument to create a folder with a nested folder inside it):
Remember that the bits in red reflect the domain names that we want our VPS to serve.
Step 2: Grant permissions.
Our files now have a directory structure, but they are controlled by our root user. If we want our regular user to be able to modify files in our web directory, we may use chown:
When you submit the command, the $USER variable will be set to the user you are currently logged in as. Our ordinary user now has ownership of the public_html folders where we will be placing our content.
We should also tweak our permissions to ensure that read access is granted to the global web directory and all of its files and directories, so that pages may be served properly:
Your web server should now have the necessary permissions to deliver content, and your user should be able to create content in the relevant folders.
Step 3: Make Demo Pages for Each Virtual Host
Now that we’ve established our directory structure, let’s build some material to serve.
Our sites will be fairly simple because this is mainly for demonstration and testing. We will just create an index.html page for each site that identifies that particular domain.
Let’s begin with example.com. We can access an index.html file in our editor by typing:
Create a short HTML document in this file that indicates the site to which the page is linked. The file for our first domain will look like this for this guide:
<title>Welcome to Example.com!</title>
<h1>Success! The example.com virtual host is working!</h1>
When you’re finished, save and close the file.
We can copy this file to serve as the index.html template for our second site by typing:
Now, open that file and change the following information:
<title>Welcome to Example2.com!</title>
<h1>Success! The example2.com virtual host is working!</h1>
This file should also be saved and closed. You now have all of the pages you need to test the virtual host settings.
Step 4:Create New Virtual Host Files
Virtual host files define the configuration of our individual sites and how the Apache web server responds to various domain requests.
To begin, we must create the directory in which our virtual hosts will be stored, as well as the directory that informs Apache when a virtual host is ready to serve visitors. All of our virtual host files will be kept in the sites-available directory, while the sites-enabled directory will contain symbolic links to virtual hosts that we want to publish. Both directories can be created by typing:
Note: This directory layout was created by Debian contributors, but we’ve included it here for added flexibility in managing our virtual hosts (because it’s easier to temporarily enable and disable virtual hosts this way).
The next step is to instruct Apache to look for virtual hosts in the sites-enabled directory. To do so, we’ll edit Apache’s main configuration file and insert a line declaring an optional directory for additional configuration files:
Add the following line to the end of the file:
When you’re finished adding that line, save and close the file. We can now create our first virtual host file.
Make Your First Virtual Host File
Begin by opening the new file in your editor as root:
Note: Because of the configurations we’ve described, all virtual host files must end in.conf.
Begin by creating a pair of tags that designate the content as a virtual host listening on port 80 (the default HTTP port):
The main server name, www.example.com will be declared next. We’ll also create a server alias for example.com so that requests for www.example.com and example.com return the same content:
Note: The domain’s DNS configuration will require an A record or CNAME that points www requests to the server’s IP in order for the www version of the domain to function properly. A record with a wildcard (*) will also work.
Finally, we’ll point to the root directory of our publicly available web documents. We’ll also tell Apache where to keep error and request logs for this site:
CustomLog /var/www/example.com/requests.log combined
You can save and close the file once you’ve finished writing out these items.
Copy the first virtual host and modify it for additional domains.
Now that we’ve established our first virtual host file, we can create our second one by copying it and modifying it as needed.
Begin by copying it using cp:
In your VI editor, open the new file with root privileges:
You must now change all of the information to refer to your secondary domain. When you’re finished, your second virtual host file should look like this:
CustomLog /var/www/example2.com/requests.log combined
When you’ve finished making these changes, save and exit the file
Step 5: Enable the New Virtual Host Files
We now need to enable our virtual host files so that Apache knows how to serve them to visitors. To accomplish this, we can create a symbolic link in the sites-enabled directory for each virtual host:
When you’re finished, restart Apache to apply your changes:
Step 6 : Create a Local Hosts File (Optional)
If you have been testing this technique with sample domains rather than actual domains, you can still verify the functionality of your virtual hosts by temporarily altering the hosts file on your local computer. This will intercept any requests for the domains you configured and route them to your VPS server, just as the DNS system would if you used registered domains. This, however, will only work from your computer and is strictly for testing reasons.
Remember to perform these instructions on your local computer rather than your VPS server. You’ll need access to the computer’s administrative credentials.
Edit your local hosts file with administrator access on a Mac or Linux computer by typing:
The following information is required: the public IP address of your VPS, followed by the domain that you want to use to reach that VPS:
Any requests for example.com and example2.com on our local computer will be sent to our server at server ip address.
Step 7 : Put Your Results to the Test
Now that you’ve configured your virtual hosts, you can quickly test your configuration by going to the domains you specified in your web browser:
You should see something like this:
Similarly, if you go to your other domains, you’ll see the files you made for them.
If all of the sites you set function properly, you have successfully configured your new Apache virtual hosts on the same CentOS server.
If you modified your home computer’s hosts file, you may wish to remove the lines you added now that your configuration is working. This will keep your hosts file from filling up with entries that aren’t actually needed.
You should now have a single CentOS 7 server handling multiple sites with different domains. You can extend this process later by repeating the steps outlined above to create additional virtual hosts. There is no software limit on the number of domain names that Apache can handle, so create as many as your server can handle.
Leave A Comment