TNS
VOXPOP
Where are you using WebAssembly?
Wasm promises to let developers build once and run anywhere. Are you using it yet?
At work, for production apps
0%
At work, but not for production apps
0%
I don’t use WebAssembly but expect to when the technology matures
0%
I have no plans to use WebAssembly
0%
No plans and I get mad whenever I see the buzzword
0%
Linux

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. Here is how.
Dec 16th, 2023 6:00am by
Featued image for: Configure Multiple Websites on a Single RHEL-Based Apache Host
Feature image courtesy of the Apache Software Foundation.

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:


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:


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:


In that file, paste the following:

Save and close the file.

Create the second file with:


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:


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:


In that file, paste the following:

Save and close the file.

With our indexes and configuration files created, restart Apache with the command:


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:


Reload the firewall with:

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:


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.

Group Created with Sketch.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.