Build Personal Blog with Hugo
Overview
Hugo, blog
Install Hugo
Install the latest version from Hugo Github releases page. E.g. download the *.deb
file and install by dpkg -i *.deb
.
Or use the official installation Guide (not recommended)
Step 1: Create the Required Repositories
On GitHub, create two repositories, both with the Add a README file
option selected:
- Blog source file repository: Store web source files used to generate the blog site (e.g., name it
blog-resources
). - Pages repository: Used to store the generated web files and can be accessed via a URL (name it
<username>.github.io
, where<username>
is your GitHub account name. Official Documentation)
Step 2: Create Blog Files Using Hugo Commands
Using blog-resources
as an example for the source file repository name:
- Clone
blog-resources
to your local machine. - Navigate to the
blog-resources
directory and use the Hugo command to create the overall website file structure:hugo new site <blog-name>
, replacing<blog-name>
with your desired name, e.g.,woods-blog
. - The repository`s file structure and their main purposes at this stage are as follows:
1โโโ README.md # Automatically added README when creating the repo 2โโโ woods-blog # Blog folder name, matching what was created using Hugo in the previous step 3 โโโ archetypes 4 โโโ config.toml # Custom configuration file for the blog site, to be edited further 5 โโโ content # Blog content folder, where blog documents, images, etc., are stored 6 โโโ data 7 โโโ layouts 8 โโโ resources 9 โโโ static 10 โโโ themes # Themes folder, to install Hugo themes further
Step 3: Add Theme Files and Configure
- Browse Hugo themes and choose a theme.
- Theme introductions typically provide installation instructions; you can follow those. However, I have encountered errors when installing some themes. Here, I`ll introduce the theme I chose, Mainroad, for reference.
- Navigate to the blog file path
blog-resources/woods-blog
and add the theme files usinggit submodule
like this:1git submodule add https://github.com/vimux/mainroad.git themes/mainroad
- If added successfully, the
blog-resources/woods-blog/themes
directory will contain the theme namedMainroad
:1โโโ themes 2 โโโ Mainroad # The name of the installed theme
- In
blog-resources/woods-blog/themes/Mainroad/exampleSite
, copy thecontent
andstatic
folders and theconfig.toml
file toblog-resources/woods-blog
, overwriting existing files. - Configure the
blog-resources/woods-blog/config.toml
file. Key items to check include:1# Ensure the URL is the URL of the Pages repository you created, with a trailing slash 2baseurl = "https://<username>.github.io/" 3#The theme is the name of the installed theme 4theme = "mainroad"
Step 4: Test the Blog and Publish as HTML
- Start the Hugo server:
hugo server
and preview the blog theme athttp://localhost:1313/
. - To add blog documents, you can create new
.md
documents inblog-resources/woods-blog/content
for testing. The Hugo server updates in real-time for preview. - Publish the blog as HTML web pages by running
hugo
in theblog-resources/woods-blog/
directory. - If the publishing is successful, a
public
folder will be added inblog-resources/woods-blog/
to store the HTML web page files.
Step 5: Push HTML Files to the Pages Repository
-
Go to the
blog-resources/woods-blog/public
directory and set it as a Pages repository:1# Initialize a new Git repository 2git init 3# Create the main branch 4git checkout -b `main` 5# Add the SSH of the Pages repository to your local repository 6git remote add origin git@github.com:<username>/<username>.github.io.git
-
Push the local files to the repository:
1# Synchronize local and remote commits 2git pull --rebase origin main 3# Add all local changes 4git add . 5# Commit with relevant description 6git commit -m "commit information, e.g., add blog template" 7# Push to the remote repository 8git push origin main
-
Enter the URL in your browser to view the blog at
https://<username>.github.io/
, which may take a few minutes to become accessible.
Next Steps
Push the blog-resources
repository to GitHub.
Reference
comments powered by Disqus