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.

Requirements

Note: In our setup, we are using the domain name tecmint.info 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/tecmint.info/html

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

$ sudo vim /var/www/tecmint.info/html/index.html

Insert the following HTML lines.

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

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/tecmint.info/html

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/tecmint.info.conf

Paste the content below to define the virtual host.

<VirtualHost *:80>
    ServerName www.tecmint.info
    ServerAlias tecmint.info
    DocumentRoot /var/www/tecmint.info/html

    <Directory /var/www/tecmint.info/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/tecmint.info-error.log
    CustomLog /var/log/httpd/tecmint.info-access.log combined
</VirtualHost>

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:

http://tecmint.info

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.

Conclusion

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.

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.