手把手使用Hugo搭建网站-初级篇
文章目录
1.1. 手把手使用hugo搭建网站初级篇
1.1.1. Demo使用hugo的网站
- https://www.flysnow.org/
- https://www.gohugo.io
- https://www.netlify.com/
- https://gohugo.io/showcase/
- https://www.smashingmagazine.com/
1.1.2. 部分主题
- https://github.com/Vimux/Mainroad
- https://github.com/kingfsen/Mainroad
- https://themes.gohugo.io/academic/
- https://themes.gohugo.io/beautifulhugo/
- https://themes.gohugo.io/hyde/
- https://themes.gohugo.io/hugo-theme-even/
- https://github.com/rujews/maupassant-hugo
1.1.3. hugo跟wordpress其他建站工具的对比
-
wordpress 全球31%的网站使用wordpress,尽管他有各种主题和插件,但是也有非常多的缺点,安全性,seo不够友好,定制麻烦
-
hugo 最快的静态生成工具,seo友好,静态更安全,方便定制模板,缺点没有插件,如果要定制模板只能懂一点go的语法
-
wordpress是动态的并且还需要托管数据库,所以托管费用比较昂贵
-
hugo是生成静态的页面,在本地生成后上传到服务器就可以了,托管费用非常便宜,可以直接用免费的github托管
-
hugo不可以在线编辑
-
wordpress vs hugo
Tables | 静态/动态 | 托管 | 安全性 | 访问速度 | 在线编辑 | markdown |
---|---|---|---|---|---|---|
wordpress | 动态 | 复杂 | 需要经常升级 | 快 | 可以 | 不支持 |
hugo | 静态 | 简单 | 不需要打补丁 | 非常快 | 不可以 | 支持 |
1.1.4. 分钟0基础使用hugo搭建个人博客网站(20m)
-
安装hugo
- 下载 https://github.com/gohugoio/hugo/releases/download/v0.58.3/hugo_extended_0.58.3_Windows-64bit.zip
- 解压到e:\hugo\bin
- 运行命令行程序cmd,cd e:\hugo\bin
- 执行hugo version
- set PAHT=%PATH%;e:\hugo\bin
-
安装git 并clone 代码
- 下载并安装git
-
运行hugo默认的theme
- cd e:\hugo\sites
- hugo new site example.com
- cd example.com\theme
- git clone https://github.com/spf13/hyde.git
-
创建个人博客:支持菜单和tag
- cd e:\hugo\sites\example.com
- 打开config.toml
1baseURL = "http://clouda3.github.io/" 2[menu] 3[[menu.main]] 4 identifier = "blog" 5 name = "Blog" 6 url = "/post/"
- hugo new post/first.md
- hugo server --theme=hyde -v -D
- 访问http://127.0.0.1:1313
1.1.5. 详解hugo使用的工具Git
-
Git 是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。1Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
-
GitHub是一个开源的代码托管平台,使用GitHub可以查看别人的项目、可以建立静态网页、可以管理插件、可以在线编译、可以托管代码等等
- 注册github www.github.com
- 登录:username or email account:xxx@xxx.com /password
- 创建repositories:create https://github.com/clouda3/test
- cd e:\hugo\sites\example.com,执行....\hugu.exe
- cd e:\hugo\sites\example.com\public
- git init
- git remote add origin https://github.com/clouda3/test.git
- git add .
- git commint -m "init commit"
- git push -u origin master
1.1.6. 使用githubpages托管个人网站
- 上一步将网站生成的代码提交到gihub上,利用已经提交的代码托管
- github repository->setting ->github pages ->source :master branch
- 访问一下https://clouda3.github.io/test
- 绑定域名test.clouda3.com,我用的是亚马逊的route53.CNAME value:https://clouda3.github.io
- github pages->setting ->custom domain test.clouda3.com
- 访问一下test.clouda3.com
1.1.7. 解析域名到github pages
- 在aws route53申请域名(或者aliyun或者腾讯云申请)
- 域名的cname指向 test.clouda3.com
- 在github settings ->github pages ->custom domain 绑定域名www.xxxx.com
- 访问www.xxxx.com就可以访问到网站了
1.1.8. icon/robot.txt/404
- icon 要放再static 或者theme/static目录下
1 <head>
2 <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
3 <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
4 </head>
- 404页面生成需要模板, 在theme/layouts/404.html
1{{ partial "head" . }}
2 <body>
3 {{ partial "header" . }}
4 <div class="body404">
5 <div class="info404">
6 <header id="header404" class="clearfix">
7 <div class="site-name404"><i>404</i></div>
8 </header>
9 <section>
10 <div class="title404">
11 <p>页面去月球了吗?回去找找看</p>
12 </div>
13 {{ partial "related" . }}
14 <a rel="nofollow" href="{{ .Site.BaseURL }}" class="index404">回首页看看</a>
15 </section>
16 </div>
17 </div>
18 {{ partial "footer" . }}
- robot.txt生成也需要模板layouts/robot.txt,同时config.toml:enableRobotsTXT = true,layout/robot.txt
1 User-agent: Baiduspider
2 Disallow:
1.1.9. 创建网站地图
- hugo 默认有internal sitemap 模板
- 如果需要定制sitemap模板可以在layouts/sitemap.xml
1 {{ printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" | safeHTML }}
2 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
3 xmlns:xhtml="http://www.w3.org/1999/xhtml">
4 {{ range .Data.Pages }}
5 <url>
6 <loc>{{ .Permalink }}</loc>{{ if not .Lastmod.IsZero }}
7 <lastmod>{{ safeHTML ( .Lastmod.Format "2006-01-02T15:04:05-07:00" ) }}</lastmod>{{ end }}{{ with .Sitemap.ChangeFreq }}
8 <changefreq>{{ . }}</changefreq>{{ end }}{{ if ge .Sitemap.Priority 0.0 }}
9 <priority>{{ .Sitemap.Priority }}</priority>{{ end }}{{ if .IsTranslated }}{{ range .Translations }}
10 <xhtml:link
11 rel="alternate"
12 hreflang="{{ .Lang }}"
13 href="{{ .Permalink }}"
14 />{{ end }}
15 <xhtml:link
16 rel="alternate"
17 hreflang="{{ .Lang }}"
18 href="{{ .Permalink }}"
19 />{{ end }}
20 </url>
21 {{ end }}
22 </urlset>