Static Sites

A static site serves plain, unchanging files from a folder on the droplet; front end builds or plain html.

Content Directory

As the communal ‘git’ user,

 mkdir ~/my-site
vi ~/my-site/index.html
<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain server block is working!</h1>
    </body>
</html>

If following /var/www convention (simplifies file permissions management)

sudo ln -s /home/git/my-site /var/www/my-site.root-domain.ca
sudo chmod 755 /var/www/my-site.root-domain.ca

Nginx1

sudo vi /etc/nginx/sites-available/my-site.root-domain.ca
server {
  root /var/www/my-site.root-domain.ca;
  error_page 404 /404.html;
  index index.html;
  
  server_name my-site.root-domain.ca www.my-site.root-domain.ca;
  
  if ($host = www.my-site.root-domain.ca) {
    return 301 https://my-site.root-domain.ca$request_uri
  } #redirect www.my-site.root-domain.ca to my-site.root-domain.ca secured

  location / {
    try_files $uri $uri.html $uri/index.html =404
  }
  
  location /404.html {
    internal;
  }
}
sudo ln -s /etc/nginx/sites-available/<sitename> /etc/nginx/sites-enabled/<sitename>
sudo nginx -t
sudo systemctl restart nginx

HTTPs configuration

In order to serve your site to a new domain (or subdomain) securely over the internet (https), you’ll need to set up ssl certificates. Before that, you’ll need to add DNS record*(s)* pointing to the droplet through the relevant registar.

sudo certbot --nginx -d <site>.bci4kids.ca -d www.<site>.bci4kids.ca

certbot will automatically renew certificates for the provided domains when they are close to expiry, notifying a provided email if this renewal fails. To change the reminder email run:

sudo certbot update_account --email <email address>
sudo nginx -t
sudo systemctl restart nginx

CI/CD

Repository setup will differ depending on your desired build process. If you are pushing built files, you could simply initialize the content directory as a repository to push new builds to from a script.

If building on the droplet itself,

mkdir ~/my-site.git
cd my-site.git
git init --bare
vi hooks/post-receive

Post-Receive Hook Example2

Hugo is used as an example build tool. See Wiki for another example

#!/bin/bash

WORK_TREE=$HOME/my-site-worktree/
GIT_DIR=$HOME/my-site.git
DESTINATION=$HOME/my-site

echo "Changes pushed, deploying..."

if [[ ! -d $WORK_TREE ]]; then
    mkdir $WORK_TREE || exit 1
fi

echo "Populating source files..."
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f || exit 1

cd $WORK_TREE
echo "Rebuilding..."
hugo -d $DESTINATION --cleanDestinationDir || exit 1

cd ..
echo "Cleaning build files..."
rm -rf $WORK_TREE || exit 1

References


  1. Setting up NginX Server Blocks ↩︎

  2. Deploying Code with a Git Hook ↩︎