What is Hugo
According to the official website, Hugo is a fast and modern static site generator written in Go. The advantages of static web pages include performance and security, which are the main attractions.
With Hugo, Markdown content is encoded into HTML pages. Since the HTML pages are pre-built, users can load them with almost zero latency.
A key feature is that Hugo does not require backend programming or a database. This makes it more secure and less vulnerable compared to non-static web pages.
Dynamic vs Static Web Pages
Dynamic web pages change content based on user requests, actions, or other conditions at the time of the visit.
Using server-side scripting languages, the server executes the relevant programs and then returns the results to the user.
Common languages in order of popularity: Node.js, PHP, Ruby on Rails, Go, Python, ASP.NET, JSP, CGI, etc…
In the past, I used WordPress, a content management system (CMS) developed in PHP, to write blogs. Setting up WordPress requires a host, either Windows or Linux, usually provided by a hosting provider, and involves obtaining FTP or SSH configurations. After setting up the host, Apache or Nginx is installed to handle HTTP requests, along with PHP for running WordPress. For storing posts, MySQL or MariaDB databases are needed, and phpMyAdmin can be installed for database management if necessary.
Once WordPress is running, settings, SEO optimization, theme customization, and plugin installations are performed. Most mainstream travel blogs in Taiwan still use this system. The dynamic web page process is as follows:
User sends Request → Apache → PHP → Database → HTML → Apache → Response to User
In contrast, static web pages only require Apache to handle the user’s Request and Response, without PHP processing. The main advantages are speed and security, as there is no need to wait for backend processing. The static web page process is:
User sends Request → Apache → HTML → Apache → Response to User
The difference lies in whether server-side programs are executed to render the page. Static web pages do not require database access as most content is pre-compiled into single HTML files.
Site Building Process
The site-building process can be summarized in the following steps, which will be detailed further in subsequent sections:
- Start and Install (Hugo, Git)
- Start a New Site
- Initialize Git Repo
- Install a Hugo Theme
- Create New Content
- Configure Site Details
- Run Hugo Server
- Push to Remote Server
- Build and Deploy the Site
Start and Install
Installing Hugo on Windows and Linux is quick and easy, recommended via command line for consistency.
Besides Hugo, Windows users need to install Git for source file management.
Future article edits can be quickly reverted using Git.
Install Hugo via Winget (Windows)
|
|
Result:
Install Hugo via Snap (Linux)
|
|
Result:
wells@server:~$ sudo snap install hugo
[sudo] password for wells:
hugo 0.120.4 from Hugo Authors installed
Install Hugo via Homebrew (macOS, Linux)
Before using brew, install Homebrew
|
|
Install Git via Winget (Windows)
Assuming Git is pre-installed on Linux or Mac, here is the Windows installation:
|
|
Check Executable Versions
Verify successful installation and check versions. Currently installed versions are Hugo v0.120.4 and Git v2.43.0
|
|
Start a New Site
In a directory where you want to store your blog source files, choose your Home directory (~/) on Linux or the User directory (C:\Users\USER) on Windows. You can also choose other locations as desired. To start a new site, enter hugo new site BLOG_NAME and navigate into the directory. Here is an example using my_blog
:
|
|
A new blog site is created, which by default has no theme or content. Direct compilation will result in an empty site.
Initialize Git Repo
Navigate to the blog directory (~/my_blog) and initialize a local Git repo with git init. Use git status to check Untracked files.
By default, Git does not track any files. Add hugo.toml, archetypes, and other relevant files to the index (git add).
|
|
Install Hugo Theme
To install a Hugo theme, visit Hugo Themes and choose a theme. Theme providers usually provide installation instructions.
Using PaperMod as an example, follow the PaperMod - Installation guide. Method 1 involves git cloning in the site directory.
|
|
Edit ~/my_blog/hugo.toml
and add the following line at the end to apply the theme. Note that the case is important:
|
|
Start a New Article
With the following command, you can create a new article:
|
|
Editing ~/my_blog/content/posts/my-first-post.md
is equivalent to writing an article. The syntax used is Markdown. Remember to change draft = true
to draft = false
to ensure that the article is in the release state.
my-first-post.md
|
|
Website Configuration
At this stage, you can actually run the Hugo server and view your blog. However, before running the server, you might want to consider configuring some blog details, such as:
- Editing the language in
~/my_blog/hugo.toml
- Blog title
- Whether to enable emojis (
enableEmoji
) - The website’s favicon
- Summary
- Other configurations…
Due to the extensive settings available, you can also dynamically adjust them after running the server.
For more configuration options, refer to Configure Hugo. At this point, you might also consider converting hugo.toml
to hugo.yaml
if you prefer.
Additionally, different themes have theme-specific configurations, usually specified in the theme’s config
settings.
Running the Hugo Server
According to the official website, Hugo is a high-performance web server, though it is mainly convenient for local testing. You can run the server with the following command:
|
|
Including the -D
flag will also build draft articles. By default, the final build will not compile draft articles.
|
|
After starting the server, you can open http://localhost:46651 in your browser to view the blog and articles. Any subsequent edits to the configuration or modifications to the articles will be updated in real-time.
Pushing to a Remote Server
Once the theme is installed, articles are added, and configuration files are edited, the next step is to manage the changes in the directory using git and push them to a remote server for management and maintenance.
Since we previously only performed git init
without adding a remote server, let’s assume that a GitHub repository has already been created, or another server is ready with the repository. You will eventually obtain an SSH connection string:
|
|
You can add and check the current local repository’s remote status using git remote add
and git remote -v
:
|
|
Ensure that all modified files have been added to the index using git add
, and that the changes are committed using git commit
. Once this is done, you can push the local changes to the remote server using git push
.
Building and Publishing the Website
Building the website is straightforward. Simply enter the command hugo
, and it will generate the ~/my_blog/public
directory. The HTML files within this public
directory can be uploaded to a server for others to view. There are several free static site hosting services available online, such as:
Among these, we are currently using GitHub Pages, which supports custom domains and integrates with repositories and GitHub Actions.
This setup allows for automatic building and pushing to GitHub Pages after each repository push. The free monthly quota is quite sufficient for most users.
Conclusion
Creating a Hugo static website is not particularly difficult. The steps are straightforward: install Hugo and Git → install a theme → add articles → publish.
Introducing Git into the process ensures that you can track and revert changes made to the website. While it’s technically possible to skip git init
, it is highly discouraged. Reverting changes manually can be quite challenging if the website gets accidentally broken.