How to Configure Apache Virtual Hosting in RHEL/CentOS

To Setup Apache Virtual Hosting in RHEL/CentOS

Apache virtual hosting can be setup in IP based and Name based in RHEL/CentOS/Fedora will be explained in this article.

Various Types of Virtual Host

Name Based Virtual Hosting

Name based virtual host will host several domains/websites in one system with one IP address.

IP Based Virtual Hosting
IP based virtual host is used to assign a separate IP address for each domain in a server. Here a single NIC card is attached with multiple NIC.

IP Address: 192.168.5.190 and 192.168.5.191
Domain: www.linuxhelp1.com
Domain: www.linuxhelp2.com
OS: CentOS 7
Application: Apache Web Server

Setting of Name Based Apache Virtual Hosts

Install Apache Web server by using the following command.

[root@linuxhelp1 ~]# yum install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package httpd.x86_64 0:2.4.6-40.el7.centos will be installed
.
.
.
Installed:
  httpd.x86_64 0:2.4.6-40.el7.centos                                            

Complete!

To Setup Name Based Virtual Host

You have to create a directory under /var/www/html to keep the website files. It is a default Document Root in Apache.

[root@linuxhelp1 ~]# mkdir /var/www/html/sample1
[root@linuxhelp1 ~]# mkdir /var/www/html/sample2


To Open the Apache main configuration file using vim editor to set up the Name based virtual hosting.

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


Using Shift + G, move to the bottom of the file for setting up the Virtual host sections for your domains.

Example two domains

www.linuxhelp1.com
www.linuxhelp2.com

Add the below two virtual directives at the bottom of the file. To save and exit the file.

< VirtualHost 192.168.5.190:80> 
 servername www.linuxhelp1.com
 Documentroot /var/www/html/sample1
< /VirtualHost> 
< VirtualHost *:80> 
 servername www.linuxhelp2.com
 documentroot /var/www/html/sample2
< /virtualhost> 


Check the syntax of files by using below command, after making changes in httpd.conf file.

[root@linuxhelp1 ~]# httpd -t
Syntax OK


Now you can add the service to the firewall

[root@linuxhelp1 ~]# firewall-cmd --permanent --add-service=http
success
[root@linuxhelp1 ~]# firewall-cmd --reload 
success

Restart and enable your web server

Run the following command to enable the httpd service.

[root@linuxhelp1 ~]# systemctl enable httpd.service
ln -s ' /usr/lib/systemd/system/httpd.service'  ' /etc/systemd/system/multi-user.target.wants/httpd.service' 


Create a test page named as index.html and add some content to the file to check it, when the IP address calls the virtual host.

[root@linuxhelp1 ~]# vim /var/www/html/sample1/index.html
Welcome
Welcome to linuxhelp1.com
[root@linuxhelp1 ~]# vim /var/www/html/sample2/index.html
Welcome
Welcome To linuxhelp2.com

Now you can test the setup by accessing both the domains in a browser.

http://www.linuxhelp1.com
http://www.linuxhelp2.com

Now name based virtual host is configured and tested successfully.

To Setup IP Based Virtual Hosting Linux

Use the below command to check the IPs attached in your server.

[root@linuxhelp1 ~]# ip a
1: lo: < LOOPBACK,UP,LOWER_UP>  mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eno16777736: < BROADCAST,MULTICAST,UP,LOWER_UP>  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d4:e7:8a brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.190/24 brd 192.168.5.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed4:e78a/64 scope link 
       valid_lft forever preferred_lft forever
3: eno33554984: < BROADCAST,MULTICAST,UP,LOWER_UP>  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d4:e7:94 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.191/24 brd 192.168.5.255 scope global eno33554984
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed4:e794/64 scope link 
       valid_lft forever preferred_lft forever


Assign a specific IP/Port to get http requests by altering the Listen directive in the file, httpd.conf.

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


Create a Virtual host sections for both domains. Navigate to the bottom of the file and include the below virtual directives.

< VirtualHost 192.168.5.190:80> 
 servername www.linuxhelp1.com
 Documentroot /var/www/html/sample1
< /VirtualHost> 
< VirtualHost 192.168.5.191:80> 
 servername www.linuxhelp2.com
 documentroot /var/www/html/sample2
< /virtualhost> 


If you modified your main Apache conf file, you have to restart the HTTP service.

[root@linuxhelp1 ~]# systemctl restart httpd.service


Access the URL on web browser, to test your IP based Virtual hosting setup.

http://192.168.5.190
http://192.168.5.191


Now IP based virtual host is configured and tested successfully.

Tag : Virtualhost
FAQ
Q
How to Setup IP Based Virtual Hosting in Linux?
A
ans: We check the IPs attached in your server.
command: ip a
Assign a specific IP/Port to get http requests by altering the Listen directive in the file, httpd.conf.
command: vim /etc/httpd/conf/httpd.conf
Q
How to restart and enable your web server?
A
ans: run the following command to enable the httpd service.
systemctl enable httpd.service
Q
How to Setup Name-Based Virtual Host?
A
You have to create a directory under /var/www/html to keep the website files. It is a default Document Root in Apache.
mkdir /var/www/html/sample1
To Open the Apache main configuration file using vim editor to set up the Name based virtual hosting.
vim /etc/httpd/conf/httpd.conf
Add the below two virtual directives at the bottom of the file. To save and exit the file.
Q
What is name-based virtual hosting and IP based virtual hosting?
A
name-based: Name-based virtual host will host several domains/websites in one system with one IP address.
IP based: IP based virtual host is used to assign a separate IP address for each domain in a server.
Q
How to install Apache Web server in RHEL/CentOS?
A
ans: To install Apache Web server by using the following command.
yum install httpd