How to Configure Apache Virtual Hosts on RHEL 8

Setup Apache Web Server Virtual Hosts on RHEL 9/RHEL 8/RHEL 7/CentOS 8/CentOS 7  

Virtual hosting means multiple websites run on single server with multiple domain names or on IP (Internet Protocol) with multiple ports at the same time. In multiple domain name websites, administrator need to be create a host entry on Domain Name Server (DNS) otherwise hostname with IP will be add on each client machines host file to open a website by use of name that is known as name base website. Virtual hosting is use single machine hardware resource (Ram, Storage and processors) that works on sharing mechanism. Due to the configuration of virtual host in a single server (machine) a hardware cost is save but delay is observe in virtual hosting environment if low amount of resources (memory and processors) used. Virtual hosting can be complicated but manageable in a single host machine.

In Apache, Virtual hosting is support on multiple website can run on a different IP (Internet Protocol), single IP (Internet Protocol) with multiple ports and name base websites.

In apache web server the directory “conf.d” is use where multiple virtual host files can be create and this directory “conf.d” location is “/etc/httpd/conf.d”, in this directory “conf.d” multiple files are available that have different main purposes. Names of these files are list below with some information about it.

ssl.conf: the file “ssl.conf” is used to provide a path of Certificate Authority (CA), Certificate and Certificate key file. This file is mainly use when apache web server is use for ssl (secure socket layer) base single website in a Linux Machine (RHEl9/RHEL8/RHEL7/CentOS8/CentOS7). In virtual hosting apache web server ssl (secure socket layer) configuration should be provide on each virtual host configuration file separately.

welcome.conf: The file “welcome.conf” is use as a default web page of apache web server, in custom configuration of Apache web server first rename this file “welcome.conf” so website will show it’s configure index page as a default page.

autoindex.conf: The main purpose of file “autoindex.conf” is that user browsing web directories and it is control the display of server generated directory listings.

userdir.conf: The file “userdir.conf” is use to allow users to access their home directories for the deployment of websites, due to this file configurations user can easily move website related files on him/her own home directory without restrictions. Actually, file “userdir.conf” allow configuration to make user home directory as a web server directory. 

README: In directory “conf.d” the file “README” is also available that give the information about the directory “conf.d” that this directory holds configuration files for the Apache HTTP Server and any file that have extension ”.conf” is work similar as httpd configuration file.   

The main configuration file of Apache web server “httpd.conf” present on directory path “/etc/httpd/conf/”. For single Apache web server the file “httpd.conf” is used otherwise for Virtual host configuration file with extension “.conf” must be created on directory path “/etc/httpd/conf.d”.

Here two scenarios are given for Virtual host configuration.

Scenario 1:

To create new virtual host configuration file “owais.conf”, first go to the directory path “/etc/httpd/conf.d” then create file with extension “.conf” or directly create a virtual host configuration file by type the below mention command.

vi /etc/httpd/conf.d/owais.conf
apache virtual hosts on rhel 8

Copy and paste the below mention code in virtual host configuration file “owais.conf”. In this scenario multiple virtual host are create on single file “owais.conf”. “Main domain” virtual host configuration web pages save on default location directory path “/var/www/html” and another “Virtual domain” virtual host configuration web pages save on different location directory path “/owais/”.

#Main domain
<VirtualHost apache1:80>
   DocumentRoot /var/www/html
   ServerName apache1.abc.org.pk
</VirtualHost>
#Virtual domain
<VirtualHost malir:80>
    DocumentRoot  /owais
    ServerName    malir.abc.org.pk
    ServerAdmin   owais@abc.org.pk
    ErrorLog logs/malir.host-error_log
    CustomLog logs/malir.host-access_log combined
</VirtualHost>

apache virtual hosts on rhel 8

As per the above virtual host configuration modify apache web server main configuration file “httpd.conf” that available on directory location “/etc/httpd/conf/”. Use editor ”vi” to modify the Apache web server configuration file “httpd.conf”.

vi /etc/httpd/conf/httpd.conf

how to configure apache web server virtual hosts on rhel 8

Comment ServerName, because this ServerName “apache1.abc.org.pk” already configure in virtual host configuration file “owais.conf”. 

#ServerName apache1.abc.org.pk:80
how to configure apache web server virtual hosts on rhel 8

