使用Hugo搭建静态站点

hugo下载地址:https://github.com/gohugoio/hugo

模板列表:https://github.com/gohugoio/hugoThemes

开始搭建

1、下载和初始化

  • 安装包
wget https://github.com/gohugoio/hugo/releases/download/v0.60.0/hugo_0.60.0_Linux-64bit.tar.gz
tar -zxvf hugo_0..0_Linux-64bit.tar.gz
chmod +x hugo cp hugo /usr/bin/ hugo new site abc.red
  ▸ archetypes/ #包括内容类型,在创建新内容时自动生成内容的配置
▸ content/ # 网站内容,全部使用markdown格式
▸ layouts/ # 网站模板文件,决定内容如何呈现
▸ static/ # 图片、css、js 等静态资源
▸ themes/ # 存放主题
config.toml # 是网站的主配置文件
  • 源码编译
#emm首先你服务器环境得有golang的环境,这里就不阐述了

go get -u -v github.com/spf13/hugo
go build -o hugo main.go
mv hugo $GOPATH/bin 在命令行下执行hugo命令,如果得到类似下面结果,则说明你已经成功安装了Hugo: hugo version Hugo Static Site Generator v0.60.0-F2DEA9B0 linux/amd64 BuildDate: --27T10::48Z

2、皮肤模板

git clone --recursive https://github.com/spf13/hugoThemes themes下载全部主题
cd theme
git clone https://github.com/coderzh/hugo-pacman-theme
#这个皮肤也不错
git clone https://github.com/spf13/hyde.git

我使用的皮肤

cd themes
wget https://github.com/digitalcraftsman/hugo-strata-theme/archive/master.zip unzip master.zip
mv hugo-strata-theme-master hugo-strata-theme
cat hugo-strata-theme/exampleSite/config.toml > ../config.toml
vim ../config.toml #Update the base URL as shown below:
baseurl = "/"
#Also add the following lines to include your new about page:
[[menu.main]]
name = "About"
url = "about"
weight =

修改layouts/index.html;

vim layouts/index.html
#添加如下内容
{{ define "main" }}
{{ if not .Site.Params.about.hide }}
{{ partial "about" . }}
{{ end }} {{ if not .Site.Params.portfolio.hide }}
{{ partial "portfolio" . }}
{{ end }} {{ if not .Site.Params.recentposts.hide }}
{{ partial "recent-posts" . }}
{{ end }} {{ if not .Site.Params.contact.hide }}
{{ partial "contact" . }}
{{ end }}
{{ end }}

3、修改config.toml配置

