IP Based Virtual Hosting on CentOS 7 with Apache Server

Virtual Hosting is a method of hosting multiple domains on single web server. If you have multiple domains (such as domain1.com, domain2.com and so on) and want to host on single web server, Virtual Web Hosting is your right choice. Apache web server provides an easy way to manage Virtual Hosting. Virtual Hosting can be either Name Based or IP Based. In Name Based Virtual Hosting, multiple domains can be hosted on single IP address. On the other hand, in IP Based Virtual Hosting, each domain is mapped on a dedicated IP address. In my previous article, I discussed how to configure Named Based Apache Virtual Web Hosting on CentOS 7 Linux. In this article, I will discuss how to configure IP Based Apache Virtual Hosting on CentOS 7 Linux.

IP Based Apache Virtual Hosting Configuration

We are now going to start Virtual Hosting configuration with apache web server. It is assumed that you have already installed CentOS 7 Server on your physical or virtual machine. If you don’t have CentOS 7 Server installed, take a look of my previous article about CentOS 7 installation and configuration with GNOME Desktop and install CentOS 7 accordingly and then follow this article. Complete Apache Virtual Hosting configuration on CentOS 7 can be divided into two parts.

  • DNS configuration for the hosting domains
  • Apache webserver configuration for Virtual Hosting

Part 1: DNS Configuration for the Hosting Domains

If you have registered domains and want to host them on your own web server, create a Host record (A record) with wildcard value (@) that will point to your webserver. In this case, your webserver must have on public IP addresses.

For this article configuration, we will use custom domains (systemzone.net, systemzone.com and systemzone.org) and these domains entry will keep in a local DNS Server. So, our webserver is also on local IP address.

In my previous article, I discussed how to configure a local DNS server on CentOS 7 with BIND package. According to that configuration, DNS zone entries are in named.rfc1912.zones file. So, open this file and put the following entries (for the three custom domains) at the bottom.

[root@ns1 ~]# vim /etc/named.rfc1912.zoneszone “systemzone.net” IN {

 

type master;

file “systemzone.net.for”;

allow-update { none; };

};

zone “systemzone.com” IN {

type master;

file “systemzone.net.for”;

allow-update { none; };

};

zone “systemzone.org” IN {

type master;

file “systemzone.net.for”;

allow-update { none; };

};

Notice that the three custom domains are pointing the same zone file (systemzone.net.for). So, open that zone file and put Host record (A record) in this file for the custom domains. Your zone file looks like the following example.

[root@ns1 ~]# vim /var/named/systemzone.net.for$TTL 1D

 

@       IN SOA  ns1.systemzone.net. root.systemzone.net. (

0       ; serial

1D      ; refresh

1H      ; retry

1W      ; expire

3H )    ; minimum

@       IN NS ns1.systemzone.net.

$ORIGIN systemzone.net.

ns1     IN A 192.168.40.100

@       IN A 192.168.40.101

www     IN A 192.168.40.101

$ORIGIN systemzone.com.

@       IN A 192.168.40.102

www     IN A 192.168.40.102

$ORIGIN systemzone.org.

@       IN A 192.168.40.103

www     IN A 192.168.40.103

Notice that each custom domain’s wildcard (@) Host record is pointing to different IP address. Actually, these IP addresses are our Apache Web Server’s IP addresses.

We will now configure IP Based Virtual Hosting in our apache webserver so that each domain gets different IP address.

Part 2: Apache Web Server Configuration for IP Based Virtual Hosting

We will first install and enable Apache Web Server and then configure IP Based Virtual Hosting. So, issue the following command to install apache web server in your CentOS 7 Linux.

[root@webserver ~]# yum install httpd -y

The httpd package will be installed within a few second. After installing apache httpd package, we have to start the Apache service with the following command.

[root@webserver ~]# systemctl start httpd

Apache service is now active and running and waiting for the incoming web server (http) requests. The daemon will now answer any incoming http request.

But if your server gets rebooted in any case, the httpd daemon will not be stated automatically. So, run the following command to start apache service automatically if any system restart is occurred.

