Hosting my Hugo blog on my own VPS
This blog is built using Hugo. Hugo is a static site generator written in Go. It is fast, simple and flexible.
Hugo is a static site generator, so as output it produces static files. Usually I use server-less platforms for hosting such projects, because often it is cheaper than setting up my own infrastructure. However, I wanted more control over my blog to make my first steps in DevOps.
Usually it is enough to run locally
1hugo --minify
or
1hugo --minify --gc
And just manually copy the content of public folder to the server. I wanted to automate this process, so I decided to use CI/CD pipeline.
Here I can build files and simple scp or rsync them with the server, but this not the best way. Server-less platforms do this with Docker, I’m going to use Docker.
Docker is a tool that allows to package an application with all of its dependencies into a standardized unit for software development. It is widely used in the industry.
To build a docker image, I need to create a Dockerfile. It is a text document that contains all the commands to assemble an image. I will use https://hub.docker.com/r/klakegg/hugo/ as a base image. It is an official Hugo image, so I don’t need to install Hugo manually.
I use the tag 0.71.1-ext-alpine. 0.71.1 is the Hugo version, and ext means Hugo extended edition, which is needed for themes with SCSS.
The site is served by nginx. My Dockerfile has two parts: the first one builds the site with Hugo, the second one copies the public folder into the nginx image.
1FROM klakegg/hugo:0.71.1-ext-alpine AS build
2WORKDIR /src
3COPY . .
4RUN hugo --minify
5
6FROM nginx:1.18-alpine
7COPY --from=build /src/public /usr/share/nginx/html
The final image contains only nginx and my static files, without Hugo.
Build the image and check it locally:
1docker build -t username/blog:latest .
2docker run --rm -p 8080:80 username/blog:latest
The site is available at http://localhost:8080.
Then I push the image to Docker Hub:
1docker login
2docker push username/blog:latest
Docker is already installed on my server. To update the blog, I pull the new image and restart the container:
1docker pull username/blog:latest
2docker stop blog
3docker rm blog
4docker run -d --name blog --restart always -p 80:80 username/blog:latest
The first time there is no container yet, so docker stop and docker rm can be skipped.
That’s it. Now I don’t copy many files to the server manually, just one :D