HEXO 部署到 阿里云ECS服务器
前言
本文将详细介绍我是如何将基于HEXO的博客部署到阿里云ECS服务器上。
- HEXO是一款高效的静态博客生成器,通过简单的配置和几个步骤即可将博客发布到互联网上。
- 阿里云ECS(Elastic Compute Service)提供了一个可弹性伸缩的计算服务,适合用于部署各种应用程序,包括静态网站。
本文是我在搭建HEXO博客时遇到的问题和解决方案,希望对你有帮助。
配置本地安装环境
- 安装Node.js
- 安装Git
Hexo 安装与配置
安装 Hexo
1
npm install -g hexo-cli
配置 Hexo(默认终端为:Git Bash)
在你的电脑上创建一个文件夹,例如:
hexo-blog
1
mkdir hexo-blog
进入
hexo-blog
文件夹1
cd hexo-blog
初始化 Hexo
1
hexo init
安装项目依赖
1
npm install
butterfly 主题安装与配置
安装butterfly主题
在你的
hexo-blog
文件夹下,执行以下命令GitHub 网络有时不稳定
1
git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly
Gitee 网络稳定
1
git clone -b master https://gitee.com/immyw/hexo-theme-butterfly.git themes/butterfly
安装渲染器
1
npm install hexo-renderer-pug hexo-renderer-stylus --save
配置butterfly主题
在
hexo-blog
文件夹中找到 _config.yml 文件,找到theme:
字段,修改如下内容1
theme: butterfly
在
themes
目录下找到butterfly
文件夹,找到其中的_config.yml
文件,复制其中的内容。在hexo-blog
文件夹下创建_config.butterfly.yml
文件,将复制的内容粘贴到_config.butterfly.yml
中。1
2# 复制并重命名 _config.yml 文件
mv ./themes/butterfly/_config.yml ./_config.butterfly.yml
butterfly主题版本更新
- 在主题目录
butterfly
文件夹下,执行以下命令1
git pull
- 在主题目录
Hexo 站点,本地预览
- 启动hexo本地服务器
- 在hexo-blog文件夹中执行以下命令在浏览器中访问
1
2
3
4
5
6# 清除缓存文件(静态文件),第一次不需要,但是从第2次开始建议每次运行hexo g && hexo s之前都使用一次
hexo clean
# 生成静态文件
hexo generate
# 本地服务器启动
hexo serverhttp://localhost:4000/
,即可看到预览效果。
- 在hexo-blog文件夹中执行以下命令
配置服务器环境
- 更新服务器系统与软件包
1
sudo apt update -y && sudo apt upgrade -y
创建 git 用户
直接使用 root 用户有点不安全,所以来创建一个git用户来作为hexo博客的唯一登录用户。
创建一个名为
git
的用户1
sudo adduser git
为
git
用户添加sudo权限修改 /etc/sudoers 权限,当前用户为roo:
1
chmod 740 /etc/sudoers
修改 /etc/sudoers 文件:
1
vim /etc/sudoers
按
i
键编辑,在root ALL=(ALL:ALL) ALL
后面添加一行:1
git ALL=(ALL:ALL) ALL
按
Esc
键退出编辑模式,输入:wq
保存退出。1
:wq
改回权限
1
chmod 440 /etc/sudoers
到此,
git
用户就可以执行sudo命令了。
安装与配置 Git
安装 Git
debian系统自带的git,版本不是最新。
1
sudo apt install git -y
Git 官方包
配置 SSH Key
切换到
git
用户:1
su git
创建 ~/.ssh 目录
1
mkdir ~/.ssh
创建 ~/.ssh/authorized_keys 文件
1
touch ~/.ssh/authorized_keys
复制本地(win10)下的id_rsa.pub内容到服务器的 ~/.ssh/authorized_keys 文件中。
1
vim ~/.ssh/authorized_keys
完成后按
Esc
键退出编辑模式,输入:wq
保存退出。切换到
root
用户1
su root
赋予权限
1
2chmod 600 /home/git/.ssh/authorized_keys
chmod 700 /home/git/.ssh
测试本地与服务器的ssh连接
打开Git bash,Server_Ip:服务器公网IP,输入:
1
ssh -v git@Server_Ip
有时会提示输入yes/no,输入yes即可。
如果最后出现以下信息则表示本地与服务器的ssh连接成功。
1
Welcome to Alibaba Cloud Elastic Compute Service !
配置仓库目录与网站目录
在服务器上创建仓库目录
切换到
root
用户1
su root
在
var
目录下新建repo
目录1
mkdir /var/repo
在 var/repo目录下创建空白git仓库
1
2cd /var/repo
git init --bare hexo.git赋予权限
1
2
3
4
5
6
7
8
9chown -R git:git /var/repo
chmod -R 755 /var/repo
```
- 在服务器上创建网站目录
- 切换到 `root` 用户
```bash
su root在 var 目录下新建
hexo
目录1
mkdir /var/hexo
赋予权限
1
2chown -R git:git /var/hexo
chmod -R 755 /var/hexo
创建 Git 钩子,用于自动部署
在 /var/repo/hexo.git/hooks 目录下创建 post-receive 文件
1
vim /var/repo/hexo.git/hooks/post-receive
进入编辑模式,输入以下内容:
1
2
git --work-tree=/var/hexo --git-dir=/var/repo/hexo.git checkout -f main赋予权限
1
2chown -R git:git /var/repo/hexo.git/hooks/post-receive
chmod +x /var/repo/hexo.git/hooks/post-receive
安装与配置 Node.js
建议安装与本地同一版本的 Node.js
安装 Node.js
切换到
root
用户1
su root
安装 nvm (nodejs版本管理)
1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
配置 nvm 环境变量
一般来说会自动添加到环境变量,只需运行以下命令即可1
source ~/.bashrc
添加 nodejs国内镜像源
1
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
配置 nodejs
安装与本地环境同一版本的 Nodejs,例如:20.17.0
1
nvm install 20.17.0
设置nvm 当前使用的Nodejs版本
1
nvm alias default 20.17.0
查看当前 Nodejs 版本
1
node -v
安装 Nginx
安装 Nginx
安装 先决条件
1
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
导入官方 nginx 签名密钥,以便 apt 可以验证软件包 真实性。 获取密钥:
1
2curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null验证下载的文件是否包含正确的密钥:
1
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
输出应包含完整的指纹
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
如下1
2
3pub rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key <signing-key@nginx.com>要为稳定的 nginx 软件包设置 apt 存储库, 运行以下命令
1
2
3echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list设置仓库固定以优先选择我们的软件包 发行版提供的:
1
2echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx运行以下命令以安装 Nginx:
1
sudo apt update && sudo apt install nginx
查看 Nginx 是否安装成功:
1
nginx -v
启动 Nginx:
1
2
3
4
5
6sudo systemctl start nginx
···
- 开机自启动 Nginx:
```bash
sudo systemctl enable nginx
配置 Nginx (let’s encrypt证书)
安装 Certbot:
安装 Certbot 和 Nginx 的依赖:
1
sudo apt install certbot python3-certbot-nginx
验证 Certbot 是否安装成功:
1
certbot --version
配置 Nginx
创建 Hexo 站点的nginx配置文件:
1
vim /etc/nginx/vhosts/hexo.conf
hexo.conf 添加内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27# HTTP server configuration
server {
listen 80;
server_name 域名;
# Redirect all HTTP traffic to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS server configuration
server {
listen 443 ssl;
server_name 域名;
root /var/hexo;
ssl_certificate /etc/letsencrypt/live/域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/域名/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
try_files $uri $uri/ =404;
}
}
编辑 nginx 配置文件
编辑 nginx.conf
1
vim /etc/nginx/nginx.conf
引用 hexo.conf, 在nginx配置文件中添加以下内容:
1
include /etc/nginx/vhosts/*.conf;
重新加载 nginx 配置文件:
1
nginx -s reload
Hexo 推送到服务器
deployer 安装与配置(本地环境)
安装 deployer
- 进入hexo-blog目录,安装部署器(Git Bash)
1
npm install hexo-deployer-git --save
- 进入hexo-blog目录,安装部署器(Git Bash)
配置 deployer
- 在hexo-blog根目录下打开_config.yml,找到 deployar字段,并修改成以下内容:
1
2
3
4deploy:
type: git
repo: git@服务器IP:/var/repo/hexo.git
branch: main
- 在hexo-blog根目录下打开_config.yml,找到 deployar字段,并修改成以下内容:
推送
- 进入hexo-blog目录,执行部署命令
1
hexo clean && hexo g && hexo d
- 进入hexo-blog目录,执行部署命令
全部结束
在这里,本文就讲述了我是如何在本地搭建一个Hexo博客并将其部署到服务器上。