On this page
- How To Set Up The Basic Apache Config For Virtual Hosts On CentOS 6
- 1.1: Virtual Hosts-An Overview
- 1.2: Vital Preliminary Checks Prior to Setting Up Apache Virtual Hosts
- 1.3: Steps to Set Up Apache Virtual Hosts on CentOS
How To Set Up The Basic Apache Config For Virtual Hosts On CentOS 6
- 1.1: Virtual Hosts-An Overview
- 1.2: Vital Preliminary Checks Prior to Setting Up
- 1.3: Steps to Set Up Apache Virtual Hosts on CentOS
- 1.3.1: Step One-Creating a New Directory
- 1.3.2: Step Two-Granting of Permissions
- 1.3.3: Step Three-Creating the Page
- 1.3.4: Step Four-Turning on Virtual Hosts
- 1.3.5: Step Five-Restarting Apache
- 1.3.6: Step Six (Optional)-Setting Up the Local Hosts
- 1.3.7: Step Seven-Virtual Host goes LIVE!
1.1: Virtual Hosts-An Overview
Virtual Hosts are required in order to run multiple domains off a single IP address. Creating virtual host is expressly helpful for users who wish to run numerous sites using one virtual private server (VPS). Typically, each server is distinctly defined by it's domain/hostname. To the utter delight of users, there does not exist an upper limit to the number of virtual hosts that may be added to a VPS.
1.2: Vital Preliminary Checks Prior to Setting Up Apache Virtual Hosts
Before you get on to the drill, please make note of the following:
1. Access to root privileges is a pre-condition for users following the steps outlined in this tutorial. You will learn the process of setting it up as part of the Initial Server Setup explained in steps 3 and 4.
2. In addition, Apache must be installed and running on your virtual server before you set out to establish Apache Virtual Hosts on CentOS. If Apache is currently missing from your virtual server, you may simply download the same using the following command:
yum install httpd
1.3: Steps to Set Up Apache Virtual Hosts on CentOS
Here is the detailed process of setting up Apache Virtual Hosts on CentOS, broken down into simple and easy-to-follow steps: for our readers:
1.3.1: Step One-Creating a New Directory
The first step in the process of creating a virtual host remains the creation of a directory to store all the information pertaining to the new website. This location will serve as the Document Root in the Apache virtual configuration file subsequently. By simply adding a -p (as shown below) to the line of code, you set the command to generate all the parents for the new directory in an automated manner.
mkdir -p /var/www/sample.com/public_html
It is also required of users to designate a real DNS approved domain/ IP address in order to verify that the virtual host is working.
Nonetheless, for those who wish to use an unauthorized domain name for testing the process, information of how to make the same function on your local PC has been explained in detail in Step Six of the process.
1.3.2: Step Two-Granting of Permissions
As a second step, users need to assign directory ownership to the user, instead of simply retaining it on the root system. This can be executed by using the following command:
chown -R www:www /var/www/sample.com/public_htmlFurthermore, users must ensure that the new files are readable to all. The following command shall help ensure the same:
chmod 755 /var/www
This brings you to the end of the process of granting all the relevant permissions, and you are now ready to graduate to the next step.
1.3.3: Step Three-Creating the Page
This step entails the creation of a new file namely index.html within our configurations directory. The same can be done using the following command:
It is advisable to add some concrete text to the file to establish something tangible for revisiting when the IP redirects to the virtual host. The same has been illustrated below:
<h1>Congrats: You Have Successfully Set Up a Virtual Host</h1>
All you need to do now is Save and ExitThe above brings you to the end of this step. You are now ready to move on to the next one, as explained below.
1.3.4: Step Four-Turning on Virtual Hosts
The next step in the process requires the user to enter into the apache configuration file itself. This can be done using the following command:
sudo vi /etc/httpd/conf/httpd.conf
Subsequent to using the above command, it is important to watch out for a few lines. Please ensure that the text you receive matches the following:
Listen 90Now users need to scroll down right to the bottom till you reach the section labelled Virtual Hosts.
NameVirtualHost *:90 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :90) if mod_ssl is being used, due to the nature of the # SSL protocol. # # # VirtualHost sample: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. #
ServerAdmin [email protected] DocumentRoot /var/www/sample.com/public_html ServerName www.sample.com ServerAlias sample.com ErrorLog /var/www/sample.com/error.log CustomLog /var/www/sample.com/requests.log
The key lines here are the ones that contain the terms NameVirtualHost, Virtual Host, Document Root, and Server Name. Let us decode each of these
You need to "uncomment", which effectively means removing the number sign, NameVirtualHost without changing anything. The star denotes that any IP address travelling through port 90 will act as the virtual host. Typically, your system would have just one IP address, hence this should not pose any concern. Nonetheless, you may replace the start with the actual IP address if you so wish to.
You may skip the remaining number marks till your reach the line
You may leave
Document Root is key! As far as this section is concerned, you need to write in the new directory extension that you created in Step One. If there exists a document root mismatch, or if it is missing altogether, you would not be able to set up the virtual host. So, accuracy in this step is crucial to creating the Virtual Host.
Server Name is yet another significant bit of information comprising the domain name of the virtual host, for instance www.sample.com. Please ensure that you spell the full name of the domain here, though any alternating possibilities may be covered in the next line.-
ServerAlias happens to be a fresh line in the config file that does not exist by default. By manually adding the same, you get the privilege to list a couple of variants of the domain name, for instance minus the www.
The remaining lines in this portion are not really needed to establish a virtual host. Nevertheless, it is good-to-know kind of stuff, so here it is:
1. Server Admin prompts you to supply the webmaster's email.
2. The Custom Logs and Error maintain track of any issues the server may face. While the Error Log enlists issues arising during server maintenance, the Custom Log maintains an activity track of the server requests. Users have the privilege to set up a custom location for these processes.
Please Note: You must always ensure that
1.3.5: Step Five-Restarting ApacheThrough the above steps, users have changed the configuration significantly. Hence, the changes shall not be effective unless Apache is restarted. To begin the process, you need to stop all all Apache processes by giving the following command:
/etc/init.d/httpd restartUpon keying in the above command, you may encounter the following error: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName This is simply a warning message, and should not hamper your access to the Virtual Host.
1.3.6: Step Six (Optional) Setting Up the Local Hosts
This step is optional, and may be skipped if you have specified the domain name as the IP address of the virtual private server, as in this scenario, you are not required to set up local hosts, and the virtual hosts should work in a hassle-free manner. Nonetheless, if you wish to try the new virtual hosts without connecting to an actual domain name, you may set up local hosts only on your computer. However, to pull this off, you must ensure that you are functioning from your computer and not a droplet. To complete this step, you must know the administrative password of your system; else you would need to use an actual domain name in order to test the Virtual Hosts. If you are on logged in on a Mac or Linux system, you must access the root user (su) on the system to open up the hosts file:
nano /etc/hostsIf you are logged in on a Windows Computer, you must refer to the Microsoft website to seek directions on altering the host file, which is a fairly simple and idiot-proof process. You may add details of the local hosts to this file, as shown below. As long as you find that line there, moving your browser towards, let's say, sample.com will throw up all the virtual host details pertaining to the IP address.
Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 18.104.22.168 localhost #Virtual Hosts 22.214.171.1240 www.example.com
It is pertinent to note here that it may be advisable to delete these proxy addresses out of the local hosts folder when once you are done to eliminate confusion possibilities in the future.
1.3.7: Step Seven-Virtual Host goes LIVE!
Once you are through with the process of setting up the Virtual Host, you would be in a position to see how it looks online. All you need to do is type your IP address (http:// 126.96.36.1990 ) into the browser, and you are good to go!