[root@webserver ~]# systemctl enable httpd.

Run the following firewall commands to allow http service through your firewall. Otherwise your webserver cannot be accessed from remote PC.

[root@webserver ~]# firewall-cmd –zone=public –add-service=http
[root@webserver ~]# firewall-cmd –permanent –zone=public –add-service=http
[root@webserver ~]# firewall-cmd –reload

As we have planned three custom domains (systemzone.net, systemzone.com and systemzone.org) will be mapped with the three IP addresses (192.168.40.101, 192.168.40.102 and 192.168.40.103) respectively, put these secondary IP addresses in your CentOS 7 Linux with the nmtui tool. Your nmtui configuration GUI will look like the following image.

Secondary IP Setup with nmtui
Secondary IP Setup with nmtui tool

We will now declare Virtual Host in apache configuration file located in /etc/httpd/conf directory. So, go to that directory and open apache configuration file (httpd.conf ) and add the following lines at the bottom.

[root@webserver ~]# cd /etc/httpd/conf[root@webserver conf]# vim httpd.conf

 

<VirtualHost 192.168.40.101>

DocumentRoot /var/www/html/systemzone.net

ServerName systemzone.net

ServerAlias www.systemzone.net

</VirtualHost>

<VirtualHost 192.168.40.102>

DocumentRoot /var/www/html/systemzone.com

ServerName systemzone.com

ServerAlias www.systemzone.com

</VirtualHost>

<VirtualHost 192.168.40.103>

DocumentRoot /var/www/html/systemzone.org

ServerName systemzone.org

ServerAlias www.systemzone.org

</VirtualHost>

In the above lines, we have declared three VIrtualHost on our webserver for the three custom domains. Each VirtualHost has the following three properties.

Property NameDescription
DocumentRootThe root directory from where web document will be retrieved.
ServerNameThe name of domain for this virtual host.
ServerAliasAlternative ServerName that can be used to access the virtual host. More than one ServerAlias can be used or multiple names can be declared with space separated value or wildcard (*) character can be used before the root ServerName.

As we have declared three root directories for three custom domains, we will now create these directories first. So, issue the following command to create three declared directories in html directory.

[root@webserver conf]# cd /var/www/html[root@webserver html]# mkdir systemzone.net systemzone.com systemzone.org

 

Now go to systemzone.net directory and create an index file (index.html) and then put the following html content.

[root@webserver html]# cd systemzone.net

[root@webserver systemzone.net]# vim index.html

<html>

<head>

<title>systemzone.net</title>

</head>

<body>

<h1> Welcome to systemzone.net </h1>

</body>

</html>

Similarly, go to systemzone.com directory and create index file and put the following html content.

[root@webserver html]# cd systemzone.com[root@webserver systemzone.com]# vim index.html

 

<html>

<head>

<title>systemzone.com</title>

</head>

<body>

<h1> Welcome to systemzone.com </h1>

</body>

</html>

Similarly, go to systemzone.org directory and create index file and put the following html content.

[root@webserver html]# cd systemzone.com[root@webserver systemzone.com]# vim index.html

 

<html>

<head>

<title>systemzone.com</title>

</head>

<body>

<h1> Welcome to systemzone.com </h1>

</body>

</html>

Now open your browser and type your custom domain in URL bar. If everything is OK, your will get the respected content of each domain.

Virtual Hosting Output
Virtual Hosting Output

If you face any confusion to follow the above steps carefully, watch the following video about IP Based Apache Virtual Hosting. I hope, it will reduce your any confusion.

How to configure IP Based Apache Virtual Hosting on CentOS 7 Linux has been discussed in this article. I hope you will now be able to create IP Based Apache Virtual Hosting for your domains following the above steps properly. However, if you face any confusion to create Virtual Hosting with Apache web server, feel free to discuss in comment or contact with me from Contact page. I will try my best to stay with you.

Why not a Cup of COFFEE if the solution?

ip-based-virtual-hosting-on-centos-7-with-apache-server

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a System Zone author.

Leave a Reply

Your email address will not be published. Required fields are marked *

*