hexo博客部署到云服务器(使用宝塔面板)

思路是:

在服务器上搭建Git环境,创建Git仓库
在主机生成Hexo静态文件,通过与服务器连接,推到服务器上的Git仓库
通过git-hooks实现自动部署到网站资源目录
Nginx做静态文件服务器,实现外界对网站资源目录的访问
一、环境准备

  1. 安装宝塔面板

我是安装了服务器上面的Cloudreve镜像,自带有宝塔面板。宝塔面板安装很简单,直接连上服务器(推荐宝塔终端,很简单易用),输入

1
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install\_6.0.sh && sh install.sh

显示如下即成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
外网面板地址: http://49.232.1.111:8888 #用来访问面板

内网面板地址: http://10.0.8.13:8888

\*以下仅为初始默认账户密码,若无法登录请执行bt命令重置账户/密码登录

username: ehmpmg7m #记住用户名和密码

password: dsjfhasd #忘记可使用sudo /etc/init.d/bt default查看

If you cannot access the panel,

release the following panel port [8888] in the security group

若无法访问面板,请检查防火墙/安全组是否有放行面板[8888]端口 #安全组注意放行,轻量级服务器为防火墙

然后在宝塔面板网站->添加站点,找到放置网站资源目录
www/wwwroot/taitrs.cc
2.安装nginx和git

1
yum install -y nginx git

3.Nginx服务器配置
通过宝塔面板可以方便地修改Nginx的配置文件,在taitres.cc的设置中找到配置文件,应该是自动配好了的,没有就自己设置下,也可以使用其他端口

二、Git仓库搭建 之前推hexo的静态文件都是推到GitHub的仓库,现在服务器也是一样,需要仓库来保存 1.添加一个用户git 在服务器端
1
2
3
4
5
6
7
8
9
10
11
adduser git   #添加git用户

chmod 740 /etc/sudoers #改变sudoers文件的权限为文件所有者可写

vim /etc/sudoers

#在root ALL=(ALL) ALL 下方添加一行

git ALL=(ALL) ALL

chmod 400 /etc/sudoers #将其权限修改为文件所有者可读
2.给git用户添加ssh密钥 这一步是为了建立主机与服务器连接,使其不需要密码也能登陆
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#在主机端打开powershell,cd到C:\Users\admin\.ssh生成密匙,如已有密匙可跳过这一步

ssh-keygen -t rsa -C "taitres.cc"

# -t 指定密钥类型,默认是 rsa ,可以省略

# -C 用于识别这个密钥的注释,可以输入任何内容

# -f 指定密钥文件存储文件名,默认id\_rsa



#在服务器端

su git #切换到git用户

mkdir -p ~/.ssh

touch ~/.ssh/authorized\_keys #创建authorized\_keys文件

chmod 600 ~/.ssh/authorized\_keys #为authorized\_keys文件赋予文件所有者可读可写的权限

chmod 700 ~/.ssh #为.ssh文件夹赋予文件夹所有者可读可写可执行的权限
复制公匙id_rsa.pub内容到服务器/home/git/.ssh/authorized_keys,关闭终端,使用`ssh git@server`重新登录服务器,测试是否能不要密码登录到git用户,如出现Permission denied的问题可尝试文章末尾的解决办法

3.创建Git仓库

1
2
3
4
5
sudo mkdir /home/git/repos    #新建目录,这是git仓库的位置

cd /home/git/repos

sudo git init --bare taiblog.git #初始化一个名叫taiblog的仓库

4.配置钩子实现自动部署
找到 /home/git/repos/taiblog.git/hooks/post-update.sample 改名post-update,内容改为

1
2
3
#!/bin/sh

git --work-tree=/www/wwwroot/taitres.cc --git-dir=/home/git/repos/taiblog.git checkout -f

然后给权限

1
2
3
4
5
6
7
cd taiblog.git/hooks/

sudo chmod +x post-update #赋予其可执行权限

sudo chown -R git:git /home/git/repos/ #仓库所有者改为git

sudo chown -R git:git /www/wwwroot/taitrs.cc/ #站点文件夹所有者改为git

5.测试Git仓库是否可用

1
#在主机端,如果能将仓库拉下来,说明Git仓库搭建成功git clone git@server\_ip:/home/git/repos/blog.git

三、本地配置和测试
1.本地配置
修改本地Hexo博客文件夹中的\_config.yml文件

1
2
3
4
5
6
7
deploy:

type: git

repo: git@server:/home/git/repos/taiblog.git

branch: maste

2.测试

1
2
3
4
5
hexo clean  #清除缓存

hexo generate #生成静态页面

hexo delopy #将本地静态页面目录部署到云服务器

也可以在package.json 中添加 npm 脚本,这样就可以直接npm run dd一下执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"scripts": {

"build": "hexo generate",

"clean": "hexo clean",

"deploy": "hexo deploy",

"dd": "hexo clean && hexo g -d",

"server": "hexo server",

"ss": "hexo clean && hexo g && hexo s"

},

然后访问自己的IP看看是否成功49.232.1.11

遇到的一些问题
在ssh-copy-id 时报错,ssh-copy-id : 无法将“ssh-copy-id”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
解决办法:需要在powershell配置文件中添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function ssh-copy-id([string]$userAtMachine){   

$publicKey = "$ENV:USERPROFILE" + "/.ssh/id\_rsa.pub"

if (!(Test-Path "$publicKey")){

Write-Error "ERROR: failed to open ID file '$publicKey': No such file"

}

else {

& cat "$publicKey" | ssh $userAtMachine "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized\_keys || exit 1"

}

}

关于powershell配置文件的使用:

1
1.首先检查配置文件是否已经存在
1
2.打开powershell输入 `Test-Path $profile`如果返回false,则在C:\Users\admin\Documents\下创建一个WindowsPowerShell文件夹,然后执行
1
New-Item -path $profile -type file

就创建了一个Microsoft.PowerShell_profile.ps1文件

使用ssh-add时出现 unable to start ssh-agent service, error :1058
解决方法:使用管理员权限运行 Power Shell,然后执行Set-Service -Name ssh-agent -StartupType automatic.

把 ssh-agent 的启动类型设置为自动方式,在任务管理器中查看,ssh-agent是否开启

连接ssh时出现 Permission denied
解决方法:vi /etc/ssh/sshd\_config 修改为PubkeyAuthentication yes(如果前面有#,就去掉#)