Loading...

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 test.com

sudo mkdir -p /var/www/test.com/public

This will create test.com 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/test.com/public

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 test.com.conf

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

Then open the conf file in vi 

sudo vi /etc/apache2/sites-available/test.com.conf

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.

ServerName example.com
ServerAlias www.example.com

Update the document root to point to public or public public_html

DocumentRoot /var/www/test.com/public

The file should look like this

<VirtualHost *:80>
        ServerAdmin admin@test.com
        ServerName test.com
        ServerAlias www.test.com
        DocumentRoot /var/www/test.com/public

        <Directory /var/www/test.com/public/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

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

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

RewriteEngine on
RewriteCond %{SERVER_NAME} =test.com [OR]
RewriteCond %{SERVER_NAME} =www.test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

To enable additional sites copy conf from an existing site ie from test.com

sudo cp /etc/apache2/sites-available/test.com.conf /etc/apache2/sites-available/newsite.com.conf

To enable the conf enable them using a2ensite

sudo a2ensite test.com.conf

To disable the default conf

sudo a2dissite 000-default.conf
0 comments
Add a comment