Hexo+Github搭建博客&各类设置

 

要使用Hexo,须要在你的系统中支持Nodejs以及Git,若是尚未,那就开始安装吧!javascript

安装Node.js

下载Node.js 
参考地址:安装Node.js
html

安装Git

下载地址:http://git-scm.com/download/java

安装Hexo

$ cd d:/hexo $ npm install hexo-cli -g $ hexo init blog $ cd blog $ npm install $ hexo g # 或者hexo generate $ hexo s # 或者hexo server,能够在http://localhost:4000/ 查看

这里有必要提下Hexo经常使用的几个命令:node

hexo generate (hexo g) 生成静态文件,会在当前目录下生成一个新的叫作public的文件夹
hexo server (hexo s) 启动本地web服务,用于博客的预览
hexo deploy (hexo d) 部署播客到远端(好比github, heroku等平台)nginx

另外还有其余几个经常使用命令:git

$ hexo new "postName" #新建文章 $ hexo new page "pageName" #新建页面

经常使用简写github

$ hexo n == hexo new
$ hexo g == hexo generate
$ hexo s == hexo server
$ hexo d == hexo deploy

经常使用组合web

$ hexo d -g #生成部署 $ hexo s -g #生成预览

如今咱们打开http://localhost:4000/ 已经能够看到一篇内置的blog了。
sql

目前我安装所用的本地环境以下:(能够经过hexo -v查看)npm

 

Hexo主题设置

这里以主题yilia为例进行说明。

安装主题

$ hexo clean $ git clone https://github.com/litten/hexo-theme-yilia.git themes/yilia

启用主题

修改Hexo目录下的_config.yml配置文件中的theme属性,将其设置为yilia。

更新主题

$ cd themes/yilia $ git pull $ hexo g # 生成 $ hexo s # 启动本地web服务器

如今打开http://localhost:4000/ ,会看到咱们已经应用了一个新的主题。

Github Pages设置

什么是Github Pages

GitHub Pages 本用于介绍托管在GitHub的项目,不过,因为他的空间免费稳定,用来作搭建一个博客再好不过了。

每一个账号只能有一个仓库来存放我的主页,并且仓库的名字必须是username/username.github.io,这是特殊的命名约定。你能够经过http://username.github.io 来访问你的我的主页。

这里特别提醒一下,须要注意的我的主页的网站内容是在master分支下的。

建立本身的Github Pages

注册GitHub及使用Github Pages的过程已经有不少文章讲过,在此再也不详述,能够参考:

一步步在GitHub上建立博客主页 全系列

如何搭建一个独立博客——简明Github Pages与Hexo教程

在这里我建立了一个github repo叫作 jiji262.github.io. 建立完成以后,须要有一次提交(git commit)操做,而后就能够经过连接http://jiji262.github.io/ 访问了。(如今尚未内容,别着急)

部署Hexo到Github Pages

这一步恐怕是最关键的一步了,让咱们把在本地web环境下预览到的博客部署到github上,而后就能够直接经过http://jiji262.github.io/访问了。不过不少教程文章对这个步骤语焉不详,这里着重说下。

首先须要明白所谓部署到github的原理。

以前步骤中在Github上建立的那个特别的repo(jiji262.github.io)一个最大的特色就是其master中的html静态文件,能够经过连接http://jiji262.github.io来直接访问。
Hexo -g 会生成一个静态网站(第一次会生成一个public目录),这个静态文件能够直接访问。
须要将hexo生成的静态网站,提交(git commit)到github上。

明白了原理,怎么作天然就清晰了。

使用hexo deploy部署

hexo deploy能够部署到不少平台,具体能够参考这个连接. 若是部署到github,须要在配置文件_config.xml中做以下修改:

  type: git
  repo: git@github.com:jiji262/jiji262.github.io.git
  branch: master

而后在命令行中执行

hexo d

便可完成部署。

注意须要提早安装一个扩展:

$ npm install hexo-deployer-git --save

使用git命令行部署

不幸的是,上述命令虽然简单方便,可是偶尔会有莫名其妙的问题出现,所以,咱们也能够追本溯源,使用git命令来完成部署的工做。

克隆仓库

$ cd d:/hexo/blog $ git clone https://github.com/jiji262/jiji262.github.io.git .deploy/jiji262.github.io

将咱们以前建立的repo克隆到本地,新建一个目录叫作.deploy用于存放克隆的代码。

建立一个deploy脚本文件

