安装

安装node.js

安装git

命令行安装cnpm

1
2
3
4
5
# 命令
npm install -g cnpm --registry==https://registry.npm.taobao.org

# 安装后验证
cnpm -v

命令行安装hexo

1
2
3
4
5
# 命令
cnpm install -g hexo-cli

# 安装后验证
hexo -v

hexo初始化

1
2
3
4
5
6
7
8
9
10
11
# 初始化hexo
hexo init

# 没有实现创建文件夹也可以使用下面的命令
hexo init myblog

# 在public文件夹生成相关html文件
hexo g

# 启动服务器
hexo s

启动后,打开浏览器访问 http://localhost:4000 即可看到内容.

将博客部署到 github 个人主页上

  • 安装 hexo-deployer-git 插件
1
npm install hexo-deployer-git --save   # 安装插件
  • 修改主目录下的 _config.yml文件
1
2
3
4
deploy:
type: git
repository: 'github个人项目链接地址'
branch: master
  • 推送
1
hexo clean;hexo g;hexo d

搭建好hexo后,在本地能够正常访问。

使用git上传到github后,发现能访问页面,但是不能加载CSS样式文件和JQuery文件.

解决方法参考 (177条消息) hexo+Github搭建博客,能访问但无法加载css文件_StarryaSky的博客-CSDN博客

主题选择

选择不同主题进行部署及魔改

部署腾讯云

部署策略:宝塔linux + Git

安装宝塔linux

采用腾讯云宝塔linux版本,用服务器模板,内置安装,省时省力,十分简单

image-20230420141604408

安装git

旧版本足够使用,无需安装最新版。

1
yum -y install git

创建git用户

  • 创建git用户
1
adduser git
  • 获取权限
1
2
chmod 740 /etc/sudoers
vim /etc/sudoers

i 键进入文件的编辑模式,按向下键找到如下字段

1
root    ALL=(ALL)       ALL

在其后面增加一句:

1
git     ALL=(ALL)       ALL

Esc 键退出编辑模式,输入:wq 保存退出。(先输入:,然后输入wq回车)

  • 退回权限
1
chmod 400 /etc/sudoers

配置秘钥

  • 生成秘钥
1
ssh-keygen -t rsa
  • 将密钥保存在服务器(之前有密钥的直接复制就可以)

id_rsa.pub里面的密钥复制,在服务器运行下面命令,创建.ssh文件夹

1
2
su git
mkdir ~/.ssh

创建.ssh/authorized_keys文件,打开authorized_keys文件并将刚才在本地机器复制的内容拷贝其中并保存

1
vim ~/.ssh/authorized_keys

i进入编辑模式粘贴完按 Esc 键退出编辑模式,输入:wq 保存退出。(先输入:,然后输入wq回车)

  • 修改权限
1
2
3
chmod 755 ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
  • 测试本地连接服务器

在本地电脑git bash here

1
2
//yourIp为远程服务器的ip地址
ssh -v git@yourIp //yourIp为你的服务器ip

创建git仓库

  • 切换到root用户,创建一个目录用于存储网站的根目录
1
su root
  • 创建网站的根目录
1
mkdir /www/wwwroot/***
  • 给予权限
1
chown git:git -R /www/wwwroot/***

自动化部署

  • 获取root权限
1
su root
  • 建立git仓库
1
2
cd /www/wwwroot/***
git init --bare blog.git
  • 修改blog.git权限
1
chown git:git -R blog.git
  • /home/hexo/blog.git 下,有一个自动生成的 hooks 文件夹,我们创建一个新的 git 钩子 post-receive,用于自动部署。
1
vim blog.git/hooks/post-receive
  • i 键进入文件的编辑模式,在该文件中添加两行代码(将下边的代码粘贴进去),指定 Git 的工作树(源代码)和 Git 目录
1
2
#!/bin/bash 
git --work-tree=/www/wwwroot/*** --git-dir=/www/wwwroot/***/blog.git checkout -f

Esc 键退出编辑模式,输入:wq 保存退出。(先输入,然后输入wq回车)

  • 修改文件权限,使得其可执行。
1
chmod +x /www/wwwroot/***/blog.git/hooks/post-receive

添加站点

image-20230420151946465

配置_config.yml

1
2
3
4
deploy:
type: git
repo: root@***(服务器ip,内网外网都行):/www/wwwroot/***/blog.git #仓库地址
branch: master #分支
  • 多仓库配置
1
2
3
4
5
6
7
deploy:
- type: git
repo: root@***(服务器ip,内网外网都行):/www/wwwroot/***/blog.git #仓库地址
branch: master #分支
- type: git
repo: # github仓库地址
branch: master

推送

  • 部署
1
2
3
hexo clean
hexo g
hexo d

一键三连,输入hexo d后,推送到服务器需输入服务器密码、

输入密码不会有显示,输完回车就可以

  • 如果出现bash: git-receive-pack: command not found,则运行:
1
sudo ln -s /usr/local/git/bin/git-receive-pack  /usr/bin/git-receive-pack