Windows 先安装好 Git,开始下面步骤:

仓库

vps 创建 git 账户

pt-get update && apt-get install git nano curl -y
adduser git
su git
cd
mkdir .ssh && cd .ssh

Windows git 界面生成密钥

ssh-keygen -t rsa -b 4096

路径在:C:\Users\用户\.ssh\id_rsa.pub,导入 id_rsa.pub 的内容到服务器 authorized_keys

nano authorized_keys
exit

Windows git 下测试连通ssh -v git@服务器 IP

回到 vps,在 root 下赋予权限

chmod 700 /home/git/.ssh
chmod 600 /home/git/.ssh/authorized_keys

git 用户下初始化仓库

su git
cd
mkdir hexo.git && cd hexo.git
git init --bare
`

创建脚本

mkdir ~/git/tmp/hexo_tmp -p
cd ~/hexo.git/hooks
nano post-receive

### 输入 ###

    #!/bin/bash -l
    GIT_REPO=/home/git/hexo.git
    TMP_GIT_CLONE=/home/git/tmp/hexo_tmp
    PUBLIC_WWW=/home/www
    rm -rf ${TMP_GIT_CLONE}
    git clone $GIT_REPO $TMP_GIT_CLONE
    rm -rf ${PUBLIC_WWW}/*
    cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

    ### 以上 ###

    exit
    chmod +x /home/git/hexo.git/hooks/post-receive

root 下建立站点目录

mkdir /home/www -p
chown git:git /home/www

安装 Nginx

设定安装源,可以安装到最新 Nginx。

cd
curl -L http://nginx.org/keys/nginx_signing.key -o nginx_signing.key && apt-key add nginx_signing.key && nano /etc/apt/sources.list

Debian 7

deb http://nginx.org/packages/mainline/debian/ wheezy nginx
deb-src http://nginx.org/packages/mainline/debian/ wheezy nginx

Debian 8

deb http://nginx.org/packages/mainline/debian/ jessie nginx deb-src http://nginx.org/packages/mainline/debian/ jessie nginx

安装apt-get update && apt-get install nginx -y

配置

    cd /etc/nginx/conf.d
    cp default.conf hexo.conf
    mv default.conf{,_`date +%F`}
    nano hexo.conf

    ### 内容 ###

    server {
     index index.html index.htm;
     server_name google.com;
     root /home/www;

     location / {
         try_files $uri $uri/ /index.html;
        }
    }

重启service nginx restart

hexo 本地环境

初始化环境

    cd
    npm install hexo -g
    hexo init hexo(hexo 可指定路径)
    cd hexo
    hexo g && hexo s

打开浏览器 http://127.0.0.1:4000 即可看到本地浏览。

推送工具(必备)npm install hexo-deployer-git -save

修改主题,实时预览npm install hexo-browsersync --save

推送参数

    deploy:
      type: git
      repository: git@主机IP:hexo.git
      branch: master

清理、编译、推送hexo clean && hexo g && hexo d

同时推送 VPS & Github

    deploy:
     - type: git
       repository: git@github.com:bpplpp/bpplpp.github.io.git
       branch: master
     - type: git
       repository: git@主机IP:hexo.git
       branch: master

服务器安全

禁止 git shell 登陆nano /etc/passwd

    git:x:1000:1000:,,,:/home/git:/bin/bash
    // 修改
    git:x:1000:1000:,,,:/home/git:/wp-content/bin/git-shell

问题集

`ssh: connect to host xx.xx.xx.xx port 22: Connection refused`  
因改过SSH端口`ssh: Could not resolve hostname xx.xx.xx.xx:123: Name or service not known`

解决:

Windows:C:\Users\Administrator\.ssh\
Linux:/home/git/.ssh/
在两个.ssh 目录创建文件 config 内容:

    Host 服务器
    Port 端口