Hosting websites with hugo, nginx and https

Building the site

This site is made using hugo, it is a fast framework to quickly make a website. Pages are written in markdown and then converted to your design by compiling the website. When the website is built locally it then has to be uploaded to a server where it will be available to rest of the world.

Install Hugo

# macOS
brew install hugo
# Debian/Ubuntu
sudo apt-get install hugo

Upload to server

When you installed a theme, created your own or made changes to your website you have to compile the website and upload it to your server. If you built the site locally this one-liner builds it and uploads the site to your server.

hugo & rsync -avz --delete public/ $USER@$SERVER:/home/website/html

Server side

On the server side I used docker to easily setup a nginx web server and to not have do much configuration work myself. On your server install docker and docker-compose. (see docker.com)

Certificates

If you want your site to have https you need a ssl certificate. The one I used is bought from transip, where I also got my domain name. You can also use let’s-encrypt to generate a free certificate. (follow instructions there to generate your certificate)

Make a folder on your server to store your certificates. I used

/etc/nginx/certs/

Put your certificate.crt certificate.key cabundle.crt in the folder and change the owner rights to 0600 with chmod. To let the nginx-proxy use the certificates correctly change the names from certificate to your $domainname e.g. melvinstans.nl.crt

Nginx and nginx-proxy

Now the only thing left to do is starting up the docker containers to run our website. To make it easier for you you can use this docker-compose.yml to generate the containers.

version: "3"
services:
  nginx-proxy:
    container_name: nginx-proxy
    image: jwilder/nginx-proxy
    ports:
      - 443:443/tcp
      - 80:80/tcp
    volumes:
      - /etc/nginx/certs/:/etc/nginx/certs
      - /var/run/docker.sock:/tmp/docker.sock:ro
  website:
    container_name: website
    environment:
      - VIRTUAL_HOST=melvinstans.nl
    expose:
      - 8123/tcp
    image: nginx
    volumes:
      - /home/website/html:/usr/share/nginx/html

Keep in mind to set the correct volumes paths and

Start both with

docker-compose up

After that they can be accessed like normal containers. User docker ps to get info about running containers

More containers to nginx-proxy

If you want to start another website or container that uses the nginx-proxy don’t forget to set the VIRTUAL_HOST environment variable and the INTERNAL_PORT

docker run -e VIRTUAL_HOST=${DOMAINNAME} --expose ${INTERNAL_PORT} ...

See nginx-proxy for more documentation.


393 Words

2019-03-26 19:54 +0000