This guide walks you through setting up Apache virtual hosts on CentOS, allowing you to host multiple websites on a single server with separate document roots, configurations, and logs. You’ll learn how to create the directory structure, write configuration files, enable the sites, and test everything. This is essential for anyone managing multiple domains or subdomains on one CentOS machine.
What You’ll Need
- A CentOS server (7, 8, or 9) with Apache (httpd) already installed. If not, install it with
sudo yum install httpd(CentOS 7) orsudo dnf install httpd(CentOS 8/9). - Root or sudo access to the server.
- At least one domain name pointing to your server’s public IP address. For local testing, you can use dummy names like
example.testand edit your local hosts file. - A text editor like
vim,nano, orvi.
Step-by-Step
- Create the directory structure for each site. For example, for
example.comandtest.com:sudo mkdir -p /var/www/example.com/public_html sudo mkdir -p /var/www/test.com/public_html - Set ownership and permissions so Apache can read the files. Usually, you set the owner to the Apache user (often
apache) or your own user if you’ll be uploading files:sudo chown -R $USER:$USER /var/www/example.com/public_html sudo chown -R $USER:$USER /var/www/test.com/public_html sudo chmod -R 755 /var/www - Create sample index.html files for each site so you can test later:
echo "<h1>Welcome to Example.com!</h1>" | sudo tee /var/www/example.com/public_html/index.html echo "<h1>Welcome to Test.com!</h1>" | sudo tee /var/www/test.com/public_html/index.html - Create the virtual host configuration files. On CentOS, the default Apache config includes files from
/etc/httpd/conf.d/. Create a file for each site with a.confextension:sudo vim /etc/httpd/conf.d/example.com.conf - Write the basic virtual host block. A minimal configuration looks like this:
<VirtualHost *:80> ServerAdmin admin@example.com 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.comwith your actual domain. TheServerNameis the primary domain,ServerAliasis for additional names likewww. TheDocumentRootpoints to the folder you created. Logs are stored in/var/log/httpd/. - Create a similar file for each additional site. For
test.com, create/etc/httpd/conf.d/test.com.confwith the same structure but different domain and paths. - Check the configuration for syntax errors before restarting:
sudo apachectl configtestYou should see
Syntax OK. If there are errors, fix them before proceeding. - Restart Apache to apply the changes:
sudo systemctl restart httpdOr, if you prefer a graceful reload that doesn’t drop connections:
sudo systemctl reload httpd - Test the virtual hosts. If you’re using real domains, you can simply visit them in a browser. For local testing, edit your local hosts file (
/etc/hostson Linux,C:\Windows\System32\drivers\etc\hostson Windows) to point the domain to your server’s IP, then visithttp://example.comandhttp://test.com. You should see the respective index pages. - Set up SSL (optional but recommended). For HTTPS, you’ll need to configure a
<VirtualHost *:443>block with the SSL certificate paths. You can use Let’s Encrypt withcertbotto get free certificates. After obtaining certs, add a configuration like:<VirtualHost *:443> ServerName example.com DocumentRoot /var/www/example.com/public_html SSLEngine on SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem </VirtualHost>Then reload Apache.
Common Mistakes
- Forgetting to set ServerName – Apache will use the first virtual host as the default for any unmatched requests, which can lead to the wrong site being served.
- Incorrect DocumentRoot path – If the path doesn’t exist or is wrong, you’ll get a 403 or 404 error.
- Not enabling the virtual host – On CentOS, if you put your config in
/etc/httpd/conf.d/, it’s automatically included. But if you put it in the mainhttpd.confand forget to include it, it won’t work. - Not restarting Apache – Changes won’t take effect until you restart or reload.
- File permissions – If the web directory is not readable by the Apache user, you’ll get a 403 Forbidden error.
Tips
- Use a naming convention for your config files, like
domain.com.conf, to keep things organized. - Keep logs separate for each virtual host – it makes troubleshooting much easier.
- Use
apachectl -Sto list all virtual hosts and verify that they are configured correctly. - Test with
curl -H "Host: example.com" http://localhostto simulate a request without changing your hosts file. - Consider using a configuration management tool like Ansible or Puppet if you manage many servers.
Troubleshooting
- If you get a 403 Forbidden – Check that the directory permissions are 755 and that the files are readable. Also ensure that the
DocumentRootexists and has an index file. - If you see the default Apache page instead of your site – This usually means the virtual host isn’t being picked up. Check that the
ServerNamematches the domain you’re accessing, and that the config file is in the correct directory. - If you get a 404 Not Found – The
DocumentRootmay be wrong, or the file doesn’t exist. Verify the path and that the file is actually there. - If Apache fails to start – Run
apachectl configtestto see syntax errors. Also check the error log at/var/log/httpd/error_log. - If only one site works – Ensure that each virtual host has a unique
ServerName. If you have duplicateServerNames, Apache will only use the first one.
Frequently Asked Questions
Can I host multiple domains on the same IP address?
Yes, that’s the whole point of name-based virtual hosting. Apache uses the Host header in the HTTP request to determine which virtual host to serve.
Do I need to edit the main httpd.conf file?
No, you can place your virtual host configurations in separate files under /etc/httpd/conf.d/. This is cleaner and easier to manage.
How do I set up virtual hosts for subdomains?
Simply create a new virtual host block with ServerName set to the subdomain (e.g., blog.example.com) and point the DocumentRoot to a different directory.
What is the difference between a restart and a reload?
A restart stops and starts Apache completely, which can interrupt active connections. A reload gracefully reloads the configuration without dropping existing connections. Use reload when possible.
Why do I get a 403 error even though I set permissions?
Check that the directory has execute permission for the Apache user (usually apache). Also, ensure that the DocumentRoot is not inside your home directory if SELinux is enforcing – you may need to set the correct SELinux context with chcon -R -t httpd_sys_content_t /var/www.
Can I use IP-based virtual hosts instead?
Yes, but you’ll need multiple IP addresses on your server. Name-based virtual hosting is more common and doesn’t require extra IPs.
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
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It