hexo从github移动到centos个人服务器

配置nginx,将http 301重定向到https,

前期安装

首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库

1
yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel

以及下面的gcc make wget等

1
2
yum -y install wget
yum install -y gcc gcc-c++ make

http://nginx.org/en/download.html 这里有nginx各个版本的下载地址

创建一个目录

1
2
mkdir /hexo
cd /hexo

下载nginx包

1
wget http://nginx.org/download/nginx-1.14.0.tar.gz

解压安装

1
2
3
4
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/hexo/nginx/ --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-debug
make && make install && echo "ok"

yum -y install git #安装git
adduser git #创建一个git用户,用来与虚拟git服务

创建裸仓:

创建证书登录
将本地用户的公钥id_rsa.pub导入到/home/git/.ssh/authorized_keys文件里面
在导入之前,并没有.ssh这个文件以及authorized_keys这个文件
所以先敲一下命令建立

1
2
cd /home/git
mkdir .ssh

创建一个新文件

1
vim authorized_keys

公钥文件本地地址:
windows用户:本地用户公钥文件在c:/users/用户/.ssh/里面 这里的/好像写反了
由于我使用的是linux系统,linux公钥文件在/home/xxx/.ssh/id_rsa.pub里面
linux用户直接输入cat /home/anjing/.ssh/id_rsa.pub 可查看到
将内容复制到服务器的authorized_keys即可

建立git仓库:

由于服务器上没有git仓库,所以我们先建立一个

选择一个目录作为git仓库

1
2
cd /home/git
git init --bare hexo.git

Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾。然后,把owner改为git
如果提示图片上的内容表示创建成功
Alt text
接着更改权限

1
chown -R git:git hexo.git

禁用shell登录:
vim /etc/passwd
找到类似下面这行

1
git:x:1001:1001::/home/git:/bin/bash

将他修改为

1
git:x:1001:1001::/home/git:/bin/bash/git-shell

就是在最后面加上git-shell就行了,这样,git用户可以正常通过ssh使用git,但是无法登录shell

配置nginx

刚刚将nginx安装到了/hexo/nginx 里面

1
vim /hexo/nginx/conf/nginx.conf

首先将证书上传,如何获取证书自己百度或谷歌找找,这里我们只谈怎么配置

可以将里面的配置都删除,使用我下面的配置,默认使用了https,如果不需要可以自己删除443段和跳转

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
user  git;
worker_processes auto;

pid logs/nginx.pid;
worker_rlimit_nofile 10240;

events
{
use epoll;
worker_connections 10240;
multi_accept on;
}


http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;

sendfile on;
keepalive_timeout 65;
fastcgi_intercept_errors on;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";

server {
listen 80;
server_name smelond.com;
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
}


server {

listen 443;
server_name smelond.com;
ssl on;
ssl_certificate /hexo/nginx/ssl/fullchain.pem;
ssl_certificate_key /hexo/nginx/ssl/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH;
ssl_prefer_server_ciphers on;
root /home/git;
index index.html index.htm;
location ~ .*\.(ico|gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 1d;
}

location ~ .*\.(js|css|txt|xml)?$ {
access_log off;
expires 12h;
}

location / {
try_files $uri $uri/ =404;
}

access_log /hexo/nginx/logs/access_log.log;
error_log /hexo/nginx/logs/error_log.log;
}

}

完成后可以启动nginx

1
2
3
/hexo/nginx/sbin/nginx -s reload    # 启动nginx
/hexo/nginx/sbin/nginx -s stop # 停止nginx
/hexo/nginx/sbin/nginx -t # 检查配置文件是否正确

如出现报错,可以执行下面语句,然后重新启动nginx

1
/hexo/nginx/sbin/nginx -c /hexo/nginx/conf/nginx.conf

自动部署

在第上一步中建立的裸仓中(hexo.git目录),找到hooks目录下的post-update.sample,将他重命名为post-update

1
2
cd /home/git/hexo.git/hooks/
mv post-update.sample post-update

然后用vim打开post-update
添加下面内容:

1
git --work-tree=/home/git --git-dir=/home/git/hexo.git checkout -f

1
2
3
4
5
6
7
8
9
#!/bin/sh
git --work-tree=/home/git --git-dir=/home/git/hexo.git checkout -f #添加到#!/bin/sh下面这行即可
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".

exec git update-server-info

接下来在本地的hexo项目中,编辑_config.yml,需要修改deploy,只需要将repo后面的域名修改为你的服务器地址(如果ssh端口已经修改,请看踩坑部分)

1
2
3
4
deploy:
type: git
repo: [email protected]:/home/git/hexo.git
branch: master

接着安装hexo-deployer-git

1
sudo npm install hexo-deployer-git --save

完成后就可以执行了

1
2
3
4
hexo new ""
hexo clean
hexo g
hexo d

踩坑

最后 献上一个巨大的坑
Alt text
如果遇到类似以上问题,可能是你的ssh登录端口 不为默认的22
解决方法:
在_config.yml配置文件中将 [email protected]:/home/git/hexo.git 修改为 ssh://[email protected]:端口号/home/git/hexo.git

1
2
3
4
deploy:
type: git
repo: ssh://[email protected]:9999/home/git/hexo.git #9999为你的ssh端口号
branch: master





参考:
https://segmentfault.com/a/1190000005723321
https://blog.csdn.net/pop1586082213/article/details/54576131
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000

本文标题:hexo从github移动到centos个人服务器

文章作者:smelond

发布时间:2018年08月09日 - 17:08

最后更新:2018年08月27日 - 18:08

原始链接:http://smelond.com/2018/08/09/hexo从github移动到centos个人服务器/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

分享