keepcalculator

06 Numbers and Powers

Apache Virtual Hosts on Debian VPS: Setup Guide

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

This guide walks you through setting up Apache virtual hosts on a Debian VPS, allowing you to host multiple websites from a single server. You’ll learn how to create directory structures, configure virtual host files, enable them, and test your setup. By the end, you’ll be able to serve different domains or subdomains with their own document roots and configurations.

What You’ll Need

  • A Debian VPS (any recent stable version, e.g., Debian 11 or 12) with root or sudo access.
  • Apache installed. If not, install it with: sudo apt update && sudo apt install apache2
  • Domain names (or subdomains) pointing to your VPS’s public IP address via DNS A records.
  • Basic familiarity with the command line and file editing (nano or vim).

Step-by-Step

  1. Create the directory structure for each site. For example, for example.com and example.org:
    sudo mkdir -p /var/www/example.com/html
    sudo mkdir -p /var/www/example.org/html
  2. Assign ownership to your user (replace youruser with your actual username):
    sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chown -R $USER:$USER /var/www/example.org/html
  3. Set permissions to ensure Apache can read the files:
    sudo chmod -R 755 /var/www
  4. Create sample index pages for each site so you can verify they work. For example:
    echo '<h1>Welcome to Example.com!</h1>' | tee /var/www/example.com/html/index.html
    echo '<h1>Welcome to Example.org!</h1>' | tee /var/www/example.org/html/index.html
  5. Copy the default configuration file as a template for each site:
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
    sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.org.conf
  6. Edit the configuration file for the first site. Open it with:
    sudo nano /etc/apache2/sites-available/example.com.conf
    Change the ServerAdmin to your email, add a ServerName (e.g., example.com), and set DocumentRoot to /var/www/example.com/html. Optionally add ServerAlias www.example.com.
  7. Edit the second site’s configuration similarly, with ServerName example.org and DocumentRoot /var/www/example.org/html.
  8. Enable the new virtual hosts using a2ensite:
    sudo a2ensite example.com.conf
    sudo a2ensite example.org.conf
  9. Disable the default site (optional but recommended) to avoid conflicts:
    sudo a2dissite 000-default.conf
  10. Test the Apache configuration for syntax errors:
    sudo apache2ctl configtest
    You should see Syntax OK.
  11. Reload Apache to apply the changes:
    sudo systemctl reload apache2
  12. Verify the sites by visiting http://example.com and http://example.org in a browser. You should see the respective welcome pages.

Common Mistakes

  • Forgetting to add ServerName – Without it, Apache may use the first virtual host for all requests, causing the wrong site to appear.
  • Incorrect file permissions – If Apache can’t read the document root or files, you’ll get a 403 Forbidden error. Ensure the directories are readable (755) and files are 644.
  • Not disabling the default site – The default site often catches requests that don’t match any virtual host, so your custom sites may not work as expected.
  • DNS not propagated – If you just changed DNS records, it may take time. Check with dig example.com to see if it resolves to your VPS IP.
  • Editing the wrong configuration file – Make sure you edit files in /etc/apache2/sites-available/, not the enabled symlinks in sites-enabled/.

Tips

  • Use descriptive file names for virtual host configs, like example.com.conf, to keep things organized.
  • Add an ErrorLog and CustomLog per site to make troubleshooting easier. For example:
    ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
  • Test with curl before relying on a browser: curl -H "Host: example.com" http://localhost – this helps verify the virtual host is responding correctly.
  • Use a2ensite and a2dissite to manage symlinks – it’s cleaner than manually creating them.
  • Consider HTTPS – Once the virtual hosts work, set up SSL certificates with Let’s Encrypt (e.g., using certbot) to secure your sites.

Troubleshooting

  • Site shows the default Apache page – This usually means the virtual host isn’t being used. Check that the config file has ServerName and that it’s enabled (sudo a2ensite example.com.conf), then reload Apache.
  • 403 Forbidden – Check directory permissions (sudo chmod -R 755 /var/www) and that the DocumentRoot path is correct. Also ensure the directory exists.
  • 404 Not Found – The DocumentRoot may be pointing to an empty directory. Verify the index file exists and is readable.
  • Syntax errors when reloading – Run sudo apache2ctl configtest to see the error. Common issues are missing ServerName or a typo in the config.
  • Site not resolving – Check DNS with dig example.com. If it doesn’t point to your VPS IP, wait for propagation or fix your DNS records.
  • Port 80 blocked – Ensure your firewall (if any) allows HTTP traffic: sudo ufw allow 'Apache Full' (if using UFW).

Frequently Asked Questions

Can I use IP-based virtual hosts instead of name-based?

Yes, but name-based virtual hosts (using ServerName) are the default and sufficient for most setups. IP-based hosts require separate IP addresses for each site.

How do I add a subdomain like blog.example.com?

Create a new DNS A record for blog pointing to your VPS IP, then create a new virtual host with ServerName blog.example.com and its own document root.

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

sites-available holds all configuration files, while sites-enabled contains symlinks to the ones Apache actually uses. a2ensite creates the symlink, a2dissite removes it.

Do I need to restart Apache after changes?

No, a reload (sudo systemctl reload apache2) is enough for configuration changes. Restart is only needed for major changes like enabling modules.

Can I use virtual hosts to run different PHP versions per site?

Yes, but that requires additional configuration with PHP-FPM and per-site pool settings. It’s more advanced but possible.

How do I redirect all traffic to HTTPS?

After setting up SSL, you can add a redirect in the virtual host config or use a .htaccess file. The simplest is to use Redirect permanent / https://example.com/ in a port 80 virtual host.

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 →