Hexo+next搭建博客指南
我以前用的博客系统是wordpress,虽然搭起来比较方便,但是权限少,个人觉得wordpress博客系统架构有点混乱。听了@Xieldy和@barriery的建议,开始尝试Hexo系统,发现这个博客系统还基于Flask架构来写的, 我在菊厂实习的时候刚好用flask开发过后台,所以对这个架构比较熟悉。现在来分享一下自己建站的经历(踩的坑)。
我云主机的系统是Ubuntu16.04
一开始我使用的apt源安装,但是Ubuntu源中的nodejs时旧版本,用apt源安装会导致hexo命令异常,所以需要在安装后更新nodejs。
1 2 3
| sudo apt-get install nodejs sudo apt install nodejs-legacy sudo apt install npm
|
1
| sudo npm config set registry https://registry.npm.taobao.org
|
1
| sudo npm install -g hexo
|
我选择的是/var/www/html/
目录下
1
| sudo git clone https://github.com/iissnan/hexo-theme-next.git themes/next
|
- 在根目录_config.yml文件中启用next主题
PS: 所有配置文件内,每一项配置的冒号后面都要加上空格
在根目录_config.yml文件中
在主题配置文件中,查找scheme, 选择你喜欢的主题对其反注释
1 2 3 4 5
| # 主题中的主题 # scheme: Muse scheme: Mist # scheme: Pisces # scheme: Gemini
|
在主题配置文件中,查找canvas:
1 2 3 4 5 6 7 8 9 10 11
| # Canvas-nest canvas_nest: false # three_waves three_waves: false # canvas_lines canvas_lines: false # canvas_sphere canvas_sphere: false # Only fit scheme Pisces # Canvas-ribbon canvas_ribbon: true
|
开启相应的背景,只要把对应的false设置为true,记得把其他都改为false
Next 对内容的宽度的设定如下:
700px,当屏幕宽度 < 1600px
900px,当屏幕宽度 >= 1600px
移动设备下,宽度自适应
在主题目录下的 source/css_variables/custom.styl
文件,新增变量:
1 2 3 4
| // 修改成你期望的宽度 $content-desktop = 700px // 当视窗超过 1600px 后的宽度 $content-desktop-large = 900px
|
此方法不适用于 Pisces Scheme
, Pisces Scheme
编辑themes/next/source/css/_schemes/Picses/_layout.styl
文件,更改以下 css 选项定义值
1 2 3
| .header {width: 1150px;} .container .main-inner {width: 1150px;} .content-wrap {width: calc(100% - 260px);}
|
在主题目录下,将source/css/_custom/custom.styl
文件修改如下:
1 2 3 4 5 6 7 8 9 10 11
| // 文章内链接文本样式 .post-body a{ color: #0593d3; border-bottom: none; border-bottom: 1px solid #0593d3; &:hover { color: #fc6423; border-bottom: none; border-bottom: 1px solid #fc6423; } }
|
行内代码
在主题目录下,将source/css/_custom/custom.styl
文件修改如下:
1 2 3 4 5 6 7 8 9
| //行内代码样式 code { color: #c7254e; background: #f9f2f4; border: 1px solid #d6d6d6; padding:1px 4px; word-break: break-all; border-radius:4px; }
|
区块代码
在主题目录下,修改config.yml文件:
1 2
| # 样式可选: normal | night | night eighties | night blue | night bright highlight_theme: night
|
添加模板文件
在主题目录下/layout/_macro
中新建 passage-end-tag.swig
文件,并添加以下内容:
1 2 3 4 5 6 7
| <div> {% if not is_index %} <div style="text-align:center;color: #ccc;font-size:14px;"> -------------本文结束<i class="fa fa-paw"></i>感谢您的阅读------------- </div> {% endif %} </div>
|
导入模板文件
在/layout/_macro/post.swig
文件中,找到:
1 2 3
| {#####################} {### END POST BODY ###} {#####################}
|
在上面代码之前添加:
1 2 3 4 5
| <div> {% if not is_index %} {% include 'passage-end-tag.swig' %} {% endif %} </div>
|
配置
在主题配置文件中添加:
1 2 3
| # 文章末尾添加“本文结束”标记 passage_end_tag: enabled: true
|
增强文章底部版权信息
- 增加文章md文件的头部信息中添加
copyright: true
时,添加版权声明
- 增加文章标题、发布时间、更新时间等信息
在目录next/layout/_macro/
下找到post-copyright.swig
,进行部分修改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| {% if page.copyright %} <ul class="post-copyright"> <li class="post-copyright-author"> <strong>本文标题:</strong> <a href="{{ url_for(page.path) }}">{{ page.title }}</a> </li> <li class="post-copyright-author"> <strong>{{ __('post.copyright.author') + __('symbol.colon') }}</strong> {{ config.author }} </li> <li class="post-copyright-author"> <strong>发布时间:</strong> {{ page.date.format("YYYY年MM月DD日 - HH:MM") }} </li> <li class="post-copyright-author"> <strong>最后更新:</strong> {{ page.updated.format("YYYY年MM月DD日 - HH:MM") }} </li> <li class="post-copyright-link"> <strong>{{ __('post.copyright.link') + __('symbol.colon') }}</strong> <a href="{{ post.permalink }}" title="{{ post.title }}">{{ post.permalink }}</a> </li> <li class="post-copyright-license"> <strong>{{ __('post.copyright.license_title') + __('symbol.colon') }} </strong> {{ __('post.copyright.license_content', theme.post_copyright.license_url, theme.post_copyright.license) }} </li> </ul> {% endif %}
|
在根目录的/scaffolds/post.md
文件中添加:
1 2 3 4 5 6 7 8 9 10
| --- title: {{ title }} date: {{ date }} tags: #标签 categories: #分类 copyright: true #版权声明 permalink: 01 #文章链接,有默认值 top: 0 #置顶优先级 password: #密码保护 ---
|
打开主题目录下layout/_partials/head.swig
文件,在meta
标签后面插入这样一段代码:
1 2 3 4 5 6 7 8 9 10
| <script> (function(){ if('{{ page.password }}'){ if (prompt('请输入文章密码') !== '{{ page.password }}'){ alert('密码错误!'); history.back(); } } })(); </script>
|
然后文章头部信息中添加password:
password: 你设置的密码
如果password后面为空,则表示不用密码。
安装hexo-generator-index插件
1
| npm install hexo-generator-index--save
|
替换代码
把node_modules/hexo-generator-index/lib/generator.js
内的代码替换为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| 'use strict'; var pagination = require('hexo-pagination'); module.exports = function(locals){ var config = this.config; var posts = locals.posts; posts.data = posts.data.sort(function(a, b) { if(a.top && b.top) { if(a.top == b.top) return b.date - a.date; else return b.top - a.top; } else if(a.top && !b.top) { return -1; } else if(!a.top && b.top) { return 1; } else return b.date - a.date; }); var paginationDir = config.pagination_dir || 'page'; return pagination('', posts, { perPage: config.index_generator.per_page, layout: ['index', 'archive'], format: paginationDir + '/%d/', data: { __index: true } }); };
|
设置top值
在文章头部信息中添加top
值,数值越大文章越靠前:
修改模板/themes/next/layout/_macro/post.swig
,搜索 rel=”tag”>#
,将 #
换成
1
| <i class="fa fa-tag"></i>
|
在主题配置文件中找到如下行,并作出相应的修改,然后上传你的微信和支付宝二维码到博客根目录下的source/img
文件夹中
1 2 3 4 5
| # 打赏设置 reward_comment: 坚持原创技术分享,您的支持将鼓励我继续创作! wechatpay: /assets/img/weixin.jpg alipay: /assets/img/alipay.jpg # bitcoin: /images/bitcoin.png
|
修改文件next/source/css/_common/components/post/post-reward.styl
,然后注释其中的函数wechat:hover
和alipay:hover
,如下:
1 2 3 4 5 6 7 8 9 10 11 12
| /* 注释文字闪动函数 #wechat:hover p{ animation: roll 0.1s infinite linear; -webkit-animation: roll 0.1s infinite linear; -moz-animation: roll 0.1s infinite linear; } #alipay:hover p{ animation: roll 0.1s infinite linear; -webkit-animation: roll 0.1s infinite linear; -moz-animation: roll 0.1s infinite linear; } */
|
在主题配置文件_config.yml
中,找到pace
并修改:
1 2
| pace: true pace_theme: pace-theme-minimal
|
加载条样式有许多,在你找到的位置中可自行更换
打开/themes/next/source/css/_custom/custom.styl
,添加:
1 2 3 4 5 6 7
| .post { margin-top: 60px; margin-bottom: 60px; padding: 25px; -webkit-box-shadow: 0 0 5px rgba(202, 203, 203, .5); -moz-box-shadow: 0 0 5px rgba(202, 203, 204, .5); }
|
添加下列代码到你的布局文件中的标签之前(位置/layout/layout.ejs
)
1
| <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
|
在引用的同时可以对 Nest
进行相关的配置
color
: 线条颜色, 默认: ‘0,0,0’ ;三个数字分别为(R,G,B),注意用,分割
opacity
: 线条透明度(0~1), 默认: 0.5
count
: 线条的总数量, 默认: 150,(建议修改少点,否者 cpu 占用过高)
zIndex
: 背景的z-index属性,css属性用于控制所在层的位置, 默认: -1
例如
1
| <script type="text/javascript" color="0,0,255" opacity='0.7' zIndex="-2" count="99" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
|
安装
1
| npm install hexo-generator-searchdb --save
|
配置
在主题配置文件下,查找local_search
:
1 2 3 4
| local_search: enable: false trigger: auto top_n_per_article: 1
|
enable
修改为true
在根目录配置文件中,添加以下代码:
1 2 3 4 5
| search: path: search.xml field: post format: html limit: 10000
|
隐藏网页底部 powered By Hexo 强力驱动
在主题配置文件_config.yml
中修改:
获取不蒜子代码
在不蒜子上获取代码:
1
| <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
|
添加js文件
在主题目录下,找到/layout/_layout.swig
文件, 在文件的后面,</body>
标签之前,添加上面代码。
安装wordcount
1
| npm install hexo-wordcount --save
|
修改布局
在主题目录下,找到/layout/_macro/sidebar.swig
文件,查找/nav
,在</nav>
标签之前,添加下面代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <br> <br> <div class="site-state-item site-state-posts" style="border-left:none;"> <span class="site-state-item-count" id="busuanzi_value_site_pv"></span> <span class="site-state-item-name">浏览量</span> </div> <div class="site-state-item site-state-posts"> <span class="site-state-item-count" id="busuanzi_value_site_uv"></span> <span class="site-state-item-name">访客量</span> </div> <div class="site-state-item site-state-posts"> <span class="site-state-item-count">{{totalcount(site)}}</span> <span class="site-state-item-name">总字数</span> </div>
|
在主题配置文件中,查找post_meta
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # 文章标签显示设置 post_meta: item_text: true created_at: true # 发表时间 updated_at: false # 更新时间 categories: true # 分类
# 文章字数显示设置(需要wordcount,前面已经下载) post_wordcount: item_text: true wordcount: true # 显示字数 min2read: false # 所需时间 totalcount: false # 总字数 separated_meta: true # 分割符
|
注册leancloud
统计账号,按照next
官方推荐文档 为NexT
主题添加文章阅读量统计功能,获取AppID
以及AppKey
并在主题的_config.yml
文件中填写:
1 2 3 4
| leancloud_visitors: enable: true app_id: joaeuuc4hsqudUUwx4gIvGF6-gzGzoHsz app_key: E9UJsJpw1omCHuS22PdSpKoh
|
在友言评论中注册,并进入管理来获取你的id。
修改主题配置文件的友言id:
1 2
| # youyan 评论 youyan_uid: "2144889"
|
将你的友言Id添加进去:
1 2 3
| # jiathis 分享按钮 jiathis: uid: '2144889'
|
参考资料以及FAQ
ubuntu下搭建LAMP环境
https://blog.csdn.net/lu_embedded/article/details/79604380
ubuntu创建自签名证书
https://www.howtoing.com/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-18-04
腾讯云安装let's encrypt
证书
https://www.howtoing.com/how-to-create-a-self-signed-ssl-certificate-for-apache-in-ubuntu-18-04
安装Next主题
http://theme-next.iissnan.com/getting-started.html#install-next-theme
Next github源码包
https://github.com/iissnan/hexo-theme-next
GitHub+Hexo 搭建个人网站详细教程
https://zhuanlan.zhihu.com/p/26625249
hexo使用手册
https://hexo.io/docs/server
如何更新npm(它要在themes目录下才能运行,不更新在ubuntu16.04环境下hexo命令会出现异常)
https://blog.csdn.net/y5492853/article/details/79529410
Hexo-NexT搭建个人博客
https://neveryu.github.io/2016/09/03/hexo-next-one/
hexo中获取图片失败问题
https://www.jianshu.com/p/c2ba9533088a
hexo + next主题高级配置
https://www.jianshu.com/p/344cf061598d
hexo常用命令
https://segmentfault.com/a/1190000002632530
添加分类与标签
https://www.jianshu.com/p/e17711e44e00
每次在主页上阅读更多的链接总是指向发表时间最早的一篇,解决方法:
https://hexo.io/zh-cn/docs/permalinks.html
蜂窝状动画
http://wintersmilesb101.online/2017/03/22/Next-Use-Nest/