CIQ

How to Serve up Multiple Websites from One Instance of Rocky Linux

How to Serve up Multiple Websites from One Instance of Rocky Linux
February 22, 2023

By now, you've read about how to install the Apache web server on Rocky Linux and are happily serving up your first web page or site from the document root directory. Although that's really cool, it only scratches the surface of what Apache can do.

Let's say you have Rocky Linux up and running and are looking to host multiple websites from that single server. Is it possible? Of course, it is. This is Linux, after all. The way you do this is via virtual hosts. What we’re going to show you is how to run two different sites, using virtual hosts, on Rocky Linux.

Requirements

In order to follow along, you'll need a running instance of Rocky Linux, a user with sudo privileges, and two different domains pointing to your server. Let's use the example domains of rockynow.com and rockyfuture.com. Obviously, you'll be using different domains, but those are good for our example. We will assume you already have Apache installed on Rocky Linux and it's already serving up a sample website.

With those things taken care of, let's get to the virtual host setups.

Creating the necessary directories and files

The first thing to do is create the necessary directories and files for our websites. Log in to your instance of Rocky Linux, open a terminal window, and create the two directories for the virtual sites with the command:

sudo mkdir -p /var/www/rockynow.com /var/www/rockyfuture.com

Next, give the new directories the proper ownership with the commands:

sudo chown apache:apache -R /var/www/rockynow.com
sudo chown apache:apache -R /var/www/rockyfuture.com

We'll now create our sample index.html files for each site. First, rockynow.com. Issue the command:

sudo nano /var/www/rockynow.com/index.html

In that file, paste the following content:

<!DOCTYPE html>
<html lang="en" dir="ltr">

  <head>

    <meta charset="utf-8">

    <title>Welcome to Rocky Linux Now</title>

  </head>

  <body>

    <h1>Rocky Linux Now!</h1>

  </body>

</html>

Save and close the file with the keyboard shortcut [Ctrl]+[X]. 

Create the second file with the command:

sudo nano /var/www/rockyfuture.com/index.html

In that file, paste the following:

<!DOCTYPE html>
<html lang="en" dir="ltr">

  <head>

    <meta charset="utf-8">

    <title>Welcome to Rocky Linux Future</title>

  </head>

  <body>

    <h1>Rocky Linux Future!</h1>

  </body>

</html>

Save and close the file.

Creating the virtual hosts file

We now must create virtual hosts files for each site. For Rocky Now, create the file with the command:

sudo nano /etc/http/conf.d/rockynow.conf

In that file, paste the following contents:

<VirtualHost *:80>
ServerName rockynow.com

  ServerAlias www.rockynow.com

  DocumentRoot /var/www/rockynow.com

</VirtualHost>

Save and close the file.

A simple explanation of each directive found about is:

  • ServerName: This is the domain name for the virtual host.

  • ServerAlias: Any other domains that will be used, such as the www subdomain.

  • DocumentRoot: The directory that houses the files served up by Apache.

Create the virtual hosts file for Rocky Future with the command:

sudo nano /etc/http/conf.d/rockyfuture.conf

In that file, paste the following contents:

<VirtualHost *:80>
ServerName rockyfuture.com

  ServerAlias www.rockyfuture.com

  DocumentRoot /var/www/rockyfuture.com

</VirtualHost>

Save and close the file.

Restart Apache with the command:

sudo systemctl restart httpd

Accessing the sites

One thing to keep in mind is that, in order to access these sites (rockynow.com, www.rockynow.com, rockyfuture.com, and www.rockyfuture.com), you would have to make sure those domain names are pointed to the IP address of the hosting server. How you do that will depend on the networking hardware/provider you use.

As well, the ServerName and ServerAlias entries in the host files are arbitrary and must match the DNS entries. ServerName is usually the domain name and ServerAlias would be an alternate name that is accepted by the server. For example, you could have a ServerName of rockyfuture.com and a ServerAlias like mail.rockyfuture.com.

Open the firewall

Finally, you might need to open the firewall to allow HTTP traffic through, which can be accomplished with the command:

sudo firewall-cmd --zone=public --add-service=http --permanent

Reload the firewall with:

sudo firewall-cmd --reload

As well, you might need to let SELinux know about our new directories with the commands:

sudo chcon -R -t httpd_sys_rw_content_t /var/www/rockynow.com
sudo chcon -R -t httpd_sys_rw_content_t /var/www/rockyfuture.com

At this point, you should be able to point your browser to both rockynow.com and rockyfuture.com and see each page. The rockynow.com address will display Rocky Linux Now! and the rockyfuture.com address will display Rocky Linux Future!

And that's all there is to hosting multiple websites with Rocky Linux, via Apache virtual hosts.

Related posts

2023 Holiday Gift Guide for Rocky Linux Users

2023 Holiday Gift Guide for Rocky Linux Users

Dec 19, 2023

Rocky Linux

Why Rocky Linux Is a Rock-Solid Choice in an Economic Downturn

Why Rocky Linux Is a Rock-Solid Choice in an Economic Downturn

Jan 18, 2023

Rocky Linux

6 Signs That It's Time to Move to Rocky Linux

6 Signs That It's Time to Move to Rocky Linux

Feb 23, 2023

Rocky Linux

123
54
>>>