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.
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.
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/htmlServerName apache1.abc.org.pk</VirtualHost>#Virtual domain<VirtualHost malir:80>DocumentRoot /owaisServerName malir.abc.org.pkServerAdmin firstname.lastname@example.orgErrorLog logs/malir.host-error_logCustomLog logs/malir.host-access_log combined</VirtualHost>
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”.
Comment ServerName, because this ServerName “apache1.abc.org.pk” already configure in virtual host configuration file “owais.conf”.
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.
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
As per Virtual Host “Virtual Domain” configuration, create a directory “owais” on root directory “/” by use of command “mkdir”.
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.htmlvi index.html
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>
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.
To modify the SELinux configuration file, type the below mention command. SELinux configuration file available on directory path “/etc/selinux/config”.
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.
To access the Main domain website, type the below mention URL “Uniform Resource Locator” on web browser.
To access the Virtual domain website, type the below mention URL “Uniform Resource Locator” on web browser.
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”.
Modify virtual host configuration file “owais.conf” by use of editor “vi”.
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/htmlServerName apache1.abc.org.pk</VirtualHost>
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.confvi malir.conf
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@localhostServerName malir.abc.org.pk#ServerAlias malir.abc.org.pkDocumentRoot /owais/DirectoryIndex index.htmlLogLevel warnErrorLog /owais/logs/malir/error.logCustomLog /owais/logs/malir/access.log combined<Directory "/owais/">Options -Indexes +FollowSymlinksAllowOverride allRequire all granted</Directory></VirtualHost>
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.
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.