How to Configure Apache Virtual Hosts on Debian 12 Bookworm
Categories:
4 minute read
When managing multiple websites on a single Debian server, Apache Virtual Hosts provide an efficient way to host each site with its own configuration, domain, and document root. This feature allows one Apache server to serve many domains or applications independently, reducing infrastructure costs and simplifying administration.
In this guide, we’ll walk you through the process of configuring Apache Virtual Hosts on a Debian 12 Bookworm system. By the end, you’ll be able to host multiple websites on a single server, each with its own domain.
Prerequisites
Before diving into the configuration, make sure you have the following:
- A system running Debian 12 (Bookworm).
- A user with sudo privileges.
- Apache web server installed.
- At least two domain names (real or local) for testing multiple sites.
- DNS A records pointing to your server’s IP (for production environments).
For local testing, you can modify your system’s /etc/hosts
file instead.
Step 1: Install Apache Web Server
If Apache is not already installed, you can install it using APT:
sudo apt update
sudo apt install apache2
Once installed, verify that Apache is running:
sudo systemctl status apache2
You should see output indicating that the service is active (running).
To enable Apache to start on boot:
sudo systemctl enable apache2
Step 2: Understand Apache Virtual Hosts
Apache uses Virtual Hosts to direct incoming requests to the appropriate website content. Virtual Hosts can be defined based on:
- IP address (rarely used unless for specific configurations),
- Port number, or
- Domain name (most common and default method).
Apache configurations are stored in /etc/apache2/
. Site configurations typically reside in:
/etc/apache2/sites-available/
– Stores configuration files for each site./etc/apache2/sites-enabled/
– Stores symlinks to active site configurations.
The default configuration file is /etc/apache2/sites-available/000-default.conf
.
Step 3: Create Directory Structure for Each Site
Assume we want to host two domains:
example1.test
example2.test
Let’s create directories for each:
sudo mkdir -p /var/www/example1.test/public_html
sudo mkdir -p /var/www/example2.test/public_html
Set appropriate ownership (replace youruser
with your actual username):
sudo chown -R youruser:www-data /var/www/example1.test
sudo chown -R youruser:www-data /var/www/example2.test
Set the right permissions:
sudo chmod -R 755 /var/www
Step 4: Create Sample Web Pages
Let’s add simple HTML files to test the sites:
For example1.test
nano /var/www/example1.test/public_html/index.html
Paste:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example1</title>
</head>
<body>
<h1>Success! This is example1.test</h1>
</body>
</html>
For example2.test
nano /var/www/example2.test/public_html/index.html
Paste:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Example2</title>
</head>
<body>
<h1>Success! This is example2.test</h1>
</body>
</html>
Step 5: Create Apache Virtual Host Configuration Files
Let’s create separate configuration files for each site.
For example1.test
sudo nano /etc/apache2/sites-available/example1.test.conf
Add the following:
<VirtualHost *:80>
ServerAdmin webmaster@example1.test
ServerName example1.test
ServerAlias www.example1.test
DocumentRoot /var/www/example1.test/public_html
ErrorLog ${APACHE_LOG_DIR}/example1_error.log
CustomLog ${APACHE_LOG_DIR}/example1_access.log combined
</VirtualHost>
For example2.test
sudo nano /etc/apache2/sites-available/example2.test.conf
Add:
<VirtualHost *:80>
ServerAdmin webmaster@example2.test
ServerName example2.test
ServerAlias www.example2.test
DocumentRoot /var/www/example2.test/public_html
ErrorLog ${APACHE_LOG_DIR}/example2_error.log
CustomLog ${APACHE_LOG_DIR}/example2_access.log combined
</VirtualHost>
Step 6: Enable the Virtual Hosts
To activate the sites, use the a2ensite
command:
sudo a2ensite example1.test.conf
sudo a2ensite example2.test.conf
Then, reload Apache to apply the changes:
sudo systemctl reload apache2
Step 7: Update the Local Hosts File (for Local Testing)
If you’re testing locally and don’t have public DNS records for your domains, edit your local /etc/hosts
file:
sudo nano /etc/hosts
Add the following lines:
127.0.0.1 example1.test
127.0.0.1 example2.test
Save and exit.
Now you can open your browser and visit:
Each should display its respective web page.
Step 8: Disable the Default Site (Optional)
Apache enables the default virtual host (000-default.conf
) by default. You can disable it if not needed:
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
Step 9: Troubleshooting and Verification
Check Configuration Syntax
Before reloading Apache, it’s a good idea to validate the configuration syntax:
sudo apache2ctl configtest
You should see:
Syntax OK
If not, the output will point to the file and line number with the error.
Check Logs
Use logs to diagnose problems:
sudo tail -f /var/log/apache2/error.log
or view access logs for specific sites:
sudo tail -f /var/log/apache2/example1_access.log
Step 10: Enable HTTPS (Optional but Recommended)
While this guide uses HTTP for simplicity, it’s best to secure your sites with HTTPS.
Step 1: Install Certbot
sudo apt install certbot python3-certbot-apache
Step 2: Run Certbot
sudo certbot --apache
Certbot will ask for your domain, create the necessary configuration changes, and reload Apache automatically.
Conclusion
By following these steps, you’ve configured Apache to host multiple websites on a single Debian 12 Bookworm server using Virtual Hosts. This setup is ideal for shared environments, development, and production hosting.
With this foundation, you can now:
- Set up SSL certificates using Let’s Encrypt.
- Configure redirects and rewrites.
- Apply security headers and access controls.
- Deploy content management systems like WordPress per site.
Apache’s Virtual Hosts offer a scalable and modular approach to website hosting, and Debian 12 makes it easy to maintain a clean and efficient server environment.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.