Comment “DocumentRoot” also because no need to provide Document Root path on apache web server main configuration file “httpd.conf” if this define on virtual host configuration file.

#DocumentRoot "/var/www/html"
how to configure apache web server virtual hosts on rhel 7

After apply all the configuration on apache web server configuration files restart the apache web server service “httpd”, type the below mention command.

systemctl restart httpd

how to configure apache web server virtual hosts on rhel 7

As per Virtual Host “Virtual Domain” configuration, create a directory “owais” on root directory “/” by use of command “mkdir”.

mkdir /owais

how to configure apache web server virtual hosts on rhel 9

After create the directory “owais” go inside the directory by use of command “cd” then create a file “index.html” by use of command “touch”. After create the file “index.html”, modify the file “index.html” by use of editor “vi”.

cd /owais/
touch index.html
vi index.html
how to configure apache web server virtual hosts on rhel 9

Save the below mention code on file “index.html”. This is the simple html code that only text “Malir” at the center of the page.

<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Malir
</div>
</body>
</html>

how to configure apache web server virtual hosts on centos 8

Add a host entry “malir” on DNS “Domain Name Server”; otherwise add the host entry with IP (Internet Protocol) on each client machines host file for access of name base website.

how to configure apache web server virtual hosts on centos 8

To modify the SELinux configuration file, type the below mention command. SELinux configuration file available on directory path “/etc/selinux/config”.

vi /etc/selinux/config

how to configure apache web server virtual hosts on centos 7

Change the SELinux mode from enforcing to permissive, it is a best practice to set SELinux mode to permissive rather than disable it because in permissive mode warning is generated means logs.

how to configure apache web server virtual hosts on centos 7

To access the Main domain website, type the below mention URL “Uniform Resource Locator” on web browser.

http://apache1.abc.org.pk

apache virtual hosts on rhel 7

To access the Virtual domain website, type the below mention URL “Uniform Resource Locator” on web browser.

http://malir.abc.org.pk/

apache virtual hosts on rhel 7

Scenario 2:

Now create separate file for each virtual host, in this lab scenario currently single virtual host configuration file is create that is “owais.conf”. To check the virtual host configuration files go to directory “conf.d” that directory location is “/etc/httpd/conf.d” by use of command “cd” then list the content by use of command “ll”.

cd /etc/httpd/conf.d/
ll
apache virtual hosts on rhel 7

Modify virtual host configuration file “owais.conf” by use of editor “vi”.

vi owais.conf

apache virtual hosts on rhel 9

Remove virtual host configuration of “virtual domain”. The remaining code of virtual host configuration file “owais.conf” after modification is mention below.

#Main domain
<VirtualHost apache1:80>
   DocumentRoot /var/www/html
   ServerName apache1.abc.org.pk
</VirtualHost>

configuration of apache web server virtual hosts on centos 8

Create new virtual host configuration file “malir.conf” by use of command “touch” then edit the file “malir.conf” by use of editor (command) “vi”. You can create the virtual host configuration file as per your desire name.

touch malir.conf
vi malir.conf

configuration of apache web server virtual hosts on redhat 8

Save the below mention html code on newly created virtual host configuration file “malir.conf”. In this html code separate path are provide for error and access log therefore must create file “error.log” and file “access.log” on directory location “/owais/logs/malir/”.

#Virtual domain
<VirtualHost malir:80>

    ServerAdmin   root@localhost
    ServerName    malir.abc.org.pk
    #ServerAlias    malir.abc.org.pk
    DocumentRoot  /owais/
    DirectoryIndex index.html
 
    LogLevel warn
    ErrorLog /owais/logs/malir/error.log
    CustomLog /owais/logs/malir/access.log combined

    <Directory "/owais/">

        Options -Indexes +FollowSymlinks
                AllowOverride all
                Require all granted

        </Directory>

</VirtualHost>
configuration of apache web server virtual hosts on redhat 7

To access the Main domain website that configure on separate apache web server virtual host configuration file “owais.conf”, type the below mention URL “Uniform Resource Locator” on web browser.

http://apache1/

configuration of apache web server virtual hosts on centos 7

To access the Virtual domain website that configure on separate virtual host configuration file “malir.conf”, type the below mention URL “Uniform Resource Locator” on web browser.

http://malir/

Comments