One server rarely hosts just one website. With Apache virtual hosts on AlmaLinux 9, a single machine can serve many domains, each with its own document root, logs and configuration. This guide walks through installing Apache (httpd), structuring your sites cleanly, creating name-based virtual hosts for two domains, handling SELinux labels, and testing everything before you point real DNS at the server. This article covers How to Set Up Apache Virtual in detail.
How Name-Based Virtual Hosts Work – How to Set Up Apache Virtual
When a browser connects, it includes the requested hostname in the HTTP Host header. Apache matches that header against the ServerName of each <VirtualHost> block and serves the matching site. Requests that match nothing fall back to the first (default) virtual host. This is why one IP address can host hundreds of unrelated websites.
Step 1: Install Apache and Open the Firewall – How to Set Up Apache Virtual
sudo dnf install -y httpd sudo systemctl enable --now httpd sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Step 2: Create the Directory Structure
Keep each site in its own directory under /var/www:
sudo mkdir -p /var/www/example.com/public_html sudo mkdir -p /var/www/example.org/public_html sudo chown -R $USER:$USER /var/www/example.com/public_html sudo chown -R $USER:$USER /var/www/example.org/public_html
Add a simple test page to each so you can tell the sites apart:
echo '<h1>Welcome to example.com</h1>' | sudo tee /var/www/example.com/public_html/index.html echo '<h1>Welcome to example.org</h1>' | sudo tee /var/www/example.org/public_html/index.html

Step 3: Create the Virtual Host Files
AlmaLinux loads extra configuration from /etc/httpd/conf.d/. Create one file per site. For the first domain, /etc/httpd/conf.d/example.com.conf:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
Create the second file the same way with example.org and its own document root and log names. Separate logs per site make troubleshooting Apache virtual hosts on AlmaLinux far easier once traffic grows.
Step 4: Set Permissions and SELinux Contexts
Apache runs as the apache user and must be able to traverse the path to your content:
sudo chmod 755 /var/www /var/www/example.com /var/www/example.org sudo chcon -R -t httpd_sys_content_t /var/www/example.com/public_html sudo chcon -R -t httpd_sys_content_t /var/www/example.org/public_html
Skipping the SELinux step is the number one reason new virtual hosts return 403 Forbidden on AlmaLinux — the labels must say the content is web content. (Our SELinux guide on this site explains contexts in depth.)
Step 5: Test and Reload Apache
sudo apachectl configtest sudo systemctl reload httpd
Syntax OK means the configuration is valid. Test name-based routing before DNS propagates by asking curl to send the right Host header:
curl -H "Host: example.com" http://127.0.0.1/ curl -H "Host: example.org" http://127.0.0.1/
Each command should return its own welcome page. Once DNS points at the server, the same sites answer in any browser. Adding HTTPS certificates per site is the natural follow-up; the workflow mirrors our Let’s Encrypt SSL guide, using Certbot’s Apache plugin on AlmaLinux.
Troubleshooting
Both domains show the same page: the ServerName values are duplicated or the second config file was not loaded — check apachectl -S to list every virtual host Apache knows about. 403 Forbidden: wrong SELinux labels or a parent directory without execute permission. Default page instead of your site: your request’s Host header matched the default vhost; verify DNS and that ServerAlias covers the www name. The AlmaLinux wiki links to upstream Apache documentation for advanced setups.
Add a Default Catch-All and Useful Redirects
Two refinements make a multi-site Apache setup behave professionally. First, add a tiny default virtual host as the first file alphabetically in conf.d (for example 00-default.conf) that answers unmatched hostnames with a maintenance page or a 404, instead of silently serving your first real site to every stray request and scanner:
<VirtualHost *:80>
ServerName default.example.invalid
DocumentRoot /var/www/default
ErrorLog /var/log/httpd/default-error.log
</VirtualHost>
Second, standardize each site’s canonical address with a redirect. Sending the www name to the bare domain (or the reverse) avoids duplicate content and cookie confusion:
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
Place this block in the same file as the site it belongs to. With a catch-all absorbing noise, clean redirects for aliases, and per-site logs from the main guide, your Apache virtual hosts on AlmaLinux behave predictably even as the number of hosted domains grows — and when something does break, apachectl -S plus the per-site error log will tell you exactly where to look within a minute.
Finally, think about resource isolation before you need it. A single misbehaving site can consume every worker on a shared Apache. The event MPM settings in /etc/httpd/conf.d/mpm.conf — especially MaxRequestWorkers — cap the total damage, and running each site’s PHP-FPM pool under its own user (a topic the LEMP and LAMP guides on this site touch on) keeps files separated even when the web server is shared. Plan these limits while traffic is small; retrofitting them during an incident is far more stressful.
Conclusion
Setting up Apache virtual hosts on AlmaLinux 9 comes down to a repeatable pattern: one directory and one config file per site, correct ownership and SELinux labels, a config test, and a Host-header check with curl. Master that pattern and you can add new websites to a server in minutes — safely and consistently.