Setup Digital Ocean - Part 4 Virtual Hosts

David Carr

2 min read - 10th Aug, 2019

Virtual hosts maps folders to domain names allowing multiple sites to be configured on a single server.


Setup directory structure

Each site will require a folder and a virtual host.

To set up the folder for a domain called

sudo mkdir -p /var/www/

This will create and a folder inside called public. public_html can be used instead of public.

Next set user permissions of the user to the new site

sudo chown -R $USER:$USER /var/www/

The first time the server is setup, permissions will need to be set for read access to the general web directory and all of the files and folders it contains 

sudo chmod -R 755 /var/www


Create the virtual host

First site should copy the 000-default.conf to the domain.extension.conf ie

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/

Then open the conf file in vi 

sudo vi /etc/apache2/sites-available/

change ServerAdmin to the desired email address:

ServerAdmin webmaster@localhost

Next, add ServerName and ServerAlias this should point to the domain and the www version of the domain.


Update the document root to point to public or public public_html

DocumentRoot /var/www/

The file should look like this

<VirtualHost *:80>
        DocumentRoot /var/www/

        <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <IfModule mod_dir.c>
            DirectoryIndex index.php index.cgi index.html index.xhtml index.htm

RewriteEngine on
RewriteCond %{SERVER_NAME} [OR]
RewriteCond %{SERVER_NAME}
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

To enable additional sites copy conf from an existing site ie from

sudo cp /etc/apache2/sites-available/ /etc/apache2/sites-available/

To enable the conf enable them using a2ensite

sudo a2ensite

To disable the default conf

sudo a2dissite 000-default.conf
Add a comment

Copyright © 2006 - 2024 DC Blog - All rights reserved.