Files
blog-ahmza-com/content/posts/setup-a-hugo-site/index.md
T

351 lines
7.7 KiB
Markdown
Raw Normal View History

2022-12-31 23:57:20 +00:00
---
title: Setup a HUGO Site
2024-11-29 02:26:53 +00:00
description: Always wanted to setup a static website. Look no further !! In this post we go over the basics of setting up a HUGO site
tags:
- blog
- HUGO
- web
- development
- website
author: ahmza
date: 2022-12-31
2022-12-31 23:57:20 +00:00
draft: false
2024-01-05 23:54:51 +00:00
weight:
2024-11-29 02:26:53 +00:00
featureimage: feature.webp
lastmod:
2022-12-31 23:57:20 +00:00
---
## **Introduction**
2024-01-05 23:54:51 +00:00
_HUGO_ is a static site generator, a tool that allows us to pre-compile our
website into static HTML, CSS and JS. The main benefit of an _ssg_ is speed. A
Static site takes a lot less time to load than a site that uses databases.
2022-12-31 23:57:20 +00:00
2024-01-05 23:54:51 +00:00
For more information on HUGO read my other post [Hugo Static Site
Generator](https://blog.ahmza.com/posts/hugo-static-site-generator/)
2022-12-31 23:57:20 +00:00
## **Prerequisites**
2024-01-05 23:54:51 +00:00
2022-12-31 23:57:20 +00:00
- Git
## **Installing HUGO**
2024-01-05 23:54:51 +00:00
2022-12-31 23:57:20 +00:00
[Official steps](https://gohugo.io/installation/)
2024-11-29 02:26:53 +00:00
The _HUGO_ binary is used to initialize our site then create posts etc… Below
2024-01-05 23:54:51 +00:00
are the steps for _MacOS_ and _Arch_ based _Linux_ distros.
### _MacOS_
2022-12-31 23:57:20 +00:00
- Install [homebrew](https://brew.sh/)
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
brew install hugo
```
2024-01-05 23:54:51 +00:00
### _Arch_
```bash
2022-12-31 23:57:20 +00:00
sudo pacman -S hugo
```
### Confirm Installation
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
hugo version
```
2024-11-29 02:26:53 +00:00
## Create Your Site
2024-01-05 23:54:51 +00:00
This assumes choices for you, i.e the theme used.
> The settings in this guide will mimic the base config of my site
```bash
2022-12-31 23:57:20 +00:00
hugo new site -f yml <your-sitename>
cd <your-sitename>
git init
git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod
echo "theme: 'PaperMod'" >> config.yml
```
## Create Content
2024-01-05 23:54:51 +00:00
2022-12-31 23:57:20 +00:00
We will now begin adding new content to our website. Lets create our first post.
Lets start by creating a template for our posts.
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
vim archetypes/posts.md
```
Paste the following base template
2024-01-05 23:54:51 +00:00
```markdown
2022-12-31 23:57:20 +00:00
---
2024-01-05 23:54:51 +00:00
title: { { replace .TranslationBaseName "-" " " | title } }
date: { { .Date } }
2022-12-31 23:57:20 +00:00
author: "<author>"
draft: true
2024-01-05 23:54:51 +00:00
weight:
2022-12-31 23:57:20 +00:00
tags: [""]
description: ""
canonicalURL: "{{ .Page | absURL }}"
cover:
2024-01-05 23:54:51 +00:00
image: "<image path/url>" # image path/url
alt: "<alt text>" # alt text
caption: "<text>" # display caption under cover
relative: false # when using page bundles set this to true
2022-12-31 23:57:20 +00:00
---
## **Introduction**
## **Conclusion**
```
If the posts folder does not exist, create it
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
mkdir content/posts
```
Create and edit a post
2024-01-05 23:54:51 +00:00
> pick an editor of your choice. **I use NVIM BTW**
```bash
2022-12-31 23:57:20 +00:00
hugo new posts/<post-name>.md
vim content/posts/<post-name>.md
```
2024-11-29 02:26:53 +00:00
## Start the Server
2022-12-31 23:57:20 +00:00
2024-01-05 23:54:51 +00:00
Now that we have created our site, installed a theme and created a post lets
start the server.
2022-12-31 23:57:20 +00:00
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
hugo server -D
```
2024-01-05 23:54:51 +00:00
_The -D flag tells hugo server to build drafts. (notice how we have a draft:
option in our posts template.) Of course once you have finished a post you set
draft to false like so draft: false_
2022-12-31 23:57:20 +00:00
```
➜ hugo server -D
Start building sites …
hugo v0.109.0+extended darwin/arm64 BuildDate=unknown
| EN
-------------------+-----
Pages | 10
Paginator pages | 0
Non-page files | 0
Static files | 0
Processed images | 0
Aliases | 2
Sitemaps | 1
Cleaned | 0
Built in 12 ms
Watching for changes in /home/ahmza/prj/ahmza/{archetypes,assets,content,data,layouts,static,themes}
Watching for config changes in /home/ahmza/prj/ahmza/config.yml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/ (bind address 127.0.0.1)
Press Ctrl+C to stop
```
2024-11-29 02:26:53 +00:00
_Notice the second line to the bottom. Web Server… This line gives you a
2024-01-05 23:54:51 +00:00
localhost URL that you can use in your browser to view your site locally._
2022-12-31 23:57:20 +00:00
## Configure Site
Open the config.yml file in a text editor.
2024-01-05 23:54:51 +00:00
```bash
2022-12-31 23:57:20 +00:00
vim config.yml
```
**Important changes you need to make**
2024-01-05 23:54:51 +00:00
2024-11-29 02:26:53 +00:00
- baseURL - External URL. In my case. <https://blog.ahmza.com>
2024-01-05 23:54:51 +00:00
- title - Title of your site.
2022-12-31 23:57:20 +00:00
Here is my config.yml with bits redacted so you can fill them in
2024-01-05 23:54:51 +00:00
```yaml
2022-12-31 23:57:20 +00:00
baseURL: "<your sites URL>"
title: <your site title>
paginate: 5
theme: PaperMod
enableRobotsTXT: true
buildDrafts: false
buildFuture: false
buildExpired: false
enableEmoji: true
googleAnalytics: <Google Analytics Tag>
minify:
disableXML: true
minifyOutput: true
outputs:
home:
- HTML
- RSS
- JSON
params:
env: production # to enable google analytics, opengraph, twitter-cards and schema.
title: <title>
description: "<description>"
keywords: [Blog, Portfolio, PaperMod]
author: <author>
images: ["<link or path of image for opengraph, twitter-cards>"] #Change this
DateFormat: "January 2, 2006"
defaultTheme: auto # dark, light
disableThemeToggle: false
ShowReadingTime: true
ShowShareButtons: false
ShowPostNavLinks: false
ShowBreadCrumbs: true
ShowCodeCopyButtons: true
ShowWordCount: true
ShowRssButtonInSectionTermList: true
UseHugoToc: false
disableSpecial1stPost: false
disableScrollToTop: false
2023-01-01 13:11:48 +00:00
hideFooter: false
2022-12-31 23:57:20 +00:00
comments: false
hidemeta: false
hideSummary: false
showtoc: true
tocopen: false
assets:
# disableHLJS: true # to disable highlight.js
# disableFingerprinting: true
favicon: "/img/favicon.png"
favicon16x16: "/img/favicon.png"
favicon32x32: "/img/favicon.png"
apple_touch_icon: "/img/favicon.png"
safari_pinned_tab: "/img/favicon.png"
label:
text: "<Home>"
icon: /img/favicon.png
iconHeight: 35
# profile-mode
profileMode:
enabled: true # needs to be explicitly set
title: "Welcome \U0001F44B"
subtitle: "<subtitle>"
imageUrl: "/img/favicon.png"
imageWidth: 120
imageHeight: 120
imageTitle: "my image"
buttons:
- name: posts
url: posts
- name: archive
url: archives
- name: tags
url: tags
# home-info mode
homeInfoParams:
Title: "Welcome \U0001F44B"
Content: "subtitle"
socialIcons:
- name: LinkedIn
url: "https://linkedin.com/in/"
- name: gitlab
url: "https://gitlab.com/"
cover:
hidden: false # hide everywhere but not in structured data
hiddenInList: false # hide on list pages and home
hiddenInSingle: false # hide on single page
editPost:
URL: "https://gitlab.com/<user>/<repo>/-/blob/main/content"
Text: "Suggest Changes" # edit text
appendFilePath: true # to append file path to Edit link
# for search
# https://fusejs.io/api/options.html
fuseOpts:
isCaseSensitive: false
shouldSort: true
location: 0
distance: 1000
threshold: 0.4
minMatchCharLength: 0
keys: ["title", "permalink", "summary", "content"]
menu:
main:
- identifier: posts
name: posts
url: /posts/
weight: 1
- identifier: archive
name: archive
url: /archives/
weight: 10
- identifier: tags
name: tags
url: /tags/
weight: 20
- identifier: search
name: search
url: /search/
weight: 30
# Read: https://github.com/adityatelange/hugo-PaperMod/wiki/FAQs#using-hugos-syntax-highlighter-chroma
pygmentsUseClasses: true
markup:
highlight:
noClasses: false
codeFences: true
guessSyntax: true
# lineNos: true
```
2024-11-29 02:26:53 +00:00
## Run the Server Again
2024-01-05 23:54:51 +00:00
Run the server and check out your hard work. Change settings make posts etc.
Have fun !!
```bash
2022-12-31 23:57:20 +00:00
hugo server -D
```
2024-11-29 02:26:53 +00:00
## Publish Your Site
2024-01-05 23:54:51 +00:00
2022-12-31 23:57:20 +00:00
Once you are happy with your site, you may want to publish it to the internet.
2024-01-05 23:54:51 +00:00
In the next post [Publishing your HUGO
site](https://blog.ahmza.com/posts/publishing-your-hugo-site/) I will cover a
basic method of publishing your site as well as a more complicated but automated
workflow that I use.
2022-12-31 23:57:20 +00:00
## **Conclusion**
2024-01-05 23:54:51 +00:00
Through this post you have learned to create your own HUGO site as well as
create and edit posts. We also learned about
2022-12-31 23:57:20 +00:00
- archetypes
2024-01-05 23:54:51 +00:00
- Mark posts as drafts
- The _HUGO_ built in server
2022-12-31 23:57:20 +00:00
- webp + webm (still need to add this)
- shortcodes (still need to add this)