How to Configure Apache Virtual Hosts on Rocky Linux

This is an optional step intended only for those who wish to host multiple sites on the same server. So far, our LAMP setup can only host one site. If you wish to host multiple sites, then you need to set up or configure virtual host files. Apache virtual host files encapsulate the configurations of multiple websites.

For this section, we will create an Apache virtual host file to demonstrate how you can go about setting your virtual hosts in Rocky Linux.


Note: In our setup, we are using the domain name which is pointed to the public IP of our virtual server. Be sure to use your own domain name in all the instances where our domain name appears.

Creating an Apache Virtual Directory Structure

The first step is to create a directory that will accommodate the website or domain’s files. This will be the DocumentRoot which will be in the /var/www/ path. Therefore run the following command.

$ sudo mkdir -p /var/www/

Next, we will create a simple index.html file which we shall use to test our virtual host file.

$ sudo vim /var/www/

Insert the following HTML lines.

<!DOCTYPE html>
<html lang="en">
    <title>Welcome to!</title>
    <h1>Success! The virtual host is active and running!</h1>

Save the HTML file and exit.

Then assign the permissions to the currently logged-in user to allow them to edit the webroot directories without permission hiccups.

$ sudo chown -R $USER:$USER /var/www/

Creating an Apache Virtual Host File

At this point, we will create a separate virtual host file for our domain. By default, Rocky Linux 8, just like CentOS 8, loads all its configurations from the /etc/httpd/conf.d directory.

So, proceed and create a separate virtual host file.

$ sudo vim /etc/httpd/conf.d/

Paste the content below to define the virtual host.

<VirtualHost *:80>
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All

    ErrorLog /var/log/httpd/
    CustomLog /var/log/httpd/ combined

Save the changes and exit the virtual host file.

To check whether all the configurations are sound, execute the command:

$ sudo apachectl configtest
Check Apache Configuration in Rocky Linux
Check Apache Configuration in Rocky Linux

Next, restart Apache to effect the changes made.

$ sudo systemctl restart httpd

Then launch your web browser and browse your domain as follows:

This should display the sample HTML page we configured in step 1 of this section. This is ironclad proof that our virtual host setup is working!

Check Apache Virtual Host Domain
Check Apache Virtual Host Domain

If you have multiple domain names, repeat the same steps to set up virtual host files for each domain or website.


And there you have it. We have successfully configured virtual host files to host several websites or domains in Rocky Linux 8 with the LAMP stack. You can proceed to host your web applications or secure your Apache with an SSL Certificate using free Let’s Encrypt.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.