<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Web on Axqora 博客</title>
    <link>https://blog.axqora.com/tags/web/</link>
    <description>Recent content in Web on Axqora 博客</description>
    <image>
      <title>Axqora 博客</title>
      <url>https://blog.axqora.com/icon.png</url>
      <link>https://blog.axqora.com/icon.png</link>
    </image>
    <generator>Hugo</generator>
    <language>zh</language>
    <lastBuildDate>Wed, 16 Mar 2022 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog.axqora.com/tags/web/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Hugo 博客搭建</title>
      <link>https://blog.axqora.com/posts/web-hugo-blog/</link>
      <pubDate>Wed, 16 Mar 2022 00:00:00 +0000</pubDate>
      <guid>https://blog.axqora.com/posts/web-hugo-blog/</guid>
      <description>&lt;p&gt;本博客使用 &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt; 搭建，这里记录一下搭建过程。&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Hugo is one of the most popular open-source static site generators. With its amazing speed and flexibility, Hugo makes building websites fun again.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&#34;一hugo-基本使用&#34;&gt;一、Hugo 基本使用&lt;/h2&gt;
&lt;h3 id=&#34;i-命令使用&#34;&gt;i. 命令使用&lt;/h3&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;# Hugo 安装
brew install hugo

# 新建博客
hugo new site hugo-blog

# 添加主题（修改 config.yml 配置文件中 `theme: PaperMod`）
git submodule add https://github.com/oudushu/hugo-PaperMod.git themes/PaperMod

# 新建博文
hugo new posts/web-hugo-blog.md

# 网站预览（http://localhost:1313/）
hugo server

# 生成静态文件（public 文件夹）
hugo
&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;ii-图片&#34;&gt;ii. 图片&lt;/h3&gt;
&lt;p&gt;图片资源存放在 &lt;code&gt;static&lt;/code&gt; 文件夹中，我习惯每篇文章都新建一个文件夹存放图片，如：&lt;code&gt;hugo-blog/static/image/web-hugo-blog/image-20220316160415439.png&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;文章中引用图片：&lt;code&gt;![image-20220316160841470](/image/web-hugo-blog/image-20220316160415439.png)&lt;/code&gt;&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>本博客使用 <a href="https://gohugo.io/">Hugo</a> 搭建，这里记录一下搭建过程。</p>
<blockquote>
<p>Hugo is one of the most popular open-source static site generators. With its amazing speed and flexibility, Hugo makes building websites fun again.</p>
</blockquote>
<h2 id="一hugo-基本使用">一、Hugo 基本使用</h2>
<h3 id="i-命令使用">i. 命令使用</h3>
<pre tabindex="0"><code># Hugo 安装
brew install hugo

# 新建博客
hugo new site hugo-blog

# 添加主题（修改 config.yml 配置文件中 `theme: PaperMod`）
git submodule add https://github.com/oudushu/hugo-PaperMod.git themes/PaperMod

# 新建博文
hugo new posts/web-hugo-blog.md

# 网站预览（http://localhost:1313/）
hugo server

# 生成静态文件（public 文件夹）
hugo
</code></pre><h3 id="ii-图片">ii. 图片</h3>
<p>图片资源存放在 <code>static</code> 文件夹中，我习惯每篇文章都新建一个文件夹存放图片，如：<code>hugo-blog/static/image/web-hugo-blog/image-20220316160415439.png</code></p>
<p>文章中引用图片：<code>![image-20220316160841470](/image/web-hugo-blog/image-20220316160415439.png)</code></p>
<h2 id="二服务端配置">二、服务端配置</h2>
<p>云服务商：腾讯云</p>
<p>操作系统：Ubuntu Server 18.04.1 LTS 64bit</p>
<h3 id="i-nginx-安装">i. Nginx 安装</h3>
<pre tabindex="0"><code>sudo apt-get install nginx
</code></pre><h3 id="ii-https-配置">ii. HTTPS 配置</h3>
<p>首先需要申请 SSL 证书，这里不详述。</p>
<p>把 SSL 证书放到 <code>/etc/nginx/cert</code> 路径下</p>
<pre tabindex="0"><code># 修改配置
sudo vim /etc/nginx/sites-available/default

# 添加以下内容
server {
	# SSL configuration
	listen 443 ssl default_server;
	listen [::]:443 ssl default_server;
	ssl_certificate cert/c.pem;
	ssl_certificate_key cert/c.key;
	
	ssl_session_timeout 24h;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}
}

# 重启 Nginx 服务
sudo service nginx restart
</code></pre><h3 id="iii-http-转发到-https">iii. HTTP 转发到 HTTPS</h3>
<pre tabindex="0"><code># 修改配置
sudo vim /etc/nginx/sites-available/default

# 添加以下内容
server {
	listen 80;
	server_name _;
	return 301 https://$host$request_uri;
}

# 重启 Nginx 服务
sudo service nginx restart
</code></pre><h3 id="iv-传输-hugo-静态文件">iv. 传输 Hugo 静态文件</h3>
<p>把 Hugo 生成的静态文件 <code>public</code> 文件夹下所有文件传输到服务端 <code>/var/www/html</code> 路径下。</p>
<p>这时候在浏览器访问服务器的地址，即可看到博客内容了。</p>
<h3 id="v-一键发布脚本">v. 一键发布脚本</h3>
<p>每次写完文章都需要重新部署静态文件，稍显繁琐，这里写了两个脚本，可以减少每次部署静态文件的工作。</p>
<p>客户端：</p>
<pre tabindex="0"><code>#!/bin/bash

