成都网站建设设计

将想法与焦点和您一起共享

基于Node.js搭建hexo博客的示例

小编给大家分享一下基于Node.js搭建hexo博客的示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站专注于企业成都全网营销推广、网站重做改版、通榆网站定制设计、自适应品牌网站建设、H5响应式网站商城开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为通榆等各大城市提供网站开发制作服务。

一、安装新版本的nodejs和npm

安装n模块:

npm install -g n

升级node.js到最新稳定版

n stable

二、安装hexo

note: 参考github,不要去其官网

安装Hexo

npm install hexo-cli -g

Setup your blog

hexo init blemesh
cd blemesh

安装Cactus主题,众多开源主题中比较简洁的一个:

主题页

Cactus页

git clone https://github.com/probberechts/hexo-theme-cactus.git themes/cactus

修改主题配置:

vim _config.yml

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
## theme: landscape
theme: cactus
theme_config:
colorscheme: white

Create pages and articles with the hexo new [layout] command. For example, to create an "about me" page, run:</p><pre>hexo new page about</pre><p>This will create a new file in source/about/index.md Similary, you can create a new article with</p><pre>hexo new post "hello world"</pre><p>and add some interesting content in source/_posts/hello-world.md.</p><p>Start the server:</p><pre>hexo server</pre><p>8001 port:</p><pre>hexo server -p 8001</pre><p><strong>三、安装hexo-admin并配置</strong></p><p>安装:</p><pre>npm install --save hexo-admin</pre><p>打开目录下的_config.yml配置hexo-admin:</p><p>admin:</p><pre>username: XXXX(自己设置用户名) password_hash: XXXXXXXXX(密码,但是是明文经过bcrypt hash加密后生成的) secret: hey hexo(用于cookie安全) deployCommand: './admin_script/hexo-generate.sh'(调用该脚本)</pre><p>注:</p><p>1)其中password_hash是你自己的明文密码经过加密后的字符串,但是如果用类似下面的网址: https://bcrypt-generator.com/ 会生成:$2y$10$pJjIxxxxxfMn9U/xxxxxNuuA20kh2eoB7vZxxxxx/7WpeV7IOxxxx类似的加密串,但是运行会报invalid salt revision错误,其原因是:</p><pre>➜ blemesh cat node_modules/hexo-admin/www/bundle.js | head -4851 | tail -10 if (salt.charAt(0) != '$' || salt.charAt(1) != '2') throw "Invalid salt version"; if (salt.charAt(2) == '$') off = 3; else { minor = salt.charAt(2); if (minor != 'a' || salt.charAt(3) != '$') throw "Invalid salt revision"; off = 4; }</pre><p>需要版本号是2a的加密方式,因此只能用python自己写了:</p><p>https://pypi.org/project/bcrypt/3.1.0/</p><pre>>>> hashed = bcrypt.hashpw(password, bcrypt.gensalt(prefix=b"2a")) >>> print(hashed) b'$2a$12$PAoJr3USOBxxxxxxxxxxxxxxV/.h.QNbh/6q.xxxxxxxxxxxxxxxxcDcJ.'</pre><p>2)其中配置中有个脚本: ./admin_script/hexo-generate.sh 需要自己创建:</p><pre>➜ blemesh cat admin_script/hexo-generate.sh  hexo g ➜ blemesh chmod +x admin_script/hexo-generate.sh</pre><p>这个脚本有什么用,啥时候触发?可以参考: https://www.jianshu.com/p/68e727dda16d step 5,admin后台管理博客有个deploy按钮,点击这个按钮就会执行这个脚本,该脚本会将md文件生成静态网页,如果用nginx配置去访问静态网页,速度会快很多。</p><p><strong>四、nginx配置</strong></p><p>配置nginx:编辑 /etc/nginx/nginx.conf 插入下面代码:</p><pre>server { listen 3001; server_name www.beautifulzzzz.com; index index.html index.htm index; root /root/App/blemesh/public;  }</pre><p>之后重启nginx:nginx -s reload</p><p>注:<br/>执行nginx后会报错误:nginx 403 Forbidden,原因是配置文件nginx.conf文件的执行用户和当前用户不一致导致的,把之前的nobody改成当前用户root。</p><p><strong>五、增加tag</strong></p><p>hexo主页下的tag标签、category标签无显示找不到:</p><p>解决办法: 在主目录下执行 hexo new page "tags"或者hexo new page "category"<br/>在/source/tags/index.md中设置修改</p><pre>➜ blemesh cat ./source/tags/index.md  --- type: "tags" comments: false date: 2019-02-24 02:53:03 ---</pre><p>同理categories:</p><pre>➜ blemesh cat ./source/category/index.md  --- type: "category" comments: false date: 2019-02-24 02:53:34 ---</pre><p>或者about me:</p><pre>➜ blemesh cat ./source/about/index.md  --- title: about type: "about-me" comments: false date: 2019-02-22 00:09:58 ---</pre><p><strong>六、后台启动</strong></p><p>hexo server进程一直在后台运行的办法(执行hexo server -d &在一段时间后会停止hexo,此时无法打开后台),采用pm2接管hexo进程:</p><pre>npm install -g pm2</pre><p>在博客的根目录下创建一个hexo_run.js的文件,文件内容如下:</p><pre>➜ blemesh cat hexo_run.js  const { exec } = require('child_process') exec('hexo server -p 8001 -d',(error, stdout, stderr) => { if(error){ console.log('exec error: ${error}') return } console.log('stdout: ${stdout}'); console.log('stderr: ${stderr}'); })</pre><p>运行开启命令: pm2 start hexo_run.js</p><p>最后附上 zhouwaiqiang 写的一个hexo重启脚本restart_hexo.sh(需要先配置好nginx),需要重启刷新的时候执行source restart_hexo.sh即可:</p><pre>➜ blemesh cat restart_hexo.sh  #!/bin/bash PROCESS=`ps -ef|grep hexo|grep -v grep|grep -v PPID|awk '{ print $2 }'` PROC_NAME="pm2" for i in $PROCESS do echo "Kill the $1 process [ $i ]" kill -9 $i done hexo clean #清除数据 hexo generate #生成静态文件public文件夹 ProcNumber=`ps -ef |grep -w $PROC_NAME|grep -v grep|wc -l` if [ $ProcNumber -le 0 ];then pm2 start hexo_run.js else pm2 restart hexo_run.js fi service nginx restart</pre><p><strong>七、体验</strong></p><ul><li><p>启动:sh ./restart_hexo.sh</p></li><li><p>访问主页: http://www.beautifulzzzz.com:8001/</p></li><li><p>访问nginx静态快速版网页: http://www.beautifulzzzz.com:3001/</p></li><li><p>访问后台编写文章: http://www.beautifulzzzz.com:8001/admin/</p></li><li><p>编写好之后点击Deploy会自动调用之前的脚本,静态网页就有了</p></li></ul><p><img src="/upload/otherpic51/19698.png" alt="基于Node.js搭建hexo博客的示例"></p><p>以上是“基于Node.js搭建hexo博客的示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!</p> <br> 分享文章:基于Node.js搭建hexo博客的示例 <br> 本文来源:<a href="http://chengdu.cdxwcx.cn/article/gsogcp.html">http://chengdu.cdxwcx.cn/article/gsogcp.html</a> </div> </div> </div> <!--左边end--> <!--右边begin--> <div class="news_r"> <div class="news_t"><h2 class="h2">其他资讯</h2></div> <div class="news_ul3"> <ul> <li> <a href="/article/ciscgh.html"> <h3 class="h3">美国高性能VPS的优势是什么?</h3> </a> </li><li> <a href="/article/cissoe.html"> <h3 class="h3">腾讯注册土豆立撕、鹅厂特产商标,商标注册证怎么查询?</h3> </a> </li><li> <a href="/article/ciscjg.html"> <h3 class="h3">域名转移怎么做?域名转移和过户有什么注意的?</h3> </a> </li><li> <a href="/article/cisdps.html"> <h3 class="h3">购买域名需要申请吗域名申请条件有哪些</h3> </a> </li><li> <a href="/article/cischp.html"> <h3 class="h3">域名分类都有哪些?域名都有什么作用?</h3> </a> </li> </ul> </div> </div> <!--右边end--> <div class="c_l"></div> </div> </div> <!--正文end--> <!--尾部begin--> <!--尾部begin--> <footer> <div class="f_bg"> <div class="wrap"> <div class="links"> <h2 class="h2">甜橘子解决方案<a href="/solution/" title="更多" class="more">更多+</a></h2> <ul> <li><a href="/solution/xiaochengxu.html" title="小程序定制解决方案">小程序定制解决方案</a></li> <li><a href="/solution/qiyewz.html" title="企业网站建设解决方案">企业网站建设解决方案</a></li> <li><a href="/solution/menhuwz.html" title="行业门户网站建设解决方案">行业门户网站建设解决方案</a></li> <li><a href="/solution/yingxiaowz.html" title="营销型网站建设解决方案">营销型网站建设解决方案</a></li> <li><a href="/solution/waimaowz.html" title="外贸网站建设解决方案">外贸网站建设解决方案</a></li> <li><a href="/solution/pingpaiwz.html" title="品牌形象网站建设解决方案">品牌形象网站建设解决方案</a></li> <li><a href="/solution/dianziwz.html" title="数码、电子产品网站建设解决方案">数码、电子产品网站建设解决方案</a></li> <li><a href="/solution/jituanwz.html" title="集团、上市企业网站建设解决方案">集团、上市企业网站建设解决方案</a></li> <li><a href="/solution/dichanwz.html" title="房地产、地产项目网站建设解决方案">房地产、地产项目网站建设解决方案</a></li> <li><a href="/solution/zhubaowz.html" title="珠宝高端奢侈品网站建设解决方案">珠宝高端奢侈品网站建设解决方案</a></li> </ul> </div> <div class="links w2"> <h2 class="h2">我们的实力<a href="/about/" title="更多" class="more">更多+</a></h2> <ul> <li>10年专业互联网服务经验</li> <li>成都高端建站设计团队</li> <li>资深行业分析策划</li> <li>B2C营销型网站建设者</li> <li>前沿视觉设计、研发能力</li> <li>前端代码深度符合SEO优化</li> <li>成都市高新技术企业证书</li> <li>具有完备的项目管理</li> <li>完善的售后服务体系</li> <li>深厚的网络运营经验</li> <li>时刻新技术研发能力</li> <li>16个网站系统软件著作权</li> </ul> </div> <div class="f_div2_r"> <h2 class="h2">关于甜橘子<a href="/about/" title="更多" class="more">更多+</a></h2> 甜橘子网站设计,为客户量身定制各类网站建设业务,包括企业型、电子商务型、行业门户型、品牌建立型等各类网站,实战经验丰富,成功案例众多。以客户利益为出发点,甜橘子网站制作为客户规划、定制符合企业需求、带有营销价值的建站方案,提供从网站前期定位分析策划到网站界面设计... </div> <div class="c_l"></div> </div> <div class="wrap"> <div class="f_div3"> <span class="l">成都网站制作案例©2020 甜橘子设计 版权所有 | <a href="http://chengdu.cdxwcx.cn" target="_blank">甜橘子网站设计</a><a href="http://chengdu.cdxwcx.cn" target="_blank">chengdu.cdxwcx.cn</a></span> <span class="r"><a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备11025516号</a></span> </div> </div> </div> </footer> <!--尾部end--> <script language="javascript" src="/Public/Home/js/foot.js"></script> <!--尾部end--> <!--侧边栏begin--> <div class="side"> <ul> <li id="qqonline_xbceo"><a href="tencent://message/?uin=631063699&Site=&Menu=yes"><i class="bgs1"></i>QQ咨询</a></li> <li class="shangqiao"><a href="tencent://message/?uin=532337155&Site=&Menu=yes" title="在线咨询"> <div><i class="bgs2"></i>在线咨询</div> </a></li> <li class="sideewm"><i class="bgs3"></i>官方微信 <div class="ewBox"></div> </li> <li class="sideetel"><i class="bgs4"></i>联系电话 <div class="telBox"> <dd class="bgs1"><span>座机</span><a href="tel:028-86922220" target="_blank">028-86922220</a></dd> <dd class="bgs2"><span>手机</span><a href="tel:13518219792" target="_blank">13518219792</a></dd> </div> </li> <li class="sidetop" onClick="goTop()" id="sidetop"><i class="bgs6"></i>返回顶部</li> </ul> </div> <script type="text/javascript"> $('.sideewm').hover(function(){ $('.ewBox').stop().fadeIn(); },function(){ $('.ewBox').stop().fadeOut(); }); $('.sideetel').hover(function(){ $('.telBox').stop().fadeIn(); },function(){ $('.telBox').stop().fadeOut(); }); $(".con_id img").each(function(){ var src = $(this).attr("src"); //获取图片地址 var str=new RegExp("http"); var result=str.test(src); if(result==false){ var url = "https://www.cdcxhl.com"+src; //绝对路径 $(this).attr("src",url); } }); </script> <!-- WPA start --> <!-- WPA end --> <!--侧边栏end--> </body> </html>