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
- Create the directory structure for each site. For example, for
example.comandexample.org:
sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/example.org/html - Assign ownership to your user (replace
youruserwith your actual username):
sudo chown -R $USER:$USER /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.org/html - Set permissions to ensure Apache can read the files:
sudo chmod -R 755 /var/www - 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 - 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 - Edit the configuration file for the first site. Open it with:
sudo nano /etc/apache2/sites-available/example.com.conf
Change theServerAdminto your email, add aServerName(e.g.,example.com), and setDocumentRootto/var/www/example.com/html. Optionally addServerAlias www.example.com. - Edit the second site’s configuration similarly, with
ServerName example.organdDocumentRoot /var/www/example.org/html. - Enable the new virtual hosts using
a2ensite:
sudo a2ensite example.com.conf
sudo a2ensite example.org.conf - Disable the default site (optional but recommended) to avoid conflicts:
sudo a2dissite 000-default.conf - Test the Apache configuration for syntax errors:
sudo apache2ctl configtest
You should seeSyntax OK. - Reload Apache to apply the changes:
sudo systemctl reload apache2 - Verify the sites by visiting
http://example.comandhttp://example.orgin 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.comto 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 insites-enabled/.
Tips
- Use descriptive file names for virtual host configs, like
example.com.conf, to keep things organized. - Add an
ErrorLogandCustomLogper 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
a2ensiteanda2dissiteto 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
ServerNameand 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 theDocumentRootpath is correct. Also ensure the directory exists. - 404 Not Found – The
DocumentRootmay be pointing to an empty directory. Verify the index file exists and is readable. - Syntax errors when reloading – Run
sudo apache2ctl configtestto see the error. Common issues are missingServerNameor 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
- Microsoft Surface Duo: The Foldable Phone Story
- Samsung SM-T813 Hard Reset: Step-by-Step Guide
- Mafia Clash Codes: Real Meanings & Usage
- Remove Programs from Mac: Full Uninstall Guide
- Project Sentinel Morning Digest: Data Stack Failure Alerts
- Best KVM VPS Hosting: Top Picks & Guide