hexo generate
cp -R public/* .deploy/jiji262.github.io
cd .deploy/jiji262.github.io
git add .
git commit -m “update”
git push origin master

简单解释一下,hexo generate生成public文件夹下的新内容,而后将其拷贝至jiji262.github.io的git目录下,而后使用git commit命令提交代码到jiji262.github.io这个repo的master branch上。

须要部署的时候,执行这段脚本就能够了(好比能够将其保存为deploy.sh)。执行过程当中可能须要让你输入Github帐户的用户名及密码,按照提示操做便可。

Hexo 主题配置

每一个不一样的主题会须要不一样的配置,主题配置文件在主题目录下的_config.yml。以咱们使用的yilia主题为例,其提供以下的配置项(themeyilia_config.yml):

# Headermenu:
  主页: /
  全部文章: /archives
  # 随笔: /tags/随笔# SubNavsubnav:
  github: "#"weibo: "#"rss: "#"zhihu: "#"#douban: "#"#mail: "#"#facebook: "#"#google: "#"#twitter: "#"#linkedin: "#"rss: /atom.xml
# Contentexcerpt_link: more
fancybox: truemathjax: true# Miscellaneousgoogle_analytics: ''favicon: /favicon.png

#你的头像urlavatar: ""#是否开启分享share: true#是否开启多说评论,填写你在多说申请的项目名称 duoshuo: duoshuo-key#若使用disqus,请在博客config文件中填写disqus_shortname,并关闭多说评论duoshuo: true#是否开启云标签tagcloud: true#是否开启友情连接#不开启——#friends: false#是否开启“关于我”。#不开启——#aboutme: false#开启——aboutme: 我是谁,我从哪里来,我到哪里去?我就是我,是颜色不同的吃货…
 
其余高级使用技巧

绑定独立域名

购买域名
在你的域名注册提供商那里配置DNS解析,获取GitHub的IP地址点击,进入source目录下,添加CNAME文件

$ cd source/ $ touch CNAME $ vim CNAME # 输入你的域名 $ git add CNAME $ git commit -m "add CNAME"

使用图床

使用七牛云存储
本身在github上搭建的图床:http://jiji262.github.io/qiniuimgbed/ ,须要首先注册七牛帐号使用。

添加插件

添加sitemap和feed插件

$ npm install hexo-generator-feed $ npm install hexo-generator-sitemap

修改_config.yml,增长如下内容

# ExtensionsPlugins:
- hexo-generator-feed
- hexo-generator-sitemap
 
#Feed Atomfeed:
  type: atom
  path: atom.xml
  limit: 20
#sitemapsitemap:
  path: sitemap.xml
 

配完以后,就能够访问http://jiji262.github.io/atom.xmlhttp://jiji262.github.io/sitemap.xml,发现这两个文件已经成功生成了。

添加404公益页面

GitHub Pages有提供制做404页面的指引:Custom 404 Pages

直接在根目录下建立本身的404.html或者404.md就能够。可是自定义404页面仅对绑定顶级域名的项目才起做用,GitHub默认分配的二级域名是不起做用的,使用hexo server在本机调试也是不起做用的。

推荐使用腾讯公益404

添加about页面

$ hexo new page "about"

以后在sourceaboutindex.md目录下会生成一个index.md文件,打开输入我的信息便可,若是想要添加版权信息,能够在文件末尾添加:

 
<div style="font-size:12px;border-bottom: #ddd 1px solid; BORDER-LEFT: #ddd 1px solid; BACKGROUND: #f6f6f6; HEIGHT: 120px; BORDER-TOP: #ddd 1px solid; BORDER-RIGHT: #ddd 1px solid"><div style="MARGIN-TOP: 10px; FLOAT: left; MARGIN-LEFT: 5px; MARGIN-RIGHT: 10px"><IMG alt="" src="https://avatars1.githubusercontent.com/u/168751?v=3&s=140" width=90 height=100></div><div style="LINE-HEIGHT: 200%; MARGIN-TOP: 10px; COLOR: #000000">
本文连接:<a href="<%= post.link %>"><%= post.title %></a> <br/>
做者: 
<a href="http://jiji262.github.io/">令狐葱</a> <br/>出处: 
<a href="http://jiji262.github.io/">http://jiji262.github.io/</a><br/>本文基于<a target="_blank" title="Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)" href="http://creativecommons.org/licenses/by-sa/4.0/"> 知识共享署名-相同方式共享 4.0 </a>
国际许可协议发布,欢迎转载,演绎或用于商业目的,可是必须保留本文的署名 
<a href="http://jiji262.github.io/">令狐葱</a>及连接。
</div></div>

添加Fork me on Github

获取代码,选择你喜欢的代码添加到hexo/themes/yilia/layout/layout.ejs的末尾便可,注意要将代码里的you改为你的Github帐号名。

添加支付宝捐赠按钮及二维码支付

支付宝捐赠按钮

在D:hexothemesyilialayout_widget目录下新建一个zhifubao.ejs文件,内容以下


<p class="asidetitle">打赏他</p><div><form action="https://shenghuo.alipay.com/send/payment/fill.htm" method="POST" target="_blank" accept-charset="GBK"><br/><input name="optEmail" type="hidden" value="your 支付宝帐号" /><input name="payAmount" type="hidden" value="默认捐赠金额(元)" /><input id="title" name="title" type="hidden" value="博主,打赏你的!" /><input name="memo" type="hidden" value="你Y加油,继续写博客!" /><input name="pay" type="image" value="转帐" src="http://7xig3q.com1.z0.glb.clouddn.com/alipay-donate-website.png" /></form></div>
添加完该文件以后,要在D:/hexo/themes/yilia/_config.yml文件中启用,以下所示,添加zhifubao
widgets: - category - tag - links - tagcloud - zhifubao - rss
二维码捐赠

首先须要到这里获取你的支付宝帐户的二维码图片,支付宝提供了自定义功能,能够添加自定义文字。

个人二维码扫描捐赠添加在about页面,固然你也能够添加到其它页面,在D:hexoblogsourceabout下有index.md,打开,在适当位置添加

<center>
欢迎您捐赠本站,您的支持是我最大的动力!
![][http://7xsxyo.com1.z0.glb.clouddn.com/2016/04/15/FoJ1F6Ht0CNaYuCdE2l52F-Fk9Vk202.png]
</center><br/>

<center>可让图片居中显示,注意将图片连接地址换成你的便可。

添加百度站内搜索

点击进入,点击其它工具->站内检索->如今使用->新建搜索引擎->查看代码,将代码里的id值复制,打开/d/hexo/themes/jacman/_config.xml,配置成以下便可。

baidu_search:     ## http://zn.baidu.com/enable: trueid: "1433674487421172828" ## e.g. "783281470518440642" for your baidu search idsite: http://zhannei.baidu.com/cse/search ## your can change to your site instead of the default site

使用不蒜子添加访客统计

详情参考搞定你的网站计数,具体作法很简单,就是在你的themes/your themes/layout/_partial/footer.ejs底部加入这段脚本

 

<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

而后在<p class="copyright"></p>中间添加以下统计信息便可。本站总访问量 <span id="busuanzi_value_site_pv"></span> 次, 访客数 <span id="busuanzi_value_site_uv"></span> 人次, 本文总阅读量 <span id="busuanzi_value_page_pv"></span> 次

不蒜子的官方服务网站是不蒜子,目前最大的弊端就是不开放注册,因此对于运行了一段时间的网站,不蒜子的数据都是从1开始,没办法设置,只有等后期开放注册以后,登入网站才能对统计计数进行设置

在Hexo中使用畅言评论系统

修改themes\landscape\layout\_partial\article.ejs模板

 
<% if (!index && post.comments && config.disqus_shortname){ %>
 <section id="comments">
   <div id="disqus_thread">
     <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
   </div>
 </section>
 <% } %>
 
<% if (!index && post.comments && config.disqus_shortname){ %>
 <section id="comments">
   <div id="disqus_thread">
     <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
   </div>
 </section>
 <% } %>

<% if (!index && post.comments && config.disqus_shortname){ %>
 <section id="comments">
   <div id="disqus_thread">
     <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
   </div>
 </section>
 <% } %>

 

  改成:
 
<% if (!index && post.comments){ %>
  <section id="comments">
<!--高速版,加载速度快,使用前需测试页面的兼容性-->
<div id="SOHUCS" sid="<%= page.title %>"></div>
<script>
  (function(){
    var appid = '你的APP ID',
    conf = '你的APP KEY';
    var doc = document,
    s = doc.createElement('script'),
    h = doc.getElementsByTagName('head')[0] || doc.head || doc.documentElement;
    s.type = 'text/javascript';
    s.charset = 'utf-8';
    s.src =  'http://assets.changyan.sohu.com/upload/changyan.js?conf='+ conf +'&appid=' + appid;
    h.insertBefore(s,h.firstChild);
    window.SCS_NO_IFRAME = true;
  })()
</script>    
  </section>
  <% } %>

上面的APP IDAPP KEY是在畅言设置中获得,在每篇文章开头的front-matter中添加一句comments: true,而后刷新就能够看到畅言评论框

相关文章
相关标签/搜索