dotlinux blog

How to Configure Apache Virtual Hosts on Rocky Linux

If you’ve ever wanted to host multiple websites on a single Rocky Linux server, Apache Virtual Hosts are the solution you need. Virtual Hosts allow you to run multiple domains or subdomains from a single server instance, each with its own content, configuration, and logs—without requiring separate hardware for each site.

Rocky Linux, a stable, enterprise-grade distribution based on Red Hat Enterprise Linux (RHEL), is a popular choice for hosting web applications thanks to its reliability and security. This step-by-step guide will walk you through every aspect of setting up Apache Virtual Hosts on Rocky Linux, from installing Apache to testing your first sites. Whether you’re hosting a personal blog, a business website, or testing multiple projects, this guide will ensure your virtual hosts are configured correctly and securely.


2026-03

Table of Contents#

  1. Prerequisites
  2. Install the Apache Web Server
  3. Configure the Firewall
  4. Create a Directory Structure for Your Websites
  5. Set Up Proper Permissions
  6. Create Sample HTML Files for Testing
  7. Create Virtual Host Configuration Files
  8. Test Apache Configuration and Restart the Service
  9. Verify Your Virtual Hosts Are Working
  10. Troubleshooting Common Issues
  11. Conclusion
  12. References

1. Prerequisites#

Before you start, ensure you have the following:

  • A Rocky Linux 8 or 9 server (physical, virtual, or cloud-based).
  • A non-root user with sudo privileges (for secure administration).
  • One or more domain names (e.g., example.com, test.com) pointed to your server’s public IP address (or use local domain names for testing).
  • Basic familiarity with the Linux command line.
  • If using a cloud server: Ensure your cloud provider’s security group allows incoming traffic on ports 80 (HTTP) and 443 (HTTPS).

2. Install the Apache Web Server#

Apache is available in Rocky Linux’s default software repositories under the package name httpd. Follow these steps to install and enable it:

Step 2.1: Update System Packages#

First, update your system’s package index to ensure you install the latest version of Apache:

sudo dnf update -y

Step 2.2: Install Apache#

Install the Apache package using dnf:

sudo dnf install httpd -y

Step 2.3: Enable and Start Apache#

Enable Apache to start automatically on system boot, then start the service:

sudo systemctl enable httpd --now

Step 2.4: Verify Apache is Running#

Check the status of the Apache service to confirm it’s active:

sudo systemctl status httpd

You should see a status message indicating active (running).


3. Configure the Firewall#

Rocky Linux uses firewalld as its default firewall. To allow incoming HTTP and HTTPS traffic, you need to open the corresponding ports:

Step 3.1: Allow HTTP and HTTPS Traffic#

Run the following commands to permanently allow HTTP (port 80) and HTTPS (port 443) services:

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent

Step 3.2: Reload the Firewall#

Apply the changes by reloading firewalld:

sudo firewall-cmd --reload

Step 3.3: Verify Firewall Settings#

Check that the services are allowed:

sudo firewall-cmd --list-services

You should see http and https in the output.


4. Create a Directory Structure for Your Websites#

Apache uses /var/www as the default root directory for web content. For virtual hosts, we’ll create a separate directory for each domain, with a public_html subdirectory as the document root (where your site’s public files will live).

For example, for example.com and test.com:

sudo mkdir -p /var/www/example.com/public_html
sudo mkdir -p /var/www/test.com/public_html
  • /var/www/[domain]/: The parent directory for all files related to the domain.
  • /var/www/[domain]/public_html/: The document root where your site’s HTML, CSS, and JavaScript files are stored.

5. Set Up Proper Permissions#

Apache runs as the apache user on Rocky Linux, so it needs read access to your site’s files. You’ll also want your non-root user to have write access to edit site files.

Step 5.1: Set Directory Ownership#

Change the group ownership of your /var/www directories to the apache group, and keep your user as the owner:

sudo chown -R $USER:apache /var/www/example.com
sudo chown -R $USER:apache /var/www/test.com

Step 5.2: Set File Permissions#

Set permissions so your user can read/write files, and the Apache group can read/execute files:

sudo chmod -R 755 /var/www

Step 5.3: Configure SELinux Contexts#

Rocky Linux enables SELinux by default, which blocks Apache from accessing files unless the correct security context is set. First, install the policycoreutils-python-utils package (if missing) to manage SELinux contexts:

sudo dnf install policycoreutils-python-utils -y

Then, set the appropriate SELinux context for your web directories:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www(/.*)?"
sudo restorecon -Rv /var/www