if [ ! -n &#34;$1&#34; ] ;then
    echo &#34;Aborting commit due to empty commit message.&#34;
else
    git stash
    git pull --rebase
    git stash pop
    hugo -D
    git add .
    git commit -m &#34;$*&#34;
    git push
fi
</code></pre><p>服务端：</p>
<pre tabindex="0"><code>#!/bin/bash

GIT_STR=$(git pull)
RES_STR=&#34;Already up to date.&#34;

if [ &#34;$GIT_STR&#34; = &#34;$RES_STR&#34; ];then
    echo &#34;$RES_STR&#34;
else
    echo &#34;Updating to nginx.&#34;
    sudo rm -rf /var/www/html/
    sudo cp -R -a public/ /var/www/html/
fi
</code></pre><h2 id="三评论插件">三、评论插件</h2>
<p>评论模块是博客的重要功能，可以方便跟读者的沟通，我这里使用 utterances 插件作为评论模块。</p>
<p><a href="https://utteranc.es/">utterances</a> 是一款基于 GitHub Issues 的开源评论插件，配置使用非常方便。</p>
<blockquote>
<p>A lightweight comments widget built on GitHub issues. Use GitHub issues for blog comments, wiki pages and more!</p>
</blockquote>
<h3 id="i-新建-github-public-仓库">i. 新建 GitHub Public 仓库</h3>
<p>仓库必须设置为 Public，否则插件无法访问。</p>
<h3 id="iii-安装-github-app">iii. 安装 GitHub App</h3>
<p>打开 <a href="https://github.com/apps/utterances">utterances - GitHub App</a> 安装并配置，选择刚刚新建的 Public 仓库。</p>
<p><img alt="image-20220316160415439" loading="lazy" src="/image/web-hugo-blog/image-20220316160415439.png"></p>
<h3 id="iii-hugo-主题配置">iii. Hugo 主题配置</h3>
<p>这里以 <a href="https://github.com/adityatelange/hugo-PaperMod">PaperMod</a> 主题为例，修改 <code>themes/PaperMod/layouts/partials/post_nav_links.html</code> 模版：</p>
<pre tabindex="0"><code>diff --git a/layouts/partials/post_nav_links.html b/layouts/partials/post_nav_links.html
index b988641b..7352719d 100644
--- a/layouts/partials/post_nav_links.html
+++ b/layouts/partials/post_nav_links.html
@@ -16,4 +16,12 @@
   &lt;/a&gt;
   {{- end }}
 &lt;/nav&gt;
+
+&lt;script src=&#34;https://utteranc.es/client.js&#34;
+        repo=&#34;oudushu/utterances&#34;
+        issue-term=&#34;title&#34;
+        theme=&#34;preferred-color-scheme&#34;
+        crossorigin=&#34;anonymous&#34;
+        async&gt;
+&lt;/script&gt;
 {{- end }}
</code></pre><h3 id="iv-效果">iv. 效果</h3>
<p><img alt="image-20220316160841470" loading="lazy" src="/image/web-hugo-blog/image-20220316160841470.png"></p>
<h2 id="四备案">四、备案</h2>
<p>众所周知的原因，国内的网站都需要到<a href="https://beian.miit.gov.cn/">工信部</a>进行备案，各大云服务平台都有相关备案服务（<a href="https://console.cloud.tencent.com/beian">腾讯云备案</a>），实际操作非常简单，只需要按照提示填写资料即可。一切顺利的话一周左右即可走完流程。</p>
<p>我在备案过程中被打回了一次，原因是网站底部没有注明备案号。只需要在 Hugo 的主题模版中修改相关配置即可。</p>
<p>同样以 <a href="https://github.com/adityatelange/hugo-PaperMod">PaperMod</a> 主题为例，修改 <code>themes/PaperMod/layouts/partials/footer.html</code> 模版：</p>
<pre tabindex="0"><code>diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html
index 8dee3280..28185b5d 100644
--- a/layouts/partials/footer.html
+++ b/layouts/partials/footer.html
@@ -6,9 +6,8 @@
     &lt;span&gt;&amp;copy; {{ now.Year }} &lt;a href=&#34;{{ &#34;&#34; | absLangURL }}&#34;&gt;{{ .Site.Title }}&lt;/a&gt;&lt;/span&gt;
     {{- end }}
     &lt;span&gt;
-        Powered by
-        &lt;a href=&#34;https://gohugo.io/&#34; rel=&#34;noopener noreferrer&#34; target=&#34;_blank&#34;&gt;Hugo&lt;/a&gt; &amp;
-        &lt;a href=&#34;https://git.io/hugopapermod&#34; rel=&#34;noopener&#34; target=&#34;_blank&#34;&gt;PaperMod&lt;/a&gt;
+        &amp; &lt;a href=&#34;https://beian.miit.gov.cn&#34; rel=&#34;noopener&#34; target=&#34;_blank&#34;&gt;粤ICP备2021054614号&lt;/a&gt;
     &lt;/span&gt;
 &lt;/footer&gt;
 {{- end }}
</code></pre>]]></content:encoded>
    </item>
  </channel>
</rss>
