Apache virtual hosting in CentOS lets you run multiple websites on one server, each with its own domain name, document root, and configuration. This guide explains how to set up name-based virtual hosts on CentOS 7, 8, and 9, covering the configuration files, directory permissions, SELinux contexts, and common troubleshooting.
What Is Virtual Hosting and Why Use It
Virtual hosting is Apache’s method of serving more than one website from a single IP address or server. Instead of installing a separate web server for each site, you define multiple VirtualHost blocks in Apache’s configuration. Each block tells Apache which domain name (or IP) maps to which directory on disk.
There are two main types:
- Name-based – the server uses the Host header sent by the browser to pick the right site. This is what most modern setups use because it works with a single IP.
- IP-based – each site gets its own IP address. This is rarer and requires multiple network interfaces or IP aliases.
On CentOS, name-based virtual hosting is the standard approach. It lets you host a personal blog, a company site, and an API on the same VPS or dedicated server without extra hardware.
Prerequisites and Initial Setup
Before creating virtual hosts, you need a working Apache installation. On CentOS 7, the package is httpd; on CentOS 8 and 9, it is also httpd but with newer defaults. Install it with:
sudo dnf install httpd # CentOS 8/9
sudo yum install httpd # CentOS 7
Then start and enable the service:
sudo systemctl start httpd
sudo systemctl enable httpd
You also need a directory for each site. A common convention is /var/www/example.com/public_html. Create it and set ownership to your user or to the Apache user (apache):
sudo mkdir -p /var/www/example.com/public_html
Add a simple index.html to test later.
Creating the Virtual Host Configuration Files
On CentOS, the main Apache configuration file is /etc/httpd/conf/httpd.conf. It usually includes a line that loads all files ending in .conf from /etc/httpd/conf.d/. That’s where you should put your virtual host definitions.
Create a file called /etc/httpd/conf.d/example.com.conf with this content:
<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>
Replace example.com with your actual domain. The *:80 means this virtual host listens on all IP addresses on port 80. If you have multiple sites, each gets its own .conf file or you can put several blocks in one file.
After creating the file, test the configuration syntax:
sudo apachectl configtest
If you see Syntax OK, reload Apache to apply changes:
sudo systemctl reload httpd
Setting Up Name-Based Virtual Hosts with Multiple Domains
To host several sites on the same IP, you simply add more VirtualHost blocks. Each block must have a unique ServerName. For example, to add a second site called blog.example.org, create another file or add another block:
<VirtualHost *:80>
ServerName blog.example.org
DocumentRoot /var/www/blog.example.org/public_html
ErrorLog /var/log/httpd/blog.example.org_error.log
CustomLog /var/log/httpd/blog.example.org_access.log combined
</VirtualHost>
Make sure the directory exists and has an index file. The first virtual host defined becomes the default for any request that doesn’t match a known ServerName – so if you want a catch-all site, put it first.
You also need to configure DNS so that each domain points to your server’s IP address. Without that, the browser won’t know where to send the request, and Apache won’t see the correct Host header.
Directory Permissions and SELinux Context
CentOS ships with SELinux enabled by default, which can block Apache from reading your site files if the security context is wrong. The standard web directories like /var/www/html already have the correct context. For custom directories, you need to set the httpd_sys_content_t type.
Check the current context with:
ls -Z /var/www/example.com
If it shows unconfined_u:object_r:default_t or similar, apply the correct context:
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com
If your site needs to write files (e.g., a CMS), you may need httpd_sys_rw_content_t instead. Also ensure the files are readable by the Apache user. A typical permission set is 755 for directories and 644 for files, owned by apache:apache or root:apache.
Common Problems and How to Fix Them
Here are the most frequent issues when setting up virtual hosts on CentOS:
- You see the default Apache page instead of your site. This usually means Apache isn’t matching your virtual host. Check that
ServerNamematches the domain exactly, and that the request comes to port 80. Also verify that theDocumentRootpath exists and has an index file. - Permission denied errors in the logs. Check SELinux context with
ls -Zand file ownership. Runsudo restorecon -Rvon the directory. - Syntax errors after editing. Run
apachectl configtest– it will tell you the exact line number. - Site works locally but not externally. Ensure your firewall allows port 80 (and 443 for HTTPS). Use
sudo firewall-cmd --add-service=http --permanentand reload. - Multiple sites all show the same content. That happens when you have duplicate
ServerNamevalues or when the first virtual host catches everything. Make sure eachServerNameis unique.
SSL/TLS and HTTPS Virtual Hosts
Modern websites should use HTTPS. On CentOS, the easiest way is to install Certbot and obtain a free Let’s Encrypt certificate. First, install the EPEL repository if needed:
sudo dnf install epel-release # CentOS 8/9
sudo yum install epel-release # CentOS 7
Then install Certbot and the Apache plugin:
sudo dnf install certbot python3-certbot-apache
Run it for each domain:
sudo certbot --apache -d example.com -d www.example.com
Certbot will automatically modify your virtual host configuration to add the SSL certificate and redirect HTTP to HTTPS. After that, you’ll have a VirtualHost block on port 443 with SSLEngine on and the certificate file paths.
If you prefer manual configuration, you can create a port 443 virtual host block and point it to your certificate and key files. But using Certbot is more reliable and keeps certificates renewed automatically.
Frequently Asked Questions
How do I set up virtual hosts on CentOS 9?
CentOS 9 uses the same httpd package and the same /etc/httpd/conf.d/ directory. The configuration syntax is identical. The main difference is that you use dnf instead of yum, and the default PHP version is newer. Follow the same steps: create a .conf file, test with apachectl configtest, reload.
Why is my virtual host not working even though the config looks right?
Check three things: 1) the ServerName must match the domain in the browser’s URL; 2) the DocumentRoot must exist and contain an index file; 3) SELinux must allow Apache to read the files. Run sudo tail -f /var/log/httpd/error_log while accessing the site – it will show the exact error.
Can I use a single IP for multiple HTTPS sites?
Yes, with Server Name Indication (SNI), which is supported by all modern browsers. Apache automatically uses SNI when you have multiple SSL virtual hosts on the same IP. Just make sure each has its own certificate.
What is the default virtual host?
The first VirtualHost block in the configuration files is the default. Any request that doesn’t match a ServerName will be served by that block. It’s common to create a default site that shows a 404 or a placeholder page.
How do I remove a virtual host?
Delete the .conf file from /etc/httpd/conf.d/ (or comment out the block), run apachectl configtest, then reload Apache. You can also leave the directory and files in place; they just won’t be served.
Do I need to restart or reload Apache after changes?
Reload is enough for configuration changes. It doesn’t drop active connections. Use sudo systemctl reload httpd. Restart (systemctl restart httpd) is only needed for major changes like enabling new modules.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Business partnership due diligence checklist what to verify before you sign?
- Does the FCC Protect Internet Privacy?
- Disaster Robbers Codes? Meaning & Origin
- Repair a Disk with Mac Disk Utility
- Avoid Vendor Lock-In: Cloud Provider Strategy
- Secure GitHub Accounts & Repos Against Malicious Code