Build a Personal Website with Hugo
In this post, I will show you how to build a personal website with Hugo and configure the theme DoIt.
Quick Start
Install Hugo
In macOS, you can use Homebrew to install Hugo.
|
|
Then add the theme DoIt as a submodule of the hugo project.
-
Create a new hugo project:
1 2
hugo new site my_website cd my_website
-
Initialize your project directory as a git repository, and add the theme repository as a submodule of your website directory:
1 2
git init git submodule add https://github.com/HEIGE-PCloud/DoIt.git themes/DoIt
-
Initialize the project:
git submodule update --init --recursive
, and finish the installation. -
Update the submodule:
git submodule update --remote --merge
. -
Create your first post!
hugo new posts/hello-world.md
. -
Preview your website:
hugo server --disableFastRender -D
.
|
|
-
hugo.toml
: The configuration of Hugo, including the configuration of the theme. See here for details. -
content
: Save the content of the website, such as posts.1 2 3 4 5 6 7 8 9 10 11 12
. └── content └── about | └── index.md // <- <https://example.com/about/> ├── posts | ├── firstpost.md // <- <https://example.com/posts/firstpost/> | ├── happy | | └── ness.md // <- <https://example.com/posts/happy/ness/> | └── secondpost.md // <- <https://example.com/posts/secondpost/> └── quote ├── first.md // <- <https://example.com/quote/first/> └── second.md // <- <https://example.com/quote/second/>
-
archetypes
: Save the markdown templates of the posts, usually including the front matter of the posts. -
static
: Save the static files used in the posts, these files will be copied to thepublic
folder when generating the website. -
public
: The static HTML, CSS, JS, etc. generated by thehugo
command. This folder is mainly published on the server.
Deploy
We can create two repositories on GitHub to deploy the website.
- Repository 1: A repository used to host the source files of the website project, set the permission to Private. We will spend most of our time on this repository.
- Repository 2: A repository used to host the static files generated by the website, set the permission to Public. The name of this repository should be
{{your_github_username}}.github.io
. For example, my GitHub username isfakephysicist
, then the name of the repository I need to create isfakephysicist.github.io
. In the settings of this repository, enable GitHub Pages, setBranch
tomain
, and set the static file location to/(root)
. We will directly push the contents of thepublic
folder generated by thehugo
command to the/
directory of themain
branch.
Create these two repositories on GitHub, and then follow the steps below to deploy the website.
-
In the
my_website
directory, executegit submodule update --init --recursive
to update the submodules to the latest state. -
In
hugo.toml
, setbaseurl = https://fakephysicist.github.io/
. -
Set the
fakephysicist.github.io
repository as a submodule and set it in thepublic
folder.1
git submodule add -f -b main <https://github.com/fakephysicist/fakephysicist.github.io.git> public
-
Make sure the
public
folder is deleted. Makefakephysicist.github.io
repository as a submodule, and set it in thepublic
folder.1
git submodule add -f -b main https://github.com/fakephysicist/fakephysicist.github.io.git public
-
Generate the website, and push the generated website to repository 2.
1 2 3 4 5 6 7
hugo cd public git add . git commit -m "Build website" git push origin main cd .. rm -rf public
-
Add, commit and push repository 1.
1 2 3
git add . git commit -m "Publish website" git push -u origin master
Next time you want to update the website, just execute the following script.
|
|
Get current timestamp
|
|
One can use the above command to get the current timestamp in the format of 2024-08-04T14:20:29-07:00
.
This timestamp can be used in the front matter of the posts.
|
|