baseurl = "/" #站点的名称
languageCode = "zh-CN" #使用的编码
title = "landv" #大标题
theme = "hugo-strata-theme"#模板
disqusShortname = "spf13" #Disqus评论
# Enable Google Analytics by inserting your tracking code
googleAnalytics = ""
# Define how many posts should appear on a site
paginate = [params]
# To provide some metadata for search engines feel free to add a few
# information about you and your website.
name = "Your name"
description = "Describe your website" # Format dates with Go's time formatting
date_format = "2006-01-02" # Add read time estimates in the format "X min read"
show_read_time = false # Add custom assets with their paths relative to the static folder
custom_js = []
custom_css = [] [params.sidebar]
# Replace the avatar with a picture of your own under static/images
avatar = "avatar.jpg"
# Use Markdown to format the string. This works nearly all over the theme
bio = "**I'm Strata**, a super simple<br> responsive site template freebie<br> crafted by [HTML5 UP](//html5up.net)."
# The icons will be shown if you enter your username
twitter = "spf13"
github = "spf13"
gitlab = "spf13"
dribbble = ""
facebook = ""
googleplus = ""
flickr = ""
linkedin = "" copyright = [
"&copy; John Doe",
"Design: [HTML5 UP](//html5up.net)",
"Demo Images: [Unsplash](//unsplash.com/)"
] [params.about]
hide = false
title = "Ipsum lorem dolor aliquam ante commodo magna sed accumsan arcu neque."
content = "Accumsan orci faucibus id eu lorem semper. Eu ac iaculis ac nunc nisi lorem vulputate lorem neque cubilia ac in adipiscing in curae lobortis tortor primis integer massa adipiscing id nisi accumsan pellentesque commodo blandit enim arcu non at amet id arcu magna. Accumsan orci faucibus id eu lorem semper nunc nisi lorem vulputate lorem neque cubilia." [params.portfolio]
hide = false
title = "Recent Work" # The images and thumbnails are stored under static/images
# Create and change subfolders as you like
[[params.portfolio.gallery]]
image = "fulls/01.jpg"
thumb = "thumbs/01.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [[params.portfolio.gallery]]
image = "fulls/02.jpg"
thumb = "thumbs/02.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [[params.portfolio.gallery]]
image = "fulls/03.jpg"
thumb = "thumbs/03.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [[params.portfolio.gallery]]
image = "fulls/04.jpg"
thumb = "thumbs/04.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [[params.portfolio.gallery]]
image = "fulls/05.jpg"
thumb = "thumbs/05.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [[params.portfolio.gallery]]
image = "fulls/06.jpg"
thumb = "thumbs/06.jpg"
title = "Lorem ipsum dolor."
description = "Lorem ipsum dolor sit amet." [params.recentposts]
hide = false
title = "Recent blog posts" [params.contact]
hide = false
title = "Get In Touch"
content = "Accumsan pellentesque commodo blandit enim arcu non at amet id arcu magna. Accumsan orci faucibus id eu lorem semper nunc nisi lorem vulputate lorem neque lorem ipsum dolor." # This can be exact coordinates or a searchable address in google maps
gmaps_address = "39.7889055,-120.5443524"
address = [
"1234 Somewhere Rd.",
"Nashville, TN 00000",
"United States"
]
phone = "000-000-0000" # Since this template is static, the contact form uses www.formspree.io as a
# proxy. The form makes a POST request to their servers to send the actual
# email. Visitors can send up to a emails each month for free.
#
# What you need to do for the setup?
#
# - set your email address under 'email' below
# - upload the generated site to your server
# - send a dummy email yourself to confirm your account
# - click the confirm link in the email from www.formspree.io
# - you're done. Happy mailing!
email = "hello@example.com" [params.contact.form]
# Set custom strings for the form if your native
# language isn't English
name = "Name"
email = "Email"
message = "Message"
submit = "Send message" # Menu links that appear on the left sidebar
[[menu.main]]
name = "Home"
url = "/"
weight = [[menu.main]]
name = "Blog"
url = "/post/"
weight =

4、重建静态

在站点根目录输入hugo

hugo

                   | EN
+------------------+----+
Pages |
Paginator pages |
Non-page files |
Static files |
Processed images |
Aliases |
Sitemaps |
Cleaned |

常用命令

#使用方法:
hugo
hugo [flags]
hugo [command]
hugo [command] [flags] #查看版本
hugo version #版本和环境详细信息
hugo env #创建新站点
hugo new site "$mysite" #创建文章
hugo new index.md 在content/文件夹可以看到,此时多了一个markdown格式的文件index.md,打开文件可以看到时间和文件名等信息已经自动加到文件开头,包括创建时间,页面名,是否为草稿等。 #编译生成静态文件
hugo Hugo将编译所有文件并输出到public目录 #编译生成静态文件并启动web服务
hugo server

常用参数

 --bind="127.0.0.1"    服务监听IP地址;
-p, --port= 服务监听端口;
-w, --watch[=true] 监听站点目录,发现文件变更自动编译;
-D, --buildDrafts 包括被标记为draft的文章;
-E, --buildExpired 包括已过期的文章;
-F, --buildFuture 包括将在未来发布的文章;
-b, --baseURL="www.datals.com" 服务监听域名;
--log[=false]: 开启日志;
--logFile="/var/log/hugo.log": log输出路径;
-t, --theme="" 指定主题;
-v, --verbose[=false]: 输出详细信息

常用参数组合

hugo server -t hyde --buildDrafts --baseURL=http://abc.red  --bind=0.0.0.0 --port=80 -w 

