006 VuePress Theme Hope 站点创建与部署
提示
本项目使用
nodejs v2版本,通过vuepress-theme-hope主题模块初始化项目,并使用pnpm模块管理命令。使用
Nginx反向代理工具部署静态资源。
资源链接参考:
Nodejs 官网: https://nodejs.org/zh-cn
Vue 官网: https://cn.vuejs.org/
VuePress 官网: https://vuepress.vuejs.org/zh/
VuePress Theme Hope 官网: https://theme-hope.vuejs.press/zh/
Nginx 官网: https://nginx.org/
开发环境准备
安装 nodejs 安装包
- Windows 平台
支持通过命令行或独立安装程序安装,参考官方文档: https://nodejs.org/zh-cn/download
- Linux 平台
提示
支持通过 dnf 源安装 nodejs,或者在 安装包镜像源 中单独下载安装包安装。
dnf install nodejs安装完成后,模块管理命令 npm 会默认携带,使用以下命令确认 nodejs 安装成功:
# 1. node
node -v
# 2. npm
npm -v为 npm 配置模块源,并安装 pnpm 模块管理命令
提示
nodejs 安装包源和模块源不同,当前是为 npm 配置模块源,安装 pnpm。
npm、pnpm、pnpm 均是 nodejs 生态系统中的包管理器命令。当前 pnpm 是首选。
配置 npm 模块源
npm config set registry https://npmmirror.comnpm config set registry https://tsinghua.edu.cn验证 npm 源配置是否成功:
npm config get registry示例输出
[root@openEuler ~]# npm config get registry
https://npmmirror.com
[root@openEuler ~]#通过 npm 全局安装 pnpm 命令
npm install -g pnpm验证 pnpm 安装是否成功:
pnpm -v单独为 pnpm 配置模块源,避免未成功读取 npm 源
pnpm config set registry https://npmmirror.compnpm config set registry https://tsinghua.edu.cn验证 pnpm 源配置是否成功:
pnpm config get registry示例输出
[root@openEuler ~]# pnpm config get registry
https://npmmirror.com
[root@openEuler ~]#禁用 SSL 校验
pnpm config set strict-ssl falsenpm config set strict-ssl false初始化 VuePress-Theme-Hope 项目
- 初始化
pnpm create vuepress-theme-hope my-blog- 若在特殊环境,推荐修改仅启动本地服务
{
"scripts": {
"docs:dev": "vuepress-vite dev src --host 127.0.0.1"
}
}或 通过启动命令行参数指定:
pnpm docs:dev --host 127.0.0.1- 安装默认依赖并启动开发服务
pnpm install
pnpm docs:dev部署
通过 GitHub Actions 自动构建并部署
将项目托管在 GitHub 平台,或通过 atomgit 将项目同步托管到 GitHub 平台
在服务器上创建专用于站点资源管理的普通用户
www,并修改目录权限:# 1. 创建一个名为 www 的普通用户(禁止其通过密码登录,提高安全性) useradd -m -s /bin/bash www # 2. 将 Nginx 静态站点目录的所有权彻底交给 www 用户 chown -R www:www /usr/share/nginx/html/ # 3. 授予 www 用户该目录的读写和执行权限 chmod -R 755 /usr/share/nginx/html/配置 Nginx 读取站点目录资源。有以下两种方案:
方案一(推荐)将运行网站的默认用户(如 Nginx 默认使用
nginx用户)添加到www用户组:# 将 nginx 用户添加到 www 组中 usermod -aG www nginx # 重启 Nginx 生效 systemctl restart nginx方案二让 Nginx 默认以
www用户运行:user www; # 修改为 www重启 Nginx 服务:
systemctl restart nginx
在服务器上登陆普通用户
www并生成(或使用现有的)SSH 密钥对:ED25519 算法ssh-keygen -t ed25519 -C "github-actions"RSA 算法ssh-keygen -t rsa -b 4096 -C "github-actions"将公钥追加到服务器的
www用户授权文件中:ED25519 算法cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keysRSA 算法cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys复制私钥
~/.ssh/id_ed25519或~/.ssh/id_rsa的全部文本内容,继续下一步在 GitHub 平台配置安全环境变量,避免把私钥暴露在公开代码中
进入 GitHub 仓库 -> Settings -> Secrets and variables -> Actions
点击 New repository secret,添加以下三个变量:
SERVER_HOST: 待部署站点的 ECS 服务器公网 IPSERVER_USER: 服务器用户名,此处使用普通用户www(和第2步保持一致)SERVER_SSH_KEY: 粘贴刚才复制的私钥全部内容
编写 GitHub Actions 任务流配置文件:
name: Deploy VuePress to ECS on: push: branches: - main # 仅在 main 分支有代码推送时触发 jobs: build-and-deploy: runs-on: ubuntu-latest steps: # 1. 检出代码 - name: Checkout Code uses: actions/checkout@v4 # 2. 安装 Node.js - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: 20 # 3. 安装 pnpm - name: Setup PNPM uses: pnpm/action-setup@v3 with: version: 8 # 4. 依赖缓存(加速后续构建) - name: Get pnpm store directory shell: bash run: | echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV - name: Cache pnpm modules uses: actions/cache@v4 with: path: ${{ env.STORE_PATH }} key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} restore-keys: | ${{ runner.os }}-pnpm-store- # 5. 云端安装依赖并构建打包 - name: Install and Build run: | pnpm install pnpm docs:build # 6. 将打包好的 dist 目录通过 SSH 传输到你的 ECS 服务器上 - name: Deploy to ECS Server uses: easingthemes/ssh-deploy@main with: # 授权凭证 SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} # 传输参数:传递给底层 rsync 命令的参数 # -a (archive): 归档模式,保持文件的修改时间、权限、软链接等属性不变 # -v (verbose): 详细输出模式,在日志中打印传输了哪些文件 # -z (compress): 传输时压缩数据,节省带宽、加快传输速度 # --delete: 极其重要!如果 GitHub 这边删除了某个旧网页,服务器对应的目标目录里也会同步删除,保持两边完全一致,防止垃圾文件堆积 ARGS: "-avz --delete" # 源目录 - 编译生成的静态资源目录 SOURCE: "src/.vuepress/dist/" # 目标服务器 IP REMOTE_HOST: ${{ secrets.SERVER_HOST }} # 登录用户名 REMOTE_USER: ${{ secrets.SERVER_USER }} # 目标目录:你的 ECS 服务器上 Nginx 存放网页的绝对路径,传过去的文件会直接平铺在这个目录下 TARGET: "/usr/share/nginx/html/"保存 GitHub Actions 配置文件并推送到 GitHub,将自动执行自动化构建、推送静态资源内容到 ECS 服务器上