#Building a static blog with Hugo and Netlify
Welcome to the first post on this blog.
I’ll start with how I set up the site itself.
##In this article
- Build a site where you can publish Markdown posts without paying for hosting
##Introduction
WordPress seemed like the obvious choice for a blog.
I already had a WordPress blog, but updating it felt like a chore. Eventually I stopped touching it.
I was still paying Sakura for hosting. It wasn’t expensive, but the bill kept coming.
Paying for a site I wasn’t using felt like a waste.
When I finally logged in and updated the plugins, I barely recognized the post editor.
That was about the last of my enthusiasm gone.
Three things bothered me about the setup:
- Renting a server costs money.
- On modest hardware the site can feel sluggish.
- Plugin updates keep changing the writing environment.
I googled the decidedly unscientific phrase “WordPress is a hassle” and found someone describing much the same experience.
Their list of frustrations could have been mine.
They also gave me a term to look up: static site generator.
##What you need
I ended up needing two things: a generator and somewhere to host what it builds.
###Static site generators
I’d only just learned the term. Here’s how I understood the trade-offs:
- The generator turns Markdown articles into a website.
- The entire site can live in a Git repository.
- The content needs no database.
- That leaves less infrastructure to secure.
- Pages are built at deploy time, so the server can deliver ready-made files quickly.
- Features that need a live database, such as a built-in comment thread, need a different approach.
- For example, you can use GitHub Issues to provide comments.
I could already write Markdown in WordPress with a plugin. What I disliked was copying each article from my text editor into the WordPress editor.
Having one less database to secure appealed to me too.
There are plenty of generators, each with its own approach:
###Web hosting
A generator builds the files locally. To make them public, I still needed a server to serve them.
That’s where static hosting comes in.
Three services came up repeatedly when I looked into static hosting:
Some of them can even build the site automatically from a GitHub repository.
That sounded pretty handy.
##Setting up the site
###Choosing the tools
There were more options than I could reasonably try, so I picked two that looked promising.
- Static site generator: Hugo
- Hugo is written in Go, which I’d just started learning.
- It also has a reputation for fast builds.
- Hosting: Netlify
- Its free tier looked particularly generous.
- Automatic builds from GitHub looked easy to configure.
Here’s the writing workflow I wanted:
- Write the post in a text editor.
- Use Markdown in Vim, Visual Studio Code, or another editor of your choice.
- Run a local server to see each change as you type.
- Push the finished post to GitHub.
- The repository doubles as a backup.
- Netlify detects the push, builds the site, and publishes the result.
- A Git push is all it takes to update the live blog.
###Setup steps
I think we will follow these steps basically.
- Installing Hugo
- Creating the site
- Pushing to GitHub
- If you haven't already, register as a GitHub user.
- Creating a Netlify account
- Connecting Netlify to GitHub
Installing Hugo
This procedure may differ based on the environment (OS) of the PC you are using.
Since I am using Ubuntu18.04, I will continue to explain it with that as the main focus.
If you are using any other OS, I think it's fine if you proceed by referring to the links below (blame it on me).
Ultimately, if the HUGO version is displayed in the following command, it's OK.
hugo version
> Hugo Static Site Generator v0.63.0-DEV/extended linux/amd64 BuildDate: unknown
Then, I will explain how to install it on Ubuntu 18.04.
There seem to be several installation methods.
By the way, I built it from the source.
- Installing with Snap
- Installing with go get
- Building from source
Installing with Snap
The easiest method seems to be using snap.
By the way, it can be installed via apt, but it seems to be an older version.
Open the terminal and enter the following command.
sudo snap install hugo --channel=extended
Installing with go get
If you have set up the Go environment, you can also use go get.
go get hugo
If you haven't set up the Go environment, refer to this.
Building from source
To obtain the source code from the repository and build it, follow these steps.
In this case, the Go language environment is also required.
Run the following command to build from the source code.
It seems that by adding --tags extended, you can build a version that can use Sass/SCSS and other languages.
git clone https://github.com/gohugoio/hugo.git
cd hugo
go install --tags extended
Eventually, the installation will complete.
After completion, run the previously mentioned version verification command to verify the functionality.
Once the version is confirmed successfully, this procedure is complete.
Creating the site
Starting a new site
Next, let's actually start creating the site.
Navigate to the location where you want to create the work directory and run the following command.
hugo new site (作業ディレクトリ名)
This creates a working directory for the site.
Next, choose the theme to use on the site.
Browse the Hugo theme gallery and pick a theme you like.
Once the theme is decided, install it by referring to the Getting started page of the corresponding theme.
It will basically involve running the following command.
cd (作業ディレクトリ名)
git submodule add (該当テーマのGitHub URL) themes/(テーマ名)
You can proceed with your work by referring to the pages of the theme you want to use.
I think we'll need to modify a few configuration files.
In most themes, there is a directory called exampleSite below themes/(theme name),
By copying this to the work directory's root, you can create a sample environment.
I think it's the quickest way to fix this sample and set it up for your own use.
Once you've completed this, run the following command to build the site in the local environment.
hugo server -D
The -D option seems to be a mode that previews draft articles as well.
I will explain the draft article later.
The following text will be displayed when the server has successfully started up.
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
Open http://localhost:1313/ in your browser.
Once the web page for the theme you selected is displayed, the site construction is complete.
By the way, HUGO allows for quick builds by using caching during the build process.
However, sometimes there are phenomena such as the image not being updated due to this.
In that case, you can reload the image by disabling the cache.
hugo server --ignoreCache
Writing a new post
Next, I will explain how to create a new article.
When creating, execute the following command in the root of the work directory.
hugo new posts/(ファイル名).md
You can set the file name freely and that's okay.
The file name you set here will remain the same as the URL of this article, so it's better not to change it too frequently.
Let's open the .md file we created.
I can use any text editor, but I use Neovim.
There should be some text already present, though it may vary slightly depending on the theme.
---
author: 'liebe-magi'
title: 'Test'
date: 2020-03-17T23:00:05+09:00
description:
featured_image: 'thumbnail/sample.png'
image: 'logo/Freaky_rectangle.png'
tags:
-
series:
-
categories:
-
draft: True
---
By the way, this template can be modified by editing the file archtypes/default.md when creating a new one.
Customizing it to your liking makes it more comfortable.
I think it varies by theme, but the meaning of each parameter is as follows.
- author
- Author name
- title
- The title of the article
- The default is the file name.
- date
- The creation date and time
- description
- Summary of the article
- featured_image
- Set an image as the thumbnail for the article
- Images should be placed under static/images.
- image
- The image set in the OGP tag (displayed when a URL is added to it, such as on Twitter)
- Place the image under the static section.
- tags
- Set the tags for the article
- series
- Set the series for the article
- categories
- Set the category for the article
- draft
- Setting it to true will treat the article as a draft and it will not be displayed in the live environment.
- When building locally with the -D option, you can preview it.
This text will be written in Markdown under these settings.
Every time an article is updated, it is immediately rebuilt and reflected on the web page.
When you place the text editor on the left side of the screen and the browser on the right side to work, you can immediately see the preview of what you have entered, which is really smooth.
Once the article is complete, it's time to proceed with preparations for its publication.
Pushing to GitHub
Once the article is ready, create a repository on GitHub and push it.
Here, I won't introduce the detailed usage of GitHub.
I think there are many articles that can be helpful when you search online.
As a supplement, the repository can be private.
In fact, considering that there are draft articles being created, I think it's better not to make them public.
Creating a Netlify account
It's time to proceed with the Netlify settings.
Before that, if you don't have an account, you will need to register as a user.
Access the site and perform user registration.
The steps are as follows.
- Click "Get started for free"
- Select the account to use for signing up the user
- It's okay to choose any service, but I registered using a GitHub account.
- Proceed with the integration settings
- Access the link that arrives in your email address and activate your account.
Your account creation is now complete.
Connecting Netlify to GitHub
Establish a connection between netlify and GitHub and set up the environment for automatic builds.
- Log in to Netlify
- Click on "New site from Git"
- As a connected service, "GitHub" was selected.
- Log in to your GitHub account
- Select the HUGO site repository
- Click on "Deploy site"
- The site is deployed.
- Edit the URL of the deployed site
- Edit the "Change site name" in the "Site settings".
- Copy the edited URL and paste it into the "baseURL" field in HUGO's "config.toml".
- Push to GitHub again
- Access the URL to confirm that the site displays without any issues.
That completes the initial setup.
##Wrapping up
I can now publish updates by editing a file and pushing to GitHub.
Next I’d like to move the articles from my WordPress blog over here.