前言
如果觉得Blog托管服务满足不了自己折腾,可将资源迁移到自己的云服务器上
目标
- 服务端部署hexo博客站点环境
- 客户端发布文章推送到服务端
准备环境
服务端必备软件
- Git
- Nginx
客户端必备软件
- Git
- Node.js
- hexo
服务端配置(以Centos为例)
添加Epel源方便yum安装Nginx
wget https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpmrpm -ivh epel-release-7-11.noarch.rpmyum clean allyum makecache安装Git Nginx
yum install git nginx -y创建Nginx用户与群组
groupadd nginxuseradd -g nginx -s /sbin/nologin nginx创建Nginx hexo站点文件目录
mkdir -pv /webdata/www/hexochown -R nginx:nginx /webdata/www/hexo配置Nginx 配置文件路径
vim /etc/nginx/nginx.conf所需修改段
.....server { listen 80 default_server; server_name www.example.com; root /webdata/www/hexo; index index.html index.htm; }.....执行service nginx configtest检查nginx.conf语法是否正确,显示success然后重启service nginx restart
Git 配置 创建文件目录,用于私人Git仓库搭建
mkdir -pv /data/repositoryGit 初始化裸库
cd /data/repositorygit init --bare hexo.git创建Git钩子(hook)
vim /data/repository/hexo.git/hooks/post-receive指定Nginx站点目录与Git仓库地址
#!/bin/bashgit --work-tree=/webdata/www/hexo --git-dir=/data/repository/hexo.git checkout -f保存退出并添加执行权限
chmod +x /data/repository/hexo.git/hooks/post-receive客户端配置
本篇以本地虚拟机Centos为例,其它平台对应搜索方知如何安装。 安装Git Nodejs
yum install git nodejs -y安装hexo
npm install -g hexo-clihexo初始化与安装
mkdir -pv <folder> (/data/www/hexo)hexo init <folder>cd <folder>npm install至于后面的hexo详细配置,请参照hexo 文档.
配置本地客户端与服务端连接 打开站点配置文件
vim /data/www/hexo/_config.yml设置Deployment字段
## Docs: https://hexo.io/docs/deployment.htmldeploy: type: git repo: root@域名或者IP:/data/repository/hexo.git #SSH默认22端口 #repo: ssh://root@域名或者IP:端口/data/repository/hexo.git #SSH非默认22端口 branch: master如果每次发布都要输入密码嫌麻烦,可以配置客户端与服务端RSA认证,如果不知可以通过搜索引擎找到。
配置Git邮箱与用户 Git初次连接需要配置邮箱与用户
git config --global user.email "you@example.com"git config --global user.name "Your Name"部署hexo
将本地生成的静态文件部署到服务端 清除缓存
hexo clean生成静态页面
hexo generate启动服务器
hexo server #调试网页用,并非必要将本地hexo目录文件部署到服务端
但是在这之前需要安装hexo-deployer-git插件
npm install hexo-deployer-git --save然后执行部署
hexo deploy结语
看似写了一大推,其实也没多少地方要配置的,Blog部署起来反而简单了。