本篇前置条件:

1 个人服务器已安装git,则可创建git仓库,参考http://blog.ixiewei.com/index.php/archives/266.html

2 本地PC机已安装node,hexo,参考对应官方网站手册即可

以下为具体步骤:

第一步:服务端创建对应git仓库:hexo-blog.git

cd /home/git 
su - git (因我的git账户名为git,需切换权限)
mkdir hexo-blog.git
cd hexo-blog.git
git --bare init

则此时git地址为:git@127.1.1.1:/home/git/hexo-blog.git
git 为您的账号
127.1.1.1 为您的服务器ip
/home/git/hexo-blog.git 为仓库目录

可以在本地PC验证服务端git仓库有效性:
git clone git@127.1.1.1:/home/git/hexo-blog.git

第二步:本地PC端 安装hexo-deployer-git,并配置hexo项目的 _config.yml 文件

hexo项目中安装hexo-deployer-git:

npm install hexo-deployer-git –save

配置_config.yml 文件:

deploy:
  type: git
  message: update files
  repo: git@127.1.1.1:/home/git/hexo-blog.git
  branch: master

注意,此处有坑
注意,此处有坑
注意,此处有坑
重要的事情说三遍!!!
因本地PC机用的window电脑,故冒号后面需带有空格,否则hexo d 不生效
即:“type: git”

第三步:提交部署hexo到服务端git仓库

hexo g
hexo d (输入您的git账户密码即可)

第四步:用脚本将仓库的资源拉取到nginx代理的对应目录下

让Git仓库每次检测到push行为后,将最新的资源文件Git clone在你要访问的文件夹下,这样达到自动化部署blog项目

1 进入git仓库下的Hooks目录,配置回调post-receive,执行

cd /home/git/hexo-blog.git/hooks
 
touch post-receive
 
vim post-receive

2 写入以下脚本:


#!/bin/sh
GIT_REPO=/home/git/hexo-blog.git
TMP_GIT_CLONE=/home/git/tmp/hexo-blog
PUBLIC_WWW=/home/git/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}

其中public_www就是用于最终存放nginx映射的静态资源文件目录

3 修改目录权限修

chmod +x post-receive
 
chmod 777 -R /home/git/www/hexo
 
chmod 777 -R /home/git/tmp/hexo-blog

对应的目录,务必保证有权限,否则不能正确执行脚本,不能拷贝对应目录文件

第五步:nginx配置

server {
 
    listen 80 ;
 
    root /home/git/www/hexo;
 
    server_name hexo.ixiewei.com;                                 
 
    location / {                                                             
 
        root /home/git/www/hexo;
 
        if (-f $request_filename) {
 
            rewrite ^/(.*)$  /$1 break;
 
        }
 
    }                                                                         
 
}

此处保证nginx运行账户,拥有/home/git/www/hexo各级目录权限,否则可能出现403

效果如下:

http://hexo.ixiewei.com/

备注:亲身实践细节,记录每个点滴

标签: none

添加新评论

选择表情