Host multiple websites with secure HTTPS on a single server using Apache Virtual Hosts and Certbot.
Estimated Time: Approximately 60 - 90 minutes (for two domains)
Apache Virtual Hosts allow you to host multiple websites or domains on a single server with a single Apache installation. This is incredibly efficient, as you don't need a separate server for each website you want to run.
Apache determines which website to serve based on the domain name requested by the user's browser. This is called "name-based virtual hosting." Each virtual host configuration specifies the domain, its document root (where its files are stored), logging, and other specific settings.
Why Use Virtual Hosts?
This guide will cover setting up two example domains, `yourdomain1.com` and `yourdomain2.com`, configuring them as Apache Virtual Hosts, and then securing both with free SSL certificates from Let's Encrypt using Certbot.
60 - 90 minutes
(For configuring two domains and setting up SSL. More domains will take slightly longer.)
Intermediate
Assumes basic familiarity with Apache, DNS, and Linux terminal commands (e.g., from our LAMP guide and Certbot guide).
sudo
privileges on your Ubuntu server.Always start by ensuring your system is up-to-date.
sudo apt update && sudo apt upgrade -y
Confirm that Apache is running and your firewall is correctly configured to allow web traffic.
Check Apache status:
sudo systemctl status apache2
Check UFW status:
sudo ufw status verbose
Each website needs its own dedicated `DocumentRoot`. We'll create separate directories for `yourdomain1.com` and `yourdomain2.com`.
1. Create document root directories:
sudo mkdir -p /var/www/yourdomain1.com/public_html
sudo mkdir -p /var/www/yourdomain2.com/public_html
2. Set ownership and permissions:
sudo chown -R $USER:$USER /var/www/yourdomain1.com
sudo chown -R $USER:$USER /var/www/yourdomain2.com
sudo chmod -R 755 /var/www/yourdomain1.com
sudo chmod -R 755 /var/www/yourdomain2.com
3. Create simple `index.html` files for testing:
echo "<h1>Welcome to yourdomain1.com!</h1>" | sudo tee /var/www/yourdomain1.com/public_html/index.html
echo "<h1>Welcome to yourdomain2.com!</h1>" | sudo tee /var/www/yourdomain2.com/public_html/index.html
Apache stores virtual host configurations in `/etc/apache2/sites-available/`. Only files symlinked to `/etc/apache2/sites-enabled/` are active.
1. Create configuration file for `yourdomain1.com`:
sudo nano /etc/apache2/sites-available/yourdomain1.com.conf
Paste the following, replacing `yourdomain1.com` and `www.yourdomain1.com` with your actual domain:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain1.com
ServerName yourdomain1.com
ServerAlias www.yourdomain1.com
DocumentRoot /var/www/yourdomain1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/yourdomain1.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain1.com_access.log combined
<Directory /var/www/yourdomain1.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Save (`Ctrl+O`, `Enter`) and exit (`Ctrl+X`).
2. Create configuration file for `yourdomain2.com`:
sudo nano /etc/apache2/sites-available/yourdomain2.com.conf
Paste the following, replacing `yourdomain2.com` and `www.yourdomain2.com` with your actual domain:
<VirtualHost *:80>
ServerAdmin webmaster@yourdomain2.com
ServerName yourdomain2.com
ServerAlias www.yourdomain2.com
DocumentRoot /var/www/yourdomain2.com/public_html
ErrorLog ${APACHE_LOG_DIR}/yourdomain2.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain2.com_access.log combined
<Directory /var/www/yourdomain2.com/public_html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Save (`Ctrl+O`, `Enter`) and exit (`Ctrl+X`).
Use `a2ensite` to enable your new virtual hosts and `a2dissite` to disable the default Apache configuration, if it conflicts.
1. Enable your new sites:
sudo a2ensite yourdomain1.com.conf
sudo a2ensite yourdomain2.com.conf
2. Disable the default site (if applicable):
sudo a2dissite 000-default.conf
3. Test Apache configuration for syntax errors:
sudo apache2ctl configtest
4. Restart Apache to apply changes:
sudo systemctl restart apache2
Open your web browser and navigate to both domain names. This step is crucial to ensure Apache is serving the correct content before we add SSL.
If you haven't already, install Certbot with the Apache plugin. This tool will automate the SSL certificate acquisition and Apache configuration.
1. Install Certbot (if needed):
sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
2. Enable Apache SSL module (if not already):
sudo a2enmod ssl
sudo systemctl restart apache2
Certbot's Apache plugin is smart enough to detect your virtual hosts and offer to secure them. It will automatically create new SSL-enabled configuration files and set up HTTP to HTTPS redirection.
sudo certbot --apache
Certbot will guide you through a series of prompts:
After Certbot completes, it's time to verify that both your sites are now serving over HTTPS.
1. Browser Check: Open your web browser and navigate to `https://yourdomain1.com` (and `https://www.yourdomain1.com`). Do the same for `https://yourdomain2.com`.
2. Online SSL Checker (Optional): Use an online tool like SSL Labs SSL Server Test. Enter each of your domain names to get a detailed report on your SSL configuration and grade (aim for A or A+).
Let's Encrypt certificates are valid for 90 days. Certbot automatically sets up a systemd timer or cron job to renew them well before expiration, so you typically don't need to do anything manually.
1. Check the renewal timer status:
sudo systemctl status snap.certbot.renew.service
2. Test the renewal process (dry run): This command simulates a renewal without actually changing your certificates.
sudo certbot renew --dry-run
Ensure both your websites are fully operational and secured:
Congratulations! You have successfully configured Apache Virtual Hosts to serve multiple websites from a single Ubuntu server and secured each domain with free, automatically renewing SSL certificates from Let's Encrypt using Certbot. This is a powerful and essential setup for modern web hosting.
Consider these advanced steps and best practices to further enhance your multi-site server:
Need Expert Web Hosting Solutions or Multi-Site Management? Contact Us!