环境
1. CentOS7 服务器: nginx + git
2. 域名(可以没有)
3. 本地ArchLinux笔记本: nodejs + git + hexo
服务端教程
- 更新系统
yum upgrade
- 服务端git部分
# 安装git
yum install -y git
# 新建一个git用户
useradd -m git
# 更改git用户权限
visudo
# 找到 root ALL=(ALL) ALL 这一行,在下边添加一行:
git ALL=(ALL) ALL
# 创建git用户的密码
sudo passwd git
# 切换用户并cd到/home/git/目录下
su git
cd ~
# 生成一个名为hexo.git的裸仓库
git init --bare hexo.git
# 新建一个git hook钩子
vim /home/git/hexo.git/hooks/post-receive
# 输入以下内容
#!/bin/bash
git --work-tree=/usr/share/nginx/html/hexo --git-dir=/home/git/hexo.git checkout -f
# 给post-receive加执行权限
chmod +x /home/git/hexo.git/hooks/post-receive
# 退出git用户
exit
- 服务端nginx配置
# 安装nginx
yum install -y nginx
# 创建网站根目录
mkdir /usr/share/nginx/html/hexo
# 修改配置文件/etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
# 找到如下部分
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.lovezy.online; # 这是你的域名,或者服务器IP地址
return 301 https://$server_name$request_uri;
root /usr/share/nginx/html/hexo; # 这是网站根目录
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
# 启动nginx并设置自启动
systemctl start nginx
systemctl enable nginx
本地教程
- 安装nodejs
NodeJS官网
下载安装后终端输入以下命令验证,返回版本号则安装成功
# 验证node版本
node -v
# 验证npm版本
npm -v
# 换源
npm config set registry https://registry.npm.taobao.org
- 安装git
下载安装,然后验证版本
Git for Windows官网
# 验证版本号
git -v
# 填写github用户信息
git config --global user.email "你的github邮箱"
git config --global user.name "你的github用户名"
- 配置git免密登录
# 本地终端生成秘钥,三次回车
ssh-keygen -t rsa
# 复制秘钥到服务器,命令执行期间需要输入服务器git用户的登录密码
ssh-copy-id git@X.X.X.X
# 测试免密登录,不用输入密码即可
ssh git@X.X.X.X
- 安装hexo
# npm全局安装hexo
npm install hexo-cli -g
# 在你想要的位置新建一个文件夹,用来存放hexo博客
mkdir /home/lwf/hexo
cd /home/lwf/hexo
# 初始化博客
hexo init
# 安装hexo部署插件
npm install hexo-deployer-git --save
# 安装hexo本地运行插件
npm install hexo server
- 配置hexo
# 进入hexo文件夹
cd /home/lwf/hexo
# 生成博客
hexo g
# 本地预览博客
hexo s
# 访问http://localhost:4000即可预览,Ctrl+C停止本地预览
# 修改hexo目录下的_config.yml文件
# 找到如下内容
deploy:
type: git
repo: git@X.X.X.X:/home/git/hexo.git # X.X.X.X改成你的服务器IP
branch: master
message:
# 发布博客到服务器
hexo d