How to Create Virtual Hosts on Debian & Ubuntu
This guide will walk you through creating Apache virtual hosts on Debian and Ubuntu Linux. Virtual hosts allow you to host multiple websites on a single server.
Prerequisites
- Ubuntu 20.04+ / Debian 10+
- Apache installed
- Root or sudo access
- Domain name pointing to your server
Step 1: Install Apache
If Apache is not already installed:
sudo apt update
sudo apt install apache2
Note: Apache configuration files are located in
/etc/apache2Step 2: Create Document Root Directory
Create the directory for your website files:
sudo mkdir -p /var/www/html/example.com/public_html
sudo mkdir -p /var/www/html/mywebsite.com/public_html
Set Permissions
Allow Apache to read the files:
sudo chown -R www-data:www-data /var/www/html/example.com
sudo chown -R www-data:www-data /var/www/html/mywebsite.com
sudo chmod -R 755 /var/www/html
Step 3: Create Virtual Host Configuration
Create the configuration file for your site:
sudo nano /etc/apache2/sites-available/example.com.conf
Add this configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/example.com/public_html
<Directory /var/www/html/example.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Tip: Replace
example.com with your actual domain name. Repeat for each additional site.Step 4: Enable the Virtual Host
Enable your new site and disable the default:
sudo a2ensite example.com
sudo a2dissite 000-default
Step 5: Test Configuration & Restart Apache
Test the configuration for syntax errors:
sudo apache2ctl configtest
If no errors, restart Apache:
sudo systemctl restart apache2
Success! Your virtual host is now active.
HTTPS (SSL/TLS) Configuration
To enable HTTPS, install Let’s Encrypt SSL certificate:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com
Follow the on-screen instructions to complete SSL setup.
Useful Commands
- List enabled sites:
apache2ctl -S - Test config:
sudo apache2ctl configtest - Restart:
sudo systemctl restart apache2 - Reload:
sudo systemctl reload apache2 - Check status:
sudo systemctl status apache2
Troubleshooting
Site Not Loading
- Check DNS is pointing to your server IP
- Verify the site is enabled:
ls /etc/apache2/sites-enabled/ - Check Apache error logs:
tail -f /var/log/apache2/example.com-error.log
Permission Denied Errors
Ensure document root has correct ownership:
sudo chown -R www-data:www-data /var/www/html/example.com
Port Already in Use
Check what’s using port 80:
sudo netstat -tulpn | grep :80
Conclusion
You have successfully created Apache virtual hosts on your Debian/Ubuntu server. You can now:
- Host multiple domains on one server
- Configure separate document roots
- Enable SSL/HTTPS for secure connections
- Access individual error and access logs
Pro Tip: For local development, add entries to
/etc/hosts to test virtual hosts before updating DNS.