Virtual Web Hosting with Apache on CentOS 7 (Name Based)

Virtual Hosting is a method of hosting multiple domains on single server. If you have multiple domains (such as domain1.com, domain2.com and so on) and want to host on a webserver, 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 this article, I will only discuss how to configure a Name Based Virtual Web Hosting on CentOS 7 Linux. IP Based Virtual Hosting will be discussed in my next article.

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 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 address.

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 a 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.zones

 

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 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.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 the custom domain’s wildcard (@) Host record is pointing to our apache webserver which IP address is 192.168.40.101.

We will now configure Virtual Hosting in our apache webserver so that we can get three different pages for the three domains.

Part 2: Apache Webserver Configuration for Virtual Hosting

Although I have discussed the complete web server configuration with Apache, MariaDB and PHP in my previous article, I will show the basic part of apache web server installation and configuration for Virtual Hosting here. 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

We will now declare Virtual Hosting 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.101>

DocumentRoot /var/www/html/systemzone.com

ServerName systemzone.com

ServerAlias www.systemzone.com

</VirtualHost>

<VirtualHost 192.168.40.101>

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 (192.168.40.101) 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.

To view whether any error is occurred or not in httpd configuration file, issue the following command.

[root@webserver conf]# httpd -t
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 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 properly, watch the following video about Apache Virtual Hosting on CentOS 7. I hope it will reduce your any confusion.

How to configure Apache Virtual Hosting on CentOS 7 Linux has been discussed in this article. I hope you will now be able to create 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?

virtual-web-hosting-with-apache-on-centos-7-name-based

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 *

*