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 Hosting is your right choice. Apache web server provides an easy way to manage Virtual Hosting. Virtual Hosting can be Name Based, IP Based or Port Based. In Name Based Virtual Hosting, multiple domains can be hosted on single Server and single IP address. On the other hand, in IP Based Virtual Hosting, each domain is hosted on single server but on a dedicated IP address and in Port Based Virtual Hosting, multiple domains are hosted on different ports. In my previous article, I discussed how to configure Named Based and IP Based Apache Virtual Web Hosting on CentOS 7 Linux. In this article, I will discuss how to configure Port Based Apache Virtual Hosting on CentOS 7 Linux.
Port Based Apache Virtual Hosting Configuration
We are now going to start Port Based 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 Port Based 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
For Port Based Virtual Hosting configuration, we will use three custom domains (systemzone.net, systemzone.com and systemzone.org) and these domains entry will keep in a local DNS Server.
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 these three custom domains) at the bottom.
zone “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 forward 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.
$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.101
www IN A 192.168.40.101
$ORIGIN systemzone.org.
@ IN A 192.168.40.101
www IN A 192.168.40.101
Notice that each custom domain’s wildcard (@) Host record is pointing to same IP address. Actually, this IP addresses is our Apache Web Server’s IP addresses.
We will now configure Port Based Virtual Hosting in our apache webserver so that each domain can be accessible on different ports.
Part 2: Apache Webserver Configuration for Port Based Virtual Hosting
We will first install and enable Apache Web Server and then configure Port Based Virtual Hosting. So, issue the following command to install apache web server in your CentOS 7 Linux.
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.
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.
By default apache works on TCP port 80. But we want to access our three domains on three different ports like the following proposed table.
Domain Name | Accessible TCP Port |
Systemzone.net | 80 |
Systemzone.com | 8080 |
Systemzone.org | 8090 |
So, we have to allow these TCP ports in Firewall. Otherwise cannot be accessible from other LAN PC.
[root@webserver ~]# firewall-cmd –permanent –zone=public –add-service=80/tcp
[root@webserver ~]# firewall-cmd –zone=public –add-port=8080/tcp
[root@webserver ~]# firewall-cmd –permanent –zone=public –add-service=8080/tcp
[root@webserver ~]# firewall-cmd –zone=public –add-port=8090/tcp
[root@webserver ~]# firewall-cmd –permanent –zone=public –add-service=8090/tcp
[root@webserver ~]# firewall-cmd –reload
Now open Apache configuration file located in /etc/httpd/conf directory and find Listen directive. Your will find that port 80 is already declared because default apache port is 80. Now add more two Listen directives for port 8080 and 8090.
Listen 8080
Listen 8090
We will now declare Virtual Host. So, go to at the bottom and add the following lines at the bottom.
[root@webserver conf]# vim httpd.conf
<VirtualHost 192.168.40.101:80>
DocumentRoot /var/www/html/systemzone.net
ServerName systemzone.net
ServerAlias www.systemzone.net
</VirtualHost>
<VirtualHost 192.168.40.101:8080>
DocumentRoot /var/www/html/systemzone.com
ServerName systemzone.com
ServerAlias www.systemzone.com
</VirtualHost>
<VirtualHost 192.168.40.101:8090>
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 Name | Description |
DocumentRoot | The root directory from where web document will be retrieved. |
ServerName | The name of domain for this virtual host. |
ServerAlias | Alternative 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. |
To view whether any error is occurred or not in httpd configuration file, issue the following command.
Syntax OK
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 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 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 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 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 with Port. If everything is OK, your will get the respected content of each domain.
If you face any confusion to follow the above steps properly, watch the following video about Apache Port Based Virtual Hosting Configuration. I hope it will reduce your any confusion.
How to configure Port Based Apache Virtual Hosting on CentOS 7 Linux has been discussed in this article. I hope you will now be able to create Port Based Apache Virtual Hosting for your domains following the above steps properly. However, if you face any confusion to create Port BasedVirtual 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.