服务器环境为 Ubuntu 16.04.1 LTS x64
本地环境为 Win10 x64
1. 本地操作
1.1. 安装 Git 和 Node.js
本地需要安装 Git 和 Node.js,安装过程略。
安装完git后还要配置环境变量:
右键我的电脑 –> 属性,然后点击高级系统设置 –> 环境变量 –> 选择用户变量或系统变量中的Path,点击编辑;找到Git安装目录,添加以下地址:
D:\Program Files\Git\bin
D:\Program Files\Git\mingw64\libexec\git-core
D:\Program Files\Git\mingw64\bin
1.2. 配置SSH 公钥
Windows 上安装 Git for Windows 之后在开始菜单里打开 Git Bash 输入:
1 2
| git config --global user.name "你的用户名" git config --global user.email "你的电子邮箱"
|
1 2 3 4
| cd ~ mkdir .ssh cd .ssh ssh-keygen -t rsa
|
在系统当前用户文件夹下生成了私钥 id_rsa
和公钥 id_rsa.pub
。
1.3. 初始化 Hexo
在电脑任意目录新建一个文件夹 hexo
,进入文件夹,在空白处点击右键选择 Git Bash,输入:
1 2 3 4 5
| npm install -g hexo-cli hexo init npm install hexo d -fg hexo serve
|
这样便在本地初始化了 Hexo 文件夹,然后输入:
hexo new post "第一篇文章"
即可新建一篇文章,用文本编辑器打开 hexo/source/_post/第一篇文章.md
可以快速开始写作。其余使用方法和配置按照 Hexo 官网操作即可。推荐编辑器**hexo-editor**
2020/4/27更新: hexo-editor速度太慢,改用Typora
1.4. 修改 deploy 参数
打开位于 hexo
文件夹下的 _config.yml
,修改 deploy 参数:
1 2 3 4
| deploy: type: git repo: git@blog.yizhilee.com:hexo.git branch: master
|
1.4.1. 提交到github
1 2 3 4
| deploy: type: git repository: git@github.com:iwyang/iwyang.github.io branch: master
|
1.4.2. 提交到coding
1 2 3 4 5 6
| deploy: type: git repo: coding: git@e.coding.net:iwyang/iwyang.coding.me.git branch: master
|
1.4.3. github、coding双线部署
1 2 3 4 5 6
| deploy: type: git repo: github: git@github.com:iwyang/iwyang.github.io.git coding: git@e.coding.net:iwyang/iwyang.coding.me.git branch: master
|
1.4.4. github、coding、服务器三线线部署
1 2 3 4 5 6 7
| deploy: type: git repo: github: git@github.com:iwyang/iwyang.github.io.git coding: git@e.coding.net:iwyang/iwyang.coding.me.git 服务器: git@45.76.173.167:hexo.git branch: master
|
2. 服务器操作
2.1. 安装依赖
首先,在 服务器 上安装 Git 和 nginx。
1 2
| apt-get update -y apt-get install git-core nginx -y
|
2.2. 配置用户
然后新增一个名为 git
的用户,过程中需要设置登录密码,先输入两次密码,然后按几次回车就可。
adduser git
给用户 git
赋予无需密码操作的权限(否则到后面 Hexo 部署的时候会提示无权限)
1 2
| chmod 740 /etc/sudoers vi /etc/sudoers
|
在图示位置root ALL=(ALL:ALL) ALL
的下方添加
然后保存。然后更改读写权限。
2.3. 上传 SSH 公钥
接下来要把本地的 ssh 公钥上传到 服务器 。执行
1 2 3 4 5
| su git cd ~ mkdir .ssh && cd .ssh touch authorized_keys vi authorized_keys
|
现在要打开本地的 Git Bash
,输入vi ~/.ssh/id_rsa.pub
,把里面的内容复制下来粘贴到上面打开的文件里。
然后建立放部署的网页的 Git 库。
1 2 3
| cd ~ mkdir hexo.git && cd hexo.git git init --bare
|
测试一下,如果在 Git Bash 中输入 ssh git@服务器的IP地址
能够远程登录的话,则表示设置成功了。
如果不成功,并且你的 服务器 的 ssh 端口不是 22
的话,请在Git Bash
执行vi ~/.ssh/config
,输入以下内容并保存:(成功就跳过这一步)
1 2 3 4 5
| Host #服务器 的 IP HostName #服务器 的 IP User git Port #SSH 端口 IdentityFile ~/.ssh/id_rsa
|
ps: 如果配置完成还是提示要输入密码,可以使用 ssh-copy-id
,在本地打开 Git Bash 输入:
1
| ssh-copy-id -i ~/.ssh/id_rsa.pub git@服务器ip地址
|
2.4. 用户授权
接下来要给用户 git 授予操作 nginx 放网页的地方的权限:
1 2 3
| cd /var/www mkdir hexo chown git:git -R /var/www/hexo
|
2.5. 配置钩子
现在就要向 Git Hooks 操作,配置好钩子:
1 2 3
| su git cd /home/git/hexo.git/hooks vi post-receive
|
输入内容并保存:(里面的路径看着换吧,上面的命令没改的话也不用换)
1 2 3 4 5 6 7 8
| #!/bin/bash GIT_REPO=/home/git/hexo.git TMP_GIT_CLONE=/tmp/hexo PUBLIC_WWW=/var/www/hexo rm -rf ${TMP_GIT_CLONE} git clone $GIT_REPO $TMP_GIT_CLONE rm -rf ${PUBLIC_WWW}/* cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}
|
赋予可执行权限:
2.6. 配置 nginx
然后是配置 nginx。执行
1
| vi /etc/nginx/conf.d/hexo.conf
|
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 29
| server { listen 80 ; listen [::]:80; root /var/www/hexo; server_name bore.vip www.bore.vip; access_log /var/log/nginx/hexo_access.log; error_log /var/log/nginx/hexo_error.log; error_page 404 = /404.html; location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { root /var/www/hexo; access_log off; expires 1d; } location ~* ^.+\.(css|js|txt|xml|swf|wav)$ { root /var/www/hexo; access_log off; expires 10m; } location / { root /var/www/hexo; if (-f $request_filename) { rewrite ^/(.*)$ /$1 break; } } location /nginx_status { stub_status on; access_log off; } }
|
因为放中文进去会乱码所以就不在里面注释了。代码里面配置了默认的根目录,绑定了域名,并且自定义了 404 页面的路径。
最后就重启 nginx 服务器:
1
| /etc/init.d/nginx restart
|
ps: 最好做一个301跳转,把bore.vip和www.bore.vip
合并,并把之前的域名也一并合并. 有两种实现方法,第一种方法是判断nginx核心变量host(老版本是http_host):
1 2 3 4 5 6 7
| server { server_name bore.vip www.bore.vip ; if ($host != 'bore.vip' ) { rewrite ^/(.*)$ http://bore.vip/$1 permanent; } ... }
|
3. 发布文章
在本地编辑好文章之后使用 hexo g -d
,如果hexo d后, ERROR Deployer not found: git,执行
1
| npm install -- save hexo-deployer-git
|
4. 参考链接
1.通过 Git Hooks 自动部署 Hexo 到 服务器
2.在服务器上搭建hexo博客,利用git更新
3.Windows10下Git环境变量配置
4.hexo d后 ERROR Deployer not found: git
5. Nginx 301重定向域名