This tutorial is about, How to setup IP Based Virtual Hosting in Centos 7 or RHEL 7with the help of Apache web server. As we are all aware about Apache it is Secure, powerful, Highly Flexible and configurable Web Server. Here in this Tutorial, We are going to discuss one more feature of Apache which allows us to host more than one (IP Based Virtual Hosts) Website on a Single Linux Machine. Also, Implementing Virtual Hosting with Apache web server can help you to save time and costs that you are investing on your server maintenance and their administrators.
Types of Virtual Host
There are two types of Virtual Hosting is available in Apache.
- Name Based Virtual Hosting
- IP Based Virtual Hosting
IP Based Virtual Hosting
In IP Based Virtual Hosting you can host Multiple Websites/Domains and you can assign a separate IP for each domain on a single server. All Domains on that Server will be sharing a separate IP. These IP’s can be attached to the server with single NIC cards as well as Multiple NIC’s.
So, In this Tutorial we are talking about How to setup IP Based Virtual Hosting in RHEL/Centos 7.
Our Testing Environment
- OS – Centos 7
- Application – Apache Web Server
- IP Address – 192.168.0.100
- IP address – 192.168.0.200
- Domain – www.domain1.com
- Domain – www.domain2.com
How to Setup IP Based Virtual Hosts
- Before we Start Configuring IP Based Virtual Hosting. Make Sure that you already Installed Apache in you Server. If you Didn’t Installed Apache in you server visit the following post and follow all steps
To Setup IP based virtual hosting, you must have more than one IP address assigned to your server/Linux machine. It can be on a single NIC or multiple NIC that’s depends upon you. but, most preferred to create multiple IP’s in One NIC. For Example: eth0:0, eth0:1, eth0:2,etc. for that first of all checkout the following post given below.
Many peoples don’t know the main purpose of implementing IP Based virtual hosting. So, Basically it is used to assign particular IP address/port for each domain and that will not be used by any other domain. IP Based hosting is also required when a website is running with SSL certificate (mod_ssl) or on a different ports/IP’s and you can also run multiple instances of Apache on a single machine.
2. I assumed that you done with the Apache installation and Created Multiple IP Address. Now, check the IP’s attached in your server using the following command.
As you can see in the above output we have two IP’s 192.168.0.100 (ens33) and 192.168.0.200 (ens33:0) that IP’s are attached to the One Single NIC to the server.
3. [Note: If you are going to use One Apache at a time then skip this step and Go to step 4 or If you are going to run multiple instance of Apache then follow this step.]
To Assign a specific IP/Port to receive http requests, for that just comment original Listendirective in httpd.conf file and add few entry’s as given below for that type the following command.
After Opening httpd.conf file search for the word “Listen”. you have to comment that original Listen directive and then write your own directive below that line as shown in the picture below.
Reason to comment original Listen 80 port is, In multiple instance of Apache we cannot use same port for both Apache. for that you have comment original Listen 80 port and add your own Listen directive with specific IP and port. In that 80 port is the Apache default port and the 90 Port is second apache port.
Also Read :
- How to run multiple instances of Apache on RHEL/Centos 7 (Linux)
4. Now, Open httpd-vhosts.conf file with any editor like vim.
to move bottom of the file press SHIFT+G button. Here in this example, we are going to setup Virtual Host section for this two domains.
Now, It’s time to Edit the virtualHost directives. In this file First VirtualHost Container is for www.domain1.com and second container is for www.domain2.com
- ServerAdmin – Email address that the server includes in error messages sent to the client. In case, if you got error in their webpage then users may able to contact them via mail.
- DocumentRoot – Main document path of website or domain that contains files like index.html and other webpages.
- ServerName – you have to specify Website/Domain Name.
- ErrorLog – In contains Error log of Website that website/domain.
- CustomLog – An CustomLog/access log is a list of all the requests for individual files that people have requested from a Web site.
you can host multiple websites/domains for that just copy and paste Virtualhost container. After done with the changes in httpd-vhosts.conf file save it with :wq!.
5. Now, You have to create a directory where you will keep all your websites/domains files. so, Create two directories for these two virtual hosts under /var/www/html folder. Please remember that /var/www/html will be your default DocumentRoot in Apache Virtual configuration.
6. After doing this, It is always recommended to check Syntax error after making changes in Apache and before restarting the web server. Because, If any syntax goes wrong Apache will refuse to work with some errors and eventually affect your existing web server go down for while. Type the following command to check syntax error in apache.
if you got output Syntax OK that means everything is ok.
7. Now, you have to add entry in /etc/hosts/ with your IP and domain name.
Add Entry :
Here, You have to just change the IP with your server IP and www.domain1.com and www.domain2.com with your domain names.
8. Now, Create a test page called index.html for each domain in DocumentRoot path as we mentioned in VirtualHost.
index.html file content
index.html file content
9. Now, After doing this restart Apache server by the following command.
10. once you done with it, you can test the setup by accessing both the domains in a browser.
That’s it, This is all about How to Setup IP Based Virtual Hosts on Linux (RHEL/Centos 7). If you want to ask any question about this Tutorial comment Below and always Remember to stay connected with the GeekyVaibhav.com for Latest updates about the Linux.