ThisCalculator

Everyday

Apache Virtual Hosts on Debian: Setup Guide

Updated · Standard formula, shown below

This guide walks you through setting up Apache virtual hosts on Debian, allowing you to host multiple websites on a single server. You’ll create separate configuration files for each domain, set up document roots, and enable the sites—all without disrupting existing services. You’ll need a Debian system with Apache installed and sudo access.

What You’ll Need

  • A Debian server (10, 11, or 12) with Apache2 installed (install with sudo apt install apache2 if needed).
  • Root or sudo access to edit files in /etc/apache2/.
  • Domain names (or test domains) pointing to your server’s IP address, or you can edit your local hosts file for testing.
  • Basic familiarity with the command line.

Step-by-Step

  1. Create the directory structure – For each site, create a directory to hold its files. For example, for example.com and test.com:
    sudo mkdir -p /var/www/example.com/html
    sudo mkdir -p /var/www/test.com/html
    Set ownership to your user (replace $USER):
    sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chown -R $USER:$USER /var/www/test.com/html
  2. Add test content – Create an index.html in each site’s html folder with a simple line like <h1>Welcome to example.com!</h1> so you can verify the virtual host is working.
  3. Create a virtual host configuration file – Apache on Debian stores site configs in /etc/apache2/sites-available/. Create a new file for the first site:
    sudo nano /etc/apache2/sites-available/example.com.conf
  4. Write the configuration – Inside the file, paste the following block (adjust paths and domain):
    <VirtualHost *:80>
        ServerAdmin admin@example.com
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Save and exit (Ctrl+X, then Y, then Enter in nano).

  5. Repeat for additional sites – Create another config file for test.com with the same structure, changing the ServerName, ServerAlias, and DocumentRoot accordingly.
  6. Enable the virtual hosts – Use the a2ensite command to enable each site:
    sudo a2ensite example.com.conf
    sudo a2ensite test.com.conf
  7. Disable the default site (optional but recommended) – If you don’t want the default site to interfere, disable it:
    sudo a2dissite 000-default.conf
  8. Test the configuration – Before restarting, check for syntax errors:
    sudo apache2ctl configtest
    You should see Syntax OK.
  9. Reload Apache – Apply the changes:
    sudo systemctl reload apache2
  10. Verify the sites work – In a browser, visit http://example.com and http://test.com. If you used fake domains, edit your local /etc/hosts file (on your local machine) to point them to the server’s IP. For example: 192.168.1.100 example.com.

Common Mistakes

  • Forgetting to enable the site – Creating a config file in sites-available does nothing until you run a2ensite.
  • Wrong file permissions – If Apache can’t read the document root, you’ll get a 403 error. Ensure the directories are readable (usually 755) and files are 644.
  • Missing ServerName – If you omit ServerName, Apache may use the first virtual host as a catch-all, causing unexpected behavior.
  • Using the same DocumentRoot for two sites – Each virtual host should have its own distinct root directory.
  • Not reloading Apache – Changes won’t take effect until you reload or restart the service.

Tips

  • Use descriptive filenames for your config files (e.g., example.com.conf) to keep things organized.
  • For HTTPS, you’ll need to create a virtual host on port 443 and configure SSL certificates (e.g., with Let’s Encrypt).
  • Use ServerAlias to include subdomains like www so they all point to the same site.
  • If you run multiple sites, consider using a wildcard virtual host (<VirtualHost *:80>) with ServerName and ServerAlias for each domain, but it’s cleaner to have separate files.
  • Test with curl -H "Host: example.com" http://your-server-ip to check the response without changing DNS.

Troubleshooting

If your virtual hosts aren’t working as expected, try these steps:

  • Check Apache status – Run sudo systemctl status apache2 to see if the service is running. If it failed, check the error log at /var/log/apache2/error.log.
  • Verify the config syntax – Run sudo apache2ctl configtest again. If there’s an error, it will tell you the file and line number.
  • Ensure the site is enabled – List enabled sites with ls /etc/apache2/sites-enabled/. If your config isn’t there, run sudo a2ensite yoursite.conf again.
  • Check DNS or hosts file – If you’re using real domains, confirm they resolve to your server’s IP. For testing, verify your /etc/hosts entry is correct.
  • Look at Apache logs – The error and access logs are your best friend. Tail them with sudo tail -f /var/log/apache2/error.log while making a request to see what’s happening.
  • Check file permissions – Ensure the Apache user (www-data) has read access to your document root. Run sudo chmod -R 755 /var/www/ if needed.

Frequently Asked Questions

Do I need a domain name to use virtual hosts?

No, you can use IP-based virtual hosts, but name-based virtual hosts (as shown here) require domain names. For testing, you can edit your local hosts file.

Can I host both HTTP and HTTPS on the same virtual host?

No, you need separate virtual host blocks for port 80 and port 443. You can use the same DocumentRoot for both.

How do I set up subdomains like blog.example.com?

Create a new virtual host with ServerName blog.example.com and point its DocumentRoot to a different directory.

What is the difference between sites-available and sites-enabled?

sites-available holds all your virtual host config files, while sites-enabled contains symlinks to the ones you’ve activated with a2ensite.

Why do I get a 403 Forbidden error?

This usually means Apache can’t access the document root due to permissions or a missing index file. Check that the directory is readable and contains an index.html or index.php.

Can I use virtual hosts with Apache on a different Linux distribution?

Yes, the concept is the same, but the file paths and commands may differ. This guide is specific to Debian and its derivatives like Ubuntu.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.