How to Set Up Virtual Hosts

If you are setting up your Droplet for the first time, follow “How To Install the Apache Web Server on Ubuntu 20.04” to install Apache. Step 5 is where you set up virtual hosts.

Create the directory for your_domain:
sudo mkdir -p /var/www/

Assign ownership of the directory:
sudo chown -R $USER:$USER /var/www/

Set up permissions:
sudo chmod -R 755 /var/www

Copy an existing virtual host file:
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

Make updates for the new directory and domain name:
sudo nano /etc/apache2/sites-available/

Edit thee following lines in :
DocumentRoot /var/www/

Enable the file with the a2ensite tool:
sudo a2ensite

Disable the default site defined in 000-default.conf:
sudo a2dissite 000-default.conf

Restart Apache
sudo systemctl restart apache2

Let’s Encrypt

To set up SSL certificate using Let’s Encrypt, follow “How To Secure Apache with Let’s Encrypt on Ubuntu 20.04.” Let’s Encrypt will generate the follow file, you can take a look:
sudo nano /etc/apache2/sites-available/

20 Years of Blogging