keepcalculator

06 Numbers and Powers

Apache Virtual Hosts on CentOS: Setup Guide

Card No. 06·52313 · Updated · Standard formula, shown below

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) or sudo 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.test and edit your local hosts file.
  • A text editor like vim, nano, or vi.

Step-by-Step

  1. Create the directory structure for each site. 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
  2. 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
  3. 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
  4. 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 .conf extension:
    sudo vim /etc/httpd/conf.d/example.com.conf
  5. 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.com with your actual domain. The ServerName is the primary domain, ServerAlias is for additional names like www. The DocumentRoot points to the folder you created. Logs are stored in /var/log/httpd/.

  6. Create a similar file for each additional site. For test.com, create /etc/httpd/conf.d/test.com.conf with the same structure but different domain and paths.
  7. Check the configuration for syntax errors before restarting:
    sudo apachectl configtest

    You should see Syntax OK. If there are errors, fix them before proceeding.

  8. Restart Apache to apply the changes:
    sudo systemctl restart httpd

    Or, if you prefer a graceful reload that doesn’t drop connections:

    sudo systemctl reload httpd
  9. 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/hosts on Linux, C:\Windows\System32\drivers\etc\hosts on Windows) to point the domain to your server’s IP, then visit http://example.com and http://test.com. You should see the respective index pages.
  10. 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 with certbot to 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 main httpd.conf and 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 -S to list all virtual hosts and verify that they are configured correctly.
  • Test with curl -H "Host: example.com" http://localhost to 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 DocumentRoot exists 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 ServerName matches the domain you’re accessing, and that the config file is in the correct directory.
  • If you get a 404 Not Found – The DocumentRoot may 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 configtest to 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 duplicate ServerNames, 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

The whole Numbers and Powers drawer →