Hi there, In this article, I’ll show you how to install Apache Web Server on CentOS 8 and configuring virtual hosts.
A virtual host is a feature for hosting more than one website in a single web server. This is a special feature in the apache web server. Let’s move for the installation steps.
- Change the Host Name
- Install Apache Web Server
- Add firewall Rules
- Start the Service
- Test the Service
- Add Virtual Host
- Test the Virtual Host
1. Change the Host Name
First of all, we need to add a specific hostname to our centos machine. Use the following command to add a hostname. I’ll use a hostname called “server.redhatlive.com”. You can use your one.
hostnamectl set-hostname server.redhatlive.com
2. Install Apache Web Server
Apache Web server is available in the CentOS repositories. Therefore It’s easy to install using a single command.
yum group install “Basic web server” -y
3. Add firewall Rules
After Installation process we need to add a firewall rule for httpd service. In this case I add http and https services via firewall.
firewall-cmd –add-service=http –permanent
firewall-cmd –add-service=https –permanent
4. Start the Service
After adding firewall rules, We can start the service called “httpd”. Use the following command.
systemctl start httpd
Service is successfully started and also running in background.
5. Test the Service
If you successfully completed the above steps, Now we can test the apache webserver using the firefox browser. Open the firefox, type hostname, and hit enter. If you correctly performed the above steps, the following screen will appears.
6. Add Virtual Host
Virtual Hosting is a feature in the apache web server. It allows you to host more that one website in a single web server. Now we’re going to add a virtual host into our apache server.
Create a directory for new website.
mkdir -p /var/www/html/redhatlive.com/public_html
In this case domain name of the new site is redhatlive.com and the document root located in /var/www/html/redhatlive.com/public_html/ directory.
The document root is a directory where your public access files are located(index.html file is located there). Therefore I’ll add an index.html file into public_html directory.
I will add some dummy html tags into index.html file for clearly render the output.
Add Virtual Host Config file.
After configuring directory for new site, We need to add configs for our new virtual host. Create a new file using following command. All virtual hosts configs must be resides there.
Add the following code block into vhost.conf file. This is the code for adding a new virtual host to the apache web server. Change the hostname(redhatlive.com) as yours.
NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin [email protected] ServerName redhatlive.com ServerAlias www.redhatlive.com DocumentRoot /var/www/html/redhatlive.com/public_html/ </VirtualHost>
7. Test the Virtual Host
Save the file and open the firefox browser for test the new virtual host.
That’s all about installing Apache server on centOS 8 and adding new virtual host. There are many ways to do above steps, but I show you a easiest way to do it. If you have any questions, leave a comment below.