Apache Virtual Host
Apache Virtual Host : IP Based Virtual Hosting in RHEL/Centos 7

Apache Virtual Host : IP Based Virtual Hosting in RHEL/Centos 7

This tutorial is about, How to setup IP Based Apache Virtual Host for web Hosting (website hosting) in Centos 7 or RHEL 7 with the help of Apache virtual server. As we know that Apache is Secure, powerful and configurable Web Server. In this Tutorial, We will discuss one feature of Apache best web hostingwhich allows to host more than one website (IP Based Virtual Hosts) on a Single Linux server i.e.linux web hosting.

Types of Apache Virtual Host

There are two types of Virtual Hosting in Apache.

  1. Name Based Virtual Hosting
  2. 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 single server with single NIC cards or Multiple NIC’s.

IP based virtual hosting apache
IP based virtual hosting apache

So, If apache virtual host not working on your server then follow this steps carefully it will help you to setup IP Based Virtual Hosting in RHEL/Centos 7.

Our Testing Environment
  • OS – Centos 7
  • Application – Apache Web Server
  • IP Address –
  • IP address –
  • Domain – www.domain1.com
  • Domain – www.domain2.com

How to Setup IP Based Virtual Hosts

  1. 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. e.g. 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 apache 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.

Sample Output:

In the above output we have two IP’s (ens33) and (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 or Port to receive http requests, for that just comment original Listen directive 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)

apache virtual host configuration

4. Now, Open apache default virtual host file (httpd-vhosts.conf) 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 virtual domain.

  • www.domain1.com
  • www.domain2.com

First of all, In apache add virtual host / linux virtual host directives. In this file, First virtual host 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 in apache virtual host for that you have to 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.

  • www.domain1.com

index.html file content

  •  www.domain2.com

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.


Preview: www.domain1.com

Virtual Hosting apache
Virtual Hosting : www.domain1.com
Preview: www.domain2.com

virtual hosting apache
Virtual Hosting : www.domain2.com

Read More :

That’s it, This is all about How to Setup Apache Virtual Host : IP Based Virtual Hosting on Linux (RHEL Centos 7). If you want to ask any question about virtual host apache centos 7 comment Below and always Remember to stay connected with the GeekyVaibhav.com for Latest updates about the Linux.


Please enter your comment!
Please enter your name here