How to Create Multiple Virtualhost on Fedora 34

To Implement Multiple Virtual Hosts on Fedora 34.

Introduction:

Virtual hosting in Apache is the concept of hosting multiple sites on a single server. There are two types of virtual hosting: IP address hosting and domain hosting, so either IP addresses or domain names can be hosted.

Installation Procedure:

Step 1: To check OS version.

[root@linuxhelp ~]# cat /etc/os-release
NAME=Fedora
VERSION="34 (Workstation Edition)"
ID=fedora
VERSION_ID=34
VERSION_CODENAME=""
PLATFORM_ID="platform:f34"
PRETTY_NAME="Fedora 34 (Workstation Edition)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:34"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/34/system-administrators-guide/"
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

Step 2: To install apache service.

[root@linuxhelp ~]# dnf install httpd -y
Last metadata expiration check: 0:30:28 ago on Wed 10 Nov 2021 01:33:26 PM IST.
Dependencies resolved.
================================================================================
 Package                               Arch    Version           Repo      Size
================================================================================
Installing:
 httpd                                 x86_64  2.4.51-1.fc34     updates  1.4 M
Installing dependencies:
 apr                                   x86_64  1.7.0-9.fc34      updates  118 k
 apr-util                              x86_64  1.6.1-16.fc34     fedora    97 k
 fedora-logos-httpd                    noarch  34.0.4-1.fc34     updates   15 k
 httpd-filesystem                      noarch  2.4.51-1.fc34     updates   12 k
 httpd-tools                           x86_64  2.4.51-1.fc34     updates   80 k
 julietaula-montserrat-fonts-common    noarch  1:7.210-4.fc34    fedora    12 k
Installing weak dependencies:
 apr-util-bdb                          x86_64  1.6.1-16.fc34     fedora    12 k
 apr-util-openssl                      x86_64  1.6.1-16.fc34     fedora    15 k
 julietaula-montserrat-base-web-fonts  noarch  1:7.210-4.fc34    fedora   918 k
 mod_http2                             x86_64  1.15.24-1.fc34    updates  150 k
 mod_lua                               x86_64  2.4.51-1.fc34     updates   60 k

Transaction Summary
================================================================================
Install  12 Packages

Total download size: 2.8 M
Installed size: 6.9 M
Downloading Packages:
(1/12): fedora-logos-httpd-34.0.4-1.fc34.noarch 9.0 kB/s |  15 kB     00:01    
(2/12): apr-1.7.0-9.fc34.x86_64.rpm              38 kB/s | 118 kB     00:03    
(3/12): httpd-filesystem-2.4.51-1.fc34.noarch.r 7.2 kB/s |  12 kB     00:01    
(4/12): httpd-tools-2.4.51-1.fc34.x86_64.rpm    114 kB/s |  80 kB     00:00    
(5/12): mod_http2-1.15.24-1.fc34.x86_64.rpm     143 kB/s | 150 kB     00:01    
(6/12): mod_lua-2.4.51-1.fc34.x86_64.rpm         57 kB/s |  60 kB     00:01    
(7/12): apr-util-bdb-1.6.1-16.fc34.x86_64.rpm    23 kB/s |  12 kB     00:00    
(8/12): apr-util-openssl-1.6.1-16.fc34.x86_64.r  55 kB/s |  15 kB     00:00    
(9/12): apr-util-1.6.1-16.fc34.x86_64.rpm        78 kB/s |  97 kB     00:01    
(10/12): julietaula-montserrat-fonts-common-7.2  45 kB/s |  12 kB     00:00    
(11/12): julietaula-montserrat-base-web-fonts-7 403 kB/s | 918 kB     00:02    
(12/12): httpd-2.4.51-1.fc34.x86_64.rpm         131 kB/s | 1.4 MB     00:10    
--------------------------------------------------------------------------------
Total                                           236 kB/s | 2.8 MB     00:12     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : apr-1.7.0-9.fc34.x86_64                               1/12 
  Installing       : apr-util-bdb-1.6.1-16.fc34.x86_64                     2/12 
  Installing       : apr-util-openssl-1.6.1-16.fc34.x86_64                 3/12 
  Installing       : apr-util-1.6.1-16.fc34.x86_64                         4/12 
  Installing       : httpd-tools-2.4.51-1.fc34.x86_64                      5/12 
  Installing       : julietaula-montserrat-fonts-common-1:7.210-4.fc34.    6/12 
  Installing       : julietaula-montserrat-base-web-fonts-1:7.210-4.fc3    7/12 
  Installing       : fedora-logos-httpd-34.0.4-1.fc34.noarch               8/12 
  Running scriptlet: httpd-filesystem-2.4.51-1.fc34.noarch                 9/12 
  Installing       : httpd-filesystem-2.4.51-1.fc34.noarch                 9/12 
  Installing       : mod_http2-1.15.24-1.fc34.x86_64                      10/12 
  Installing       : mod_lua-2.4.51-1.fc34.x86_64                         11/12 
  Installing       : httpd-2.4.51-1.fc34.x86_64                           12/12 
  Running scriptlet: httpd-2.4.51-1.fc34.x86_64                           12/12 
  Verifying        : apr-1.7.0-9.fc34.x86_64                               1/12 
  Verifying        : fedora-logos-httpd-34.0.4-1.fc34.noarch               2/12 
  Verifying        : httpd-2.4.51-1.fc34.x86_64                            3/12 
  Verifying        : httpd-filesystem-2.4.51-1.fc34.noarch                 4/12 
  Verifying        : httpd-tools-2.4.51-1.fc34.x86_64                      5/12 
  Verifying        : mod_http2-1.15.24-1.fc34.x86_64                       6/12 
  Verifying        : mod_lua-2.4.51-1.fc34.x86_64                          7/12 
  Verifying        : apr-util-1.6.1-16.fc34.x86_64                         8/12 
  Verifying        : apr-util-bdb-1.6.1-16.fc34.x86_64                     9/12 
  Verifying        : apr-util-openssl-1.6.1-16.fc34.x86_64                10/12 
  Verifying        : julietaula-montserrat-base-web-fonts-1:7.210-4.fc3   11/12 
  Verifying        : julietaula-montserrat-fonts-common-1:7.210-4.fc34.   12/12 