This ensures Apache can access and serve files from your document roots.


6. Create Sample HTML Files for Testing#

Create a simple index.html file for each domain to verify your virtual hosts are working.

For example.com:#

nano /var/www/example.com/public_html/index.html

Paste the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Example.com</title>
</head>
<body>
    <h1>Success! The example.com Virtual Host is Working.</h1>
    <p>This is the default page for your example.com domain.</p>
</body>
</html>

Save and exit the editor (press Ctrl+O, then Enter, then Ctrl+X).

For test.com:#

nano /var/www/test.com/public_html/index.html

Paste this content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Test.com</title>
</head>
<body>
    <h1>Success! The test.com Virtual Host is Working.</h1>
    <p>This is the default page for your test.com domain.</p>
</body>
</html>

Save and exit.


7. Create Virtual Host Configuration Files#

Apache reads virtual host configuration files from /etc/httpd/conf.d/ (any file ending with .conf will be loaded). Create a separate configuration file for each domain.

Step 7.1: Create a Configuration File for example.com#

sudo nano /etc/httpd/conf.d/example.com.conf

Paste the following configuration:

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/example.com/public_html
    ServerName example.com
    ServerAlias www.example.com
 
    # Logging configuration
    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
 
    # Allow access to the document root
    <Directory /var/www/example.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 7.2: Create a Configuration File for test.com#

sudo nano /etc/httpd/conf.d/test.com.conf

Paste this configuration (replace test.com with your domain):

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/test.com/public_html
    ServerName test.com
    ServerAlias www.test.com
 
    ErrorLog /var/log/httpd/test.com-error.log
    CustomLog /var/log/httpd/test.com-access.log combined
 
    <Directory /var/www/test.com/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Step 7.3: Disable the Default Welcome Page (Optional)#

By default, Apache shows a welcome page when visitors access your server’s IP address. To disable it:

sudo mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak

8. Test Apache Configuration and Restart the Service#

Before restarting Apache, always test your configuration files for syntax errors to avoid downtime:

sudo apachectl configtest

If you see Syntax OK, restart Apache to apply the changes:

sudo systemctl restart httpd

If there’s an error, the command will display a message pointing to the issue (e.g., a missing semicolon or typo in your configuration).


9. Verify Your Virtual Hosts Are Working#

Now, test your virtual hosts to ensure they serve the correct content.

Option 1: Using a Web Browser#

If your domains are pointed to your server’s public IP, open a browser and visit:

  • http://example.com (you should see the example.com welcome page)
  • http://test.com (you should see the test.com welcome page)

Option 2: Local Testing (Without a Public Domain)#

If you’re testing locally, edit your machine’s hosts file to map the domains to your server’s IP:

  • Linux/macOS: Open /etc/hosts with sudo nano /etc/hosts and add:
    192.168.1.100 example.com test.com
    
    Replace 192.168.1.100 with your Rocky Linux server’s local IP.
  • Windows: Open C:\Windows\System32\drivers\etc\hosts as Administrator and add the same line.

Then, visit the domains in your browser as above.

Option 3: Using curl (Command Line)#

On the Rocky Linux server, run:

curl http://example.com
curl http://test.com

You should see the HTML content of your sample pages in the output.


10. Troubleshooting Common Issues#

If your virtual hosts aren’t working, check these common issues:

  1. Syntax Errors in Configuration: Run sudo apachectl configtest to identify and fix syntax issues.
  2. SELM Blocking Access: Reapply SELinux contexts with sudo restorecon -Rv /var/www.
  3. Firewall Blocking Traffic: Verify HTTP/HTTPS are allowed with sudo firewall-cmd --list-services.
  4. Incorrect Permissions: Double-check ownership with ls -l /var/www and ensure the apache group has read access.
  5. Domain Not Resolving: Use nslookup example.com to confirm your domain points to the correct IP.

11. Conclusion#

Congratulations! You’ve successfully configured Apache Virtual Hosts on Rocky Linux. You can now host multiple websites on a single server, each with its own isolated content and configuration.

Next steps to enhance your setup include:

  • Enabling HTTPS: Use Let’s Encrypt and Certbot to install free SSL certificates for your domains (run sudo dnf install certbot python3-certbot-apache to get started).
  • Adding More Virtual Hosts: Repeat steps 4–8 for additional domains.
  • Securing Apache: Implement hardening measures like disabling directory listing, using mod_security, or configuring password protection for sensitive directories.

12. References#