写在前面
因为 hexo 的搭建和使用与其他博客程序不同,而且搜索到的很多结果都是在 Github 上搭建 hexo 的教程。
于是就有很多朋友问我 VPS 也可以搭建 hexo 吗?
刚好前几天在迁移博客的时候也查了一些资料,现在再整理一遍。
方案
Hexo 本地生成静态文件,再部署到 VPS 上,用 Nginx 做 Web 服务器。
准备
- VPS
- Macintosh / Hackintosh OS
本地环境
Node.js
$ brew install node
创建 Hexo 目录
$ mkdir "your hexo dir name"
$ cd "your hexo dir name"
安装 Hexo
$ npm install -g hexo-cli
如果上面这行安装命令报错了,可以试试看下面这行
$ sudo npm install -g hexo-cli --unsafe
安装完成之后
$ hexo init
$ npm install
$ hexo d -fg
$ hexo serve
打开 http://localhost:4000 如果看到 hexo 的初始页面证明安装成功。
生成 SSH 公钥密钥
$ cd ~/.ssh
$ ssh-keygen
它先要求你确认保存公钥的位置(.ssh/id_rsa),然后它会让你重复一个密码两次,如果不想在使用公钥的时候输入密码,可以留空。
GitHub Help - Generating a new SSH key and adding it to the ssh-agent
服务器环境 (以 Ubuntu 为例)
安装 Git & Node.js
$ apt-get install git
$ curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
$ apt-get install -y nodejs
创建 git 用户 & 禁用 git 用户的 shell 登录权限
$ adduser git
$ vim /etc/passwd
将
git:x:1001:1001:,,,:/home/git:/bin/bash
改为
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
添加证书登录
前面已经生成了公钥,现在把 .pub 公钥里的内容添加到服务器的 /home/git/.ssh/authorized_keys 文件中
初始化 Git 仓库
目录可自己选择,此处用 your-git-dir 代替目录
$ mkdir your-git-dir
$ cd your-git-dir
$ git init --bare hexo.git
使用 --bare 参数,Git 就会创建一个裸仓库,裸仓库没有工作区,我们不会在裸仓库上进行操作,它只为共享而存在。
配置 git hooks
在 hexo.git/hooks 目录下新建一个 post-receive 文件:
$ cd /your-git-dir/hexo.git/hooks
$ vim post-receive
在 post-receive 文件中写入如下内容:
#!/bin/sh
git --work-tree=/www/hexo --git-dir=/your-git-dir/hexo.git checkout -f
其中 /www/hexo 为部署目录,/your-git-dir/hexo.git 为该 git 仓库。
设置这个文件的可执行权限:
$ chmod +x post-receive
Git 钩子
设置拥有者
$ chown -R git:git hexo.git
$ chown -R git:git /www/hexo
本地配置
现在配置 hexo 的 deploy。
修改 hexo 目录下的 _config.yml 找到 deploy, 修改为:
repo 的地址为你自己的地址以及 git 仓库目录
deploy:
type: git
repo: git@www.example.com:/your-git-dir//hexo.git
branch: master
开始使用
新建文章:
$ hexo new "post name"
生成 & 部署:
$ hexo clean && hexo g && hexo d
参考
原文来自:Viosey's Blog
声明:所有来源为“澳门太阳集团城网址8722”的内容信息,未经本网许可,不得转载!如对内容有异议或投诉,请与我们联系。邮箱:marketing@think-land.com