本地配置

配置用户名邮箱

#配置过Git的请省略以下两步
git config --global user.name "你的用户名"
git config --global user.email "你的电子邮箱"

配置SSH公钥

cd ~
mkdir .ssh
cd .ssh
ssh-keygen -t rsa

服务器配置Git库

安装Git

# CentOS用yum安装
sudo apt-get install git

建立网站目录

# 我的博客目录在/home/www/jx,你们自行修改成自己目录
cd /home
mkdir www
cd www
mkdir jx

在WWW文件夹里建议Git库

git init --bare jx.git

配置 Git Hooks

cd jx.git/hooks

编辑post-receive文件(没有就新增):

vim post-receive
# Git仓库
GIT_REPO=/home/www/jx.git
# 临时目录
TMP_GIT_CLONE=/tmp/hexo
# 网站目录
PUBLIC_WWW=/home/www/jx
rm -rf ${TMP_GIT_CLONE}
git clone $GIT_REPO $TMP_GIT_CLONE
rm -rf ${PUBLIC_WWW}/*
cp -rf ${TMP_GIT_CLONE}/* ${PUBLIC_WWW}

赋予执行权限

chmod +x post-receive

服务器安装与配置Nginx

安装Nginx

# CentOS用yum安装
sudo apt-get install nginx

配置Nginx

vim /etc/nginx/nginx.conf

我的nginx.conf文件(仅供参考)

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    # 上面可以不用管,以下是重点

    # Http服务,server_name 后填域名,一个空格一个,root填网站根目录

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  www.jixian.io jixian.io;
        root         /home/www/jx;


        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }


# 如果没有SSL证书或者不打算配Https,以下请忽略,也不用复制进去!!!!
# 我的是腾讯云免费的SSL证书,在腾讯云下载证书后我把crt和key文件放到了/etc/nginx/zs/目录下

# Settings for a TLS enabled server.
#
    server {
        listen       443 ssl http2 default_server;
        listen       [::]:443 ssl http2 default_server;
        server_name  jixian.io www.jixian.io;
        root         /home/www/jx;

        ssl_certificate "/etc/nginx/zs/wwwjixianio.crt";
        ssl_certificate_key "/etc/nginx/zs/wwwjixianio.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;

#        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

}

重启Nginx

service nginx restart

上传本地公钥

ssh-copy-id -i ~/.ssh/id_rsa.pub root@服务器ip地址

Hexo配置文件

deploy:
     type: git
     repo: root@你的IP:/home/www/jx.git
     branch: master