hexo博客安装+git上线

备份下之前在windows机器上部署安装hexo的经历和步骤

本机环境:windows10(1903) , 服务器环境 阿里云ECS centos6

以下是需要在服务器上的相关安装
1. 首先需要安装web环境,我安装的lnmp
之前已经安装过,我使用过的是 lnmp(https://lnmp.org/install.html)
这个用起来比较简单,但是没有可视化操作界面

1
wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && ./install.sh lnmp

添加一个网站

1
lnmp vhost add # 按照指引一步步填就可以了

我放在 /home/wwwroot/hexo_win 目录

接着去阿里云域名管理页将域名指向到服务器ip

  1. 安装git
    之前也已经安装过,可以去git官网按指示安装,配置起来也比较简单
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
# 安装git
apt-get install git #或者 yum install git
# 配置用户
adduser git
# 修改文件权限
chmod 740 /etc/sudoers
# 修改内容,添加权限
vi /etc/sudoers
# 找到 root ALL=(ALL) ALL 这句,在下面再加一行
git ALL=(ALL) ALL
# 保存退出,改回文件权限
chmod 400 /etc/sudoers
# 设置git用户密码
sudo passwd git
# 切换到git用户,创建.ssh文件夹
su git
cd ~
mkdir .ssh
cd .ssh
# 生成要是对
ssh-keygen
# 复制公钥
cp id_rsa.pub id_rsa_copy.pub
# 修改文件权限
chmod 600 ~/.ssh/id_rsa_copy.pub
chmod 700 ~/.ssh
  1. 在本机测试git连接
1
2
3
4
5
6
# ssh 连接git
ssh -v git@服务器ip

# 根据提示输入密码
# 看到服务器欢迎语句即连接成功
# Welcome to Alibaba Cloud Elastic Compute Service !
  1. 接上一步,创建hexo git仓库
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
cd ~
git init --bare hexo.git
# 创建完成后加入钩子
# 以便本地提交后线上自动更新
# 将第一步中初始化的网站目录给git用户访问
chown git:git /home/wwwroot/hexo_win/
# 再创建一个缓存目录(随便一个地址,存在就行)
mkdir ~/tmp/hexo_tmp
# 修改hook文件
cd ~/hexo.git/hooks
touch post-receive
vi post-receive
# 填入以下信息

#!/bin/bash -l
GIT_REPO=/home/git/hexo.git
TMP_GIT_CLONE=/root/tmp/hexo_tmp
PUBLIC_WWW=/home/wwwroot/hexo_win
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

以下是我配置完成后的信息

域名 live.wisesz.cn
目录 /home/wwwroot/hexo_win
git /home/git/hexo.git

至此线上部署完成

以下是本机需要做的安装

1. 下载并安装NodeJS (https://nodejs.org/en/)
安装后进入cmd查看

1
2
3
4
node -v
v12.16.0
npm -v
6.13.4

可以看到版本好说明安装成功

  1. 安装hexo
    1
    cnpm install -g hexo-cli

完成后新建一个目录当做hexo的程序目录(我的是 D:\www\hexo)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 初始化安装hexo
D:
cd www/hexo
hexo init

# 等待安装完成后安装以下插件
npm install hexo-deployer-git --save
npm install hexo-server

# 等待安装完成后
# 渲染静态页面
hexo g
# 启动服务
hexo s

至此浏览器输入 http://localhost:4000 已经可以访问初始化的hexo了

3. 配置hexo _config.yml

1
2
3
4
5
6
# 打开_config.yml 修改以下
deploy:
type: git
repo: git@这里改为服务器公网IP:/home/git/hexo.git
branch: master
message:
  1. 测试hexo
1
2
3
4
hexo new "test new blog"
hexo clean
hexo g
hexo s

访问http://localhost:4000 已经可以看到刚刚创建的文章了

5. 部署到线上

1
hexo d

访问 部署的域名即可看到线上的内容了