Adding Multiple Domains to Apache

1 — Create the Directory Structure

sudo mkdir -p /var/www/example.com

2 — Grant Permissions

sudo chown -R www-data:www-data  /var/www/example.com

3 — Create New Virtual Host Files

sudo nano /etc/apache2/sites-available/example.com.conf

The file should contain the following

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<Directory /var/www/example.com/>
    AllowOverride All
</Directory>

Enable Rewrite

sudo a2enmod rewrite

4 — Enable the New Virtual Host Files

sudo a2ensite example.com.conf

Disable the Default

sudo a2dissite 000-default.conf

5 – Restart Apache

sudo systemctl restart apache2

6. — Set Up Local Hosts File (Optional)

Open up the host file on the system

sudo nano /etc/hosts

In the file, fill up accordingly

127.0.0.1   localhost
127.0.1.1   guest-desktop
your_server_IP example.com

7. Setup SSL – Lets Encrypt

This assumes Certbot has aleady been setup. If not, please go to “Setup of Certbot”

sudo certbot --apache

Verify Certbot Renewal

sudo systemctl status certbot.timer
sudo certbot renew --dry-run
Scroll to Top