Installed:
  apr-1.7.0-9.fc34.x86_64                                                       
  apr-util-1.6.1-16.fc34.x86_64                                                 
  apr-util-bdb-1.6.1-16.fc34.x86_64                                             
  apr-util-openssl-1.6.1-16.fc34.x86_64                                         
  fedora-logos-httpd-34.0.4-1.fc34.noarch                                       
  httpd-2.4.51-1.fc34.x86_64                                                    
  httpd-filesystem-2.4.51-1.fc34.noarch                                         
  httpd-tools-2.4.51-1.fc34.x86_64                                              
  julietaula-montserrat-base-web-fonts-1:7.210-4.fc34.noarch                    
  julietaula-montserrat-fonts-common-1:7.210-4.fc34.noarch                      
  mod_http2-1.15.24-1.fc34.x86_64                                               
  mod_lua-2.4.51-1.fc34.x86_64                                                  
Complete!

Step 3: To start the apache service.

[root@linuxhelp ~]# systemctl start httpd

Step 4: To enable apache service.

 [root@linuxhelp ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

Step 5: To check apache status.

 [root@linuxhelp ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor pre>
     Active: active (running) since Wed 2021-11-10 14:06:46 IST; 19s ago
       Docs: man:httpd.service(8)
   Main PID: 12027 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes>
      Tasks: 177 (limit: 4965)
     Memory: 13.9M
        CPU: 101ms
     CGroup: /system.slice/httpd.service
             ├─12027 /usr/sbin/httpd -DFOREGROUND
             ├─12028 /usr/sbin/httpd -DFOREGROUND
             ├─12029 /usr/sbin/httpd -DFOREGROUND
             ├─12030 /usr/sbin/httpd -DFOREGROUND
             └─12031 /usr/sbin/httpd -DFOREGROUND

Step 6: To make new directory sample1 and sample2.

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

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

Step 7: To open apache configuration file and create virtualhost.

[root@linuxhelp ~]# vim /etc/httpd/conf/httpd.conf
<VirtualHost 192.168.6.125:80> 
 servername www.linuxhelp1.com
 Documentroot /var/www/html/sample1
</VirtualHost> 
<VirtualHost *:80> or your ip 
 servername www.linuxhelp2.com
 documentroot /var/www/html/sample2
</virtualhost> 

Step 8: To check apache Syntex.

[root@linuxhelp ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::e2eb:b5d6:40ab:3b84%ens33. Set the 'ServerName' directive globally to suppress this message
Syntax OK
 

Step 9: To Start and Reload firewall service.

[root@linuxhelp ~]# firewall-cmd --permanent --add-service=http
Success
[root@linuxhelp ~]# firewall-cmd --reload
Success

Step 10: To Enable apache service.

[root@linuxhelp ~]# systemctl enable httpd.service

Step 11: To open index.html file and create any content.

[root@linuxhelp ~]# vim /var/www/html/sample1/index.html
[root@linuxhelp ~]# vim /var/www/html/sample2/index.html

Step 12: Check your IP address.

[root@linuxhelp ~]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:a7:45:98 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.6.125/23 brd 192.168.7.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::e2eb:b5d6:40ab:3b84/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

Step 13: To restart httpd.service.

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

Step 14: Go to browser then browse your virtualhost domains.

With this the implement of multiple virtual hosts comes to an end

FAQ
Q
What is Mod_vhost_alias?
A
mod_vhost_alias. The VirtualDocumentRoot directive allows you to determine where Apache HTTP Server will find your documents based on the value of the server name.
Q
How do I get Apache to listen on multiple ports?
A
Open configure file /etc/httpd/conf/httpd.conf and modify the Listen directive tells the server to accept incoming requests on the specified port.
Multiple Listen directives may be used to specify the number of ports to listen.
Q
How do I find the Apache virtual host?
A
/etc/httpd/conf/httpd.conf is the default location for Apache Virtual Host configuration files on Fedora systems
Q
What is the maximum number of virtual hosts that Apache can handle?
A
Each virtual host may have its own log, so the limit may be 64 based on file descriptor limits. However, you can use this guide to configure Apache to run more processes.
Q
What is a name-based virtual host?
A
Name-based Virtual Hosting is used to serve different websites hosted on the same IP address or Port.