运行环境
本地计算机:windows10
服务器:阿里云服务器 CentOS 8.0
博客框架:Hexo
工作原理
- 在本地计算机搭建 Hexo 环境并渲染
- Hexo 通过 hexo d 命令通过公钥登录服务器 git 用户
- 将 html 页面推送至服务器的 git 仓库
- 服务器将 html 页面拉取至网站的根目录下,实现博客自动部署
环境搭建
本地计算机环境搭建
安装 Node.js
进入官网下载:https://nodejs.org/zh-cn/
打开 cmd 验证 node.js 和 npm
1 | node -v |
npm 安装淘宝源
1 | npm install -g cnpm --registry=http://registry.npm.taobao.org |
验证 cnpm
1 | cnpm -v |
安装 Hexo
在磁盘中新建 blog 文件夹存放个人博客,并 cmd
1 | cnpm install -g hexo-cli |
验证 hexo
1 | hexo -v |
初始化 blog 文件夹
1 | hexo init |
启动 hexo 服务
1 | hexo server |
如果初始化失败,则进行
1 | npm install hexo-server --save |
通过 http://localhost:4000/访问个人博客
安装 Git
进入官网下载:https://git-scm.com/downloads
打开 git bash 进行初始化
1 | git config --global user.name "username" |
检验 git 设置
1 | git config --global --list |
服务器环境搭建
打开服务器端口
打开安全组,添加安全组规则
安装配置 Nginx
安装 nginx 并启动
1 | yum install nginx |
创建部署目录
1 | mkdir -p /home/www/hexo |
更改 nginx 的配置文件
1 | vim /etc/nginx/nginx.conf |
1 | server { |
重启 nginx
1 | systemctl restart nginx.service |
安装 Node.js
安装 node.js
1 | cd ~ |
检验 node.js 的安装
1 | node -v |
安装 Git
安装 git
1 | yum install git |
检验 git
1 | git --version |
创建 git 用户
创建 git 用户
1 | adduser git |
修改权限
1 | chmod 740 /etc/sudoers |
为 git 用户添加权限
1 | # Next comes the main part: which users can run what software on |
修改回原来的权限
1 | chmod 400 /etc/sudoers |
设置 git 用户密码
1 | sudo passwd git |
git 用户的 ssh 免密公钥登录
回到本地计算机,在桌面打开 git bash
1 | ssh-keygen -t rsa |
进入本地计算机的用户根目录(C:\Users\ASUS)的.ssh 目录,复制 id_rsa.pub 中的内容
回到服务器
1 | su git |
将 id_rsa.pub 中的内容复制进.ssh/authorized_keys
设置权限
1 | chmod 600 ~/.ssh/authorized_keys |
在本地计算机上使用 ssh 链接服务器
1 | ssh -v git@xxx.xxx.xxx.xxx(阿里云公网IP) |
配置 Git 仓库
在 git 用户下创建仓库
1 | su git |
向文件中写入
1 | #!/bin/sh |
赋予文件权限
1 | chmod +x ~/hexo.git/hooks/post-receive |
根据个人多次的错误,在这里重启 nginx
1 | systemctl start nginx.service |
重启服务器
本地计算机 hexo 配置
进入本地计算机的 blog 文件夹下,打开_config.yml 文件进行修改
1 | deploy: |
下载插件,打开 git bash
1 | npm install hexo-deployer-git --save |
使用 Hexo 生成、发布个人博客
1 | hexo clean && hexo g && hexo d |
通过公网 ip 访问博客主页
后续
我买了一个.icu 的域名作为个人域名使用(.icu 这种域名可太艹了哈哈哈哈哈
等服务器与域名备案完成后可以用域名替换公网 ip
针对博客主题等做优化