Configure Multiple Websites on a Single RHEL-Based Apache Host
At some point, in your development or admin career, you’re going to have to host more than one website on a single server, virtual machine, or even container, probably using the Apache HTTP Server. When that happens, you have a few options at your disposal. You could deploy each website as a separate container or you could go the old-school route and use Apache’s virtual host option.
Sure, there are better ways to do this now but Apache virtual hosts is one of the most reliable methods and it’s also a skill you’ll be glad you have. Not only will it get you better acquainted with the web server you’ll also up your Linux game in the process (and who doesn’t want or need that)?
Say, for example, you need to host more than one internal site (say, one for HR and one for CRM) and you need to do it on the same server. This is possible with Apache virtual hosts.
Let me show you how this is done.
What You Need
To follow along, you’ll need the following things:
- A machine with a RHEL-based OS installed (such as Red Hat Enterprise Linux, Rocky Linux, AlmaLinux, CentOS Stream, or Oracle Linux).
- A user with sudo privileges.
- An FQDN with your network hardware directing that domain to the hosting server. For this example, we’ll use the domains newstackexample1.com and newstackexample2.com. Keep in mind that you’ll need to replace these domains with whatever domains you’ve chosen.
That’s it. Let’s get to work.
Create the Required Directories
The first thing we must do is create the directories that will house our virtual hosts. Log into your server and (if necessary) open a terminal window. We’re going to create directories named newstackexample1.com and newstackexample2.com with the following commands:
sudo mkdir -p /var/www/newstackexample1.com sudo mkdir -p /var/www/newstackexample2.com
With our directories created, we then need to change the ownership so they belong to the apache user and group. This can be done with the following commands:
sudo chown apache:apache -R /var/www/newstackexample1.com sudo chown apache:apache -R /var/www/newstackexample2.com
Outstanding.
Create index.html Files for Each Virtual Host
Our next task is to create index files for each virtual host. These two files will both be named index.html.
Create the first file with the command:
sudo nano /var/www/newstackexample1.com/index.html
In that file, paste the following:
Save and close the file.
Create the second file with:
sudo nano /var/www/newstackexample2.com/index.html
In that file, paste the following:
Save and close the file.
Configuring the Virtual Hosts
The next step is to create the virtual hosts file for each of our new sites. We’ll create the file for web1 with the command:
sudo nano /etc/http/conf.d/newstackexample1.conf
In that file, paste the following:
Save and close the file.
It’s important to note that ServerName will be your domain name (such as newstackexample1.com) and ServerAlias will be an alternate name that is also accepted by the server (such as www.newstackexample1.com or mail.newstackexample1.com).
Create the virtual host config file for the second host with the command:
sudo nano /etc/http/conf.d/newstackexample2.conf
In that file, paste the following:
Save and close the file.
With our indexes and configuration files created, restart Apache with the command:
sudo systemctl restart httpd
The Apache service should restart without complaint.
Opening the Firewall
By default, your firewall is most likely not configured to allow HTTP traffic through. To open the firewall for port 80, issue the command:
sudo firewall-cmd --zone=public --add-service=http --permanent
Reload the firewall with:
sudo firewall-cmd --reload
Configure SELinux
Because we’re using a RHEL-based distribution, we also have to deal with the SELinux security framework. Unless you take care of this, traffic will not be able to reach the directories housing the websites. To fix this, issue the following commands:
sudo chcon -R -t httpd_sys_rw_content_t /var/www/newstackexample1.com
sudo chcon -R -t httpd_sys_rw_content_t /var/www/newstackexample2.com
Accessing the Sites
With everything in place (including your network hardware directing the domains to the correct server IP address), you should now be able to view the sites by pointing your browser to either newstackexample1.com or newstackexample2.com. When newstackexample1.com loads you should see Welcome to New Stack Example 1 and when newstackexample2.com loads you should see Welcome to New Stack Example 2.
Congratulations, you’ve just deployed your first virtual hosts with the Apache web server and a RHEL-based Linux distribution. This skill can be very helpful when you need to host multiple internal sites or even external sites for different clients on the same server.