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/yoursite.com/public_html

Assign ownership of the directory:

sudo chown -R $USER:$USER /var/www/yoursite.com/public_html

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/yoursite.com.conf

Make updates for the new directory and domain name:

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

Edit thee following lines in :

ServerName yoursite.com
ServerAlias www.yoursite.com
DocumentRoot /var/www/yoursite.com/public_html

Enable the file with the a2ensite tool:

sudo a2ensite yoursite.com.conf

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/yoursite.com-le-ssl.conf
Beacon of HOV