hugo server --baseUrl=lv.abc.red --bind="0.0.0.0" --port=
-t hyde        使用hyde主题,如果使用-t 选择了主题会将当前默认的主题覆盖;
--buildDrafts参数将生成被标记为草稿的页面,是否发布:hugo 会忽略所有通过 draft: true 标记为草稿的文件。必须改为 draft: false 才会编译进 HTML 文件。
--baseURL=http://www.datals.com 站点监听域名
--bind=0.0.0.0 监听全部网段
--port= 服务监听端口
-w 如果修改了网站内的信息,会直接显示在浏览器的页面上,不需要重新运行hugo server,方便我们进行修改。

如何启动

hugo自承载

hugo server --baseUrl=lv.abc.red --bind="0.0.0.0" --port=80 -w

httpd快速静态部署

yum install httpd
#hugo 生成public目录
cp -R ./public/* /var/www/html/
Service httpd start

发布到github

生成文件的github发布目录命令

sudo hugo server --theme=hyde --baseUrl='https://landv.github.io' --watch --port= -d ./public

发布文件中犹豫baseUrl需要替换为github路径,所以需要注意几点:

1.添加baseUrl,如:–baseUrl=‘https://landv.github.io'
2.url修改为https,现在github都是https的链接
3.设定对应的绑定端口,http设定port=80,https设定port=443

github相关问题

1.创建github目录文件
在github中New reository,用户名一定要用:你的注册用户名.github.io,其他无需修改,然后点击Create repository即可
2.github本地文件提交

// 1.设置配置
git config --global user.name "你的注册用户名"
git config --global user.email "你的邮箱"
git config --global color.ui auto
// 2.设置生成的ssh key
ssh-keygen -t rsa -C "你的邮箱"
要求输入存储id_rsa的目录,接着是输入密码,这个密码可以与github的不同,过程中确认即可,
最后会出现一个随机图形,说明生成SSH密匙成功,这样需要到刚刚的存储目录下打开id_rsa.pub复制里面的内容,
到你的github页面上->点击右上角头像旁三角->settings->SSH keys->add SSH key->黏贴刚刚复制的内容保存
// 3.初始化与提交github
cd public
git init
git remote add origin https://github.com/用户名/用户名.github.io.git
git add -A
git commit -m "提交注释"
git push -u origin master
// 4.修改文件提交
git add -A
git commit -m "提交注释"
git push -u origin master

hugo写markdown文件规范

文件头部格式:

+++
Categories =["github"]
Tags = ["github", "开发者", "go"]
date = "2019-12-12T18:20:42+08:00"
title = "hugo搭建github博客过程"
description = "landv"
+++
  • 1.title 文章名称
  • 2.description 文章详细介绍
  • 3.tag 标签
  • 4.categories 文章分类

在 +++ 与 +++ 中写完规范以后,然后可以添加markdown格式的正文内容(markdown格式文件暂时不介绍,可以参考https://segmentfault.com/markdown)

[golang][hugo]使用Hugo搭建静态站点的更多相关文章

  1. Hexo搭建静态博客踩坑日记(一)

    前言 博客折腾一次就好, 找一个适合自己的博客平台, 专注于内容进行提升. 方式一: 自己买服务器, 域名, 写前端, 后端(前后分离最折腾, 不分离还好一点)... 方式二: 利用Hexo, Hug ...

  2. 利用hugo生成静态站点

    动机 使用Markdown撰写博客,并以静态页面形式发布. 选择hugo 现在jekyll似乎更加流行,但是jekyll是基于Ruby的,在windows下安装很繁琐. 而hugo是用go写的,win ...

  3. Hugo + Github Pages 搭建个人博客

    尝试过 Hexo .GatsbyJs. Vuepress 搭建博客后,对这些工具最大的不满,就是运行速度以及打包速度. 后来看到 Hugo ,号称最快的静态站点生成器后. 尝试搭建博客,发现不管是运行 ...

  4. 使用Hugo和GitHub搭建博客

    折腾了几天博客的框架终于搭建起来了.研究了一番之后,最终还是选择使用Hugo和GitHub来搭建博客.本文介绍了如何使用Hugo来搭建静态博客网站,并将其部署在GitHub上.使用https://&l ...

  5. 用 hugo 和 netlify 搭建blog【转】

    用 hugo 和 netlify 搭建blog - kok的笔记本 Releases · gohugoio/hugo · GitHub 测试baidu 测试163

  6. WIN7搭建ASP站点

    在WIN7配置IIS用于搭建ASP站点(非ASP.NET) ,仅安装.配置必要文件. 1.安装IIS管理工具,用于支持静态页面. 2.添加匿名访问权限. 搭建站点指定到特定文件夹,浏览静态页面会报如下 ...

  7. windows上使用mkdocs搭建静态博客

    windows上使用mkdocs搭建静态博客 之前尝试过用HEXO搭建静态博客,最近发现有个叫mkdocs的开源项目也是搭建静态博客的好选择,而且它支持markdown格式,下面简要介绍一下mkdoc ...

  8. CentOS 6(64-bit) + Nginx搭建静态文件服务器

    Nginx搭建静态文件服务器 使用命令打开Nginx配置文件: sudo vim /etc/nginx/conf.d/default.conf 将配置改为: server { ...... ..... ...

  9. Ubuntu 14.04TLS Nginx搭建静态文件服务器

    Nginx搭建静态文件服务器 [官方教程]https://www.nginx.com/resources/admin-guide/serving-static-content/ 打开Nginx的配置文 ...

随机推荐

  1. 网络编程之TCP三次握手与四次挥手、基于TCP协议的套接字编程

    目录 TCP三次握手和四次挥手 背景描述 常用的熟知端口号 TCP概述 TCP连接的建立(三次握手) TCP四次挥手 如果已建立连接,客户端突然断开,会怎么办呢? 基于TCP协议的套接字编程 什么是S ...

  2. vue的v-bind详解

    前言 v-bind 主要用于属性绑定,比方你的class属性,style属性,value属性,href属性等等,只要是属性,就可以用v-bind指令进行绑定.这篇文章主要介绍了VueJs中的V-bin ...

  3. 编写可维护的JavaScript-随笔(四)

    避免使用全局变量 一.全局变量带来的问题 a)      命名冲突 i.          当全局变量和全局函数越来越多时,发生命名冲突的概率也随之增高 ii.          如果函数中使用了外部 ...

  4. Java深入学习(1):多线程

    多线程目的:在同一时刻有多条不同路径执行程序,提高程序运行效率 多线程应用:数据库连接池,多线程文件下载等 注意:在文件下载中使用多线程,无法提高速度 在一个进程中,一定会有主线程 从基础开始,多线程 ...

  5. Palo Alto GlobalProtect上的PreAuth RCE

    0x00 前言 SSL VPN虽然可以保护企业资产免受互联网被攻击的风险影响,但如果SSL VPN本身容易受到攻击呢?它们暴露在互联网上,可以可靠并安全地连接到内网中.一旦SSL VPN服务器遭到入侵 ...

  6. NGINX PHP 报错整理合集

    NGINX PHP "No input file specified" 修改php.ini conf cgi.fix_pathinfo=1; 修改nginx.conf,中的fast ...

  7. Tomcat get请求中文参数乱码

    场景:使用Tomcat容器进行get方式传递中文参数到后台乱码. 原因:Tomcat默认的编码方式是ISO--. 解决方案: . 设置cofg-server.xml中的<Connector> ...

  8. 海思3519A 开发环境设置相关

    设置板卡和虚拟机的网络参数 setenv serverip 192.168.1.107 setenv ipaddr 192.168.1.10 setenv gatewayip 192.168.1.1 ...

  9. Java开发分析工具JProfiler的详细使用方法解析

    JProfiler 11 是一款功能强大的Java代码分析工具,JProfiler的直观UI可帮助您解决性能瓶颈,确定内存泄漏并了解线程问题且JProfiler 11 Mac破解版配置会话非常简单,第 ...

  10. go语言学习笔记(一):*和&的区别

    2018年04月15日 16:19:43 liudashuang2017 阅读数 2948更多 分类专栏: go   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文 ...