随着静态博客方案的成熟,Hexo + 云服务器 依然是技术人搭建个人博客的经典组合。相比 GitHub Pages,自建在云服务器上的博客更灵活、可控性更强,也更适合长期运营。

本文将手把手教你:

使用 Hexo 在腾讯云服务器上搭建一个属于自己的博客网站

安装node.js

首先访问 Node.js 官网 下载并安装 Node.js(建议 LTS 版本):

https://nodejs.org

安装完成后,Node.js 会自动附带安装 npm(Node 包管理工具)

配置 npm 国内镜像源

由于 npm 默认使用国外镜像,下载速度较慢,推荐切换到 淘宝 npm 镜像

1
npm config set registry https://registry.npmmirror.com

验证是否配置成功

1
npm config get registry

安装 Hexo

全局安装 Hexo CLI

1
npm install -g hexo-cli

验证安装是否成功:

1
hexo -v

创建博客目录并初始化

以 Windows 为例,在 D 盘创建 blog 目录:

1
2
cd /d D:\blog
hexo init

安装hexo-server

1
npm install hexo-server --save

启动本地预览

1
hexo s

浏览器访问:

1
http://localhost:4000

Hexo 常用命令说明

1
2
3
4
hexo clean        # 清理缓存
hexo generate # 生成静态文件
hexo deploy # 部署(简写 hexo d)
hexo g -d # 生成并部署

到此本地搭建预览已经完成

腾讯云安装配置 Nginx

安装 Nginx(CentOS)

1
yum install -y nginx

如果提示找不到包:

1
2
yum install -y epel-release
yum install -y nginx

配置 Nginx 站点

1
2
cd /etc/nginx/conf.d
vim test.conf

进入配置目录:

1
2
cd /etc/nginx/conf.d
vim test.conf

示例配置(含 HTTPS + HTTP 重定向):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server {
listen 80;
rewrite ^ https://$host$request_uri? permanent;
}

server {
listen 443 ssl;
server_name test.cn www.test.cn;

root /data/www/testweb;
index index.html index.htm;

ssl_certificate cert/test.cn.crt;
ssl_certificate_key cert/test.cn.key;

ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

location / {
root /data/www/testweb;
index index.html index.htm;
}
}

若暂时没有证书,可申请 腾讯云 90 天免费 SSL 证书

创建普通用户(避免 root 登录)

创建用户

1
2
3
4
5
6
7
adduser usertest
#查看是否添加成功
cat /etc/passwd | grep usertest
#显示结果如下
#usertest:x:1000:1000::/home/usertest:/bin/bas
#给用户设置密码
passwd usertest

赋予 sudo 权限

1
2
3
#为root用户添加/etcsudoers的文件读写执行权限
chmod 740 /etc/sudoers
vim /etc/sudoers

root ALL=(ALL) ALL 下添加:

1
usertest ALL=(ALL) ALL

恢复权限:

1
chmod 440 /etc/sudoers

配置 SSH 免密登录

本地生成 SSH 密钥(Windows)

在 Git Bash 中:

1
2
cd ~
ssh-keygen -t rsa

生成文件:

  • id_rsa(私钥)
  • id_rsa.pub(公钥)

上传公钥到服务器

1
ssh-copy-id -i ~/.ssh/id_rsa.pub usertest@服务器IP

以上命令执行完成之后,会在服务器usertest用户家目录生成一个.ssh的隐藏目录,里面包含一个authorized_keys文件,这个就是我们的公钥文件,只不过自动把文件名字改了,内容与id_rsa.pub是一样的,这时我们就可以在本地登录服务器了,登录测试:

1
2
3
ssh usertest@服务器IP
#如果要退出终端直接输入
logout

如果登录失败,请检查下usertest家目录下的.ssh文件夹和authorized_keys文件的访问权限

  • .ssh 文件夹访问权限应该为700
  • authorized_keys 文件的访问权限应为为600或644
1
2
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

使用ssh-copy-id命令,会自动设置好相应权限,如果使用其他方式请注意查看文件的访问权限,如果不是以上权限,请使用chmod自行修改

服务器端配置 Git 自动部署

安装 Git

1
2
yum install -y git
git --version

创建 Git 裸仓库

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

配置 post-receive 钩子

1
vim ~/hexo.git/hooks/post-receive

内容如下(建议手敲):

1
git --work-tree=/data/www/testweb -git-dir=/home/usertest/hexo.git checkout -f

赋予执行权限:

1
chmod +x ~/hexo.git/hooks/post-receive

把 Git 裸仓库里的最新代码,强制同步到网站目录中。

修改 Hexo deploy 配置

hexo根目录下的_config.yml

1
2
3
4
deploy:
type: git
repo: usertest@服务器公网IP:/home/usertest/hexo.git
branch: master

安装hexo-deployer-git

1
npm install hexo-deployer-git --save

测试自动部署

1
2
hexo clean
hexo g -d

常见问题整理

unable to create directory或文件没有更新

解决方案:

1
chmod -R o+w /data/www/testweb

部署成功但页面未更新

解决方案:

1
删除本地缓存目录:在客户端博客根目录找到.deploy_git 删除即可

Please tell me who you are

解决方案:

1
2
git config --global user.name "Your Name"
git config --global user.email "you@example.com"