无需花钱购买域名服务器!使用 VuePress + Github 30分钟搭建属于自己的博客网站
前言
GitHub Pages 提供免费全球加速的服务器资源,VuePress 将 Markdown 变成艺术品级的网页,仅需 30 分钟,你便可以像提交代码一样发布文章,过程完全免费。
一、本地搭建
准备工具:电脑上需要提前装好 Node.js
第一步:新建一个文件夹
# 打开电脑的「终端」 # 输入命令: mkdir vuepress-starter && cd vuepress-starter
翻译:创建一个叫 vuepress-starter
的文件夹,并进入它
第二步:初始化项目
# 输入命令: yarn init # 或者用 npm init
操作提示:直接按回车键确认所有选项
第三步:安装 VuePress
# 输入命令: yarn add -D vuepress # 或者用 npm install -D vuepress
翻译:把 VuePress 这个建站工具安装到你的文件夹里
第四步:创建文档
# 输入命令: mkdir docs && echo '# Hello VuePress' > docs/README.md
*翻译:在文件夹里新建一个 docs
子文件夹,并创建主页文件 README.md
*
第五步:修改配置文件
打开 package.json
文件,在 scripts
里添加:
{ "scripts": { "docs:dev": "vuepress dev docs", // 启动开发模式 "docs:build": "vuepress build docs" // 生成最终网站 } }
第六步:启动网站
# 输入命令: yarn docs:dev # 或者用 npm run docs:dev
效果:浏览器会自动打开 http://localhost:8080,你会看到一个写着 "Hello VuePress" 的网站!
🔥 实时更新
现在你可以:
- 用 IDE 或记事本打开
docs/README.md
- 把
# Hello VuePress
改成# 我的第一个网站
- 保存文件 → 网站会自动刷新!
跟着做下来,你的第一个文档网站就煮好啦! 🎉
二、基础配置
在 docs
文件夹里新建 .vuepress
文件夹并添加 config.js
,所有 VuePress 相关的文件都会被放在这里。
此时你的项目结构可能是这样:
. ├─ docs │ ├─ README.md │ └─ .vuepress │ └─ config.js └─ package.json
在 config.js
里写:
module.exports = { title: 'VuePress 文档', description: 'VuePress + Github 30分钟搭建属于自己的博客网站' }
👉 效果:搜索引擎更容易找到你的网站(SEO)
三、添加导航菜单
修改 config.js
如下:
module.exports = { title: 'VuePress 文档', description: 'VuePress + Github 30分钟搭建属于自己的博客网站', themeConfig: { nav: [ {text: '首页', link: '/'}, { text: 'CodeFish的博客', items: [ {text: 'GitHub', link: 'https://github.sheincorp.cn/'}, {text: 'CSDN', link: 'https://blog.csdn.net/weixin_41105242'}, {text: '掘金', link: 'https://juejin.cn/'} ] } ] } }
👉 效果:网站右上角出现导航菜单
四、添加侧边栏
在 docs
文件夹里新建 blog
文件夹并添加一些 .md 文档,添加后的目录如下:
. ├─ docs │ ├─ README.md │ └─ .vuepress │ └─ config.js | └─ blog | └─ FirstMenu.md | └─ SecondMenu.md | └─ ThirdMenu.md └─ package.json
修改 config.js
如下:
module.exports = { title: 'VuePress 文档', description: 'VuePress + Github 30分钟搭建属于自己的博客网站', themeConfig: { nav: [...], sidebar: [ { title: '首页', path: '/' }, { title: "菜单一", path: '/blog/FirstMenu', collapsable: false, // 不折叠 children: [ { title: "菜单二", path: "/blog/SecondMenu" }, { title: "菜单三", path: "/blog/ThirdMenu" } ], } ] } }
👉 效果:左侧出现清晰的菜单目录
五、部署到 Github Pages
我们的博客就算是正式做好了,接下来我们部署到免费的 Github Pages 上。
首先在 Github 上新建一个仓库,这里我取的仓库名为 example-blog
下一步,我们需要在 config.js
对应添加一个 base
路径配置:
module.exports = { // 路径名为 "/<REPO>/" base: '/example-blog/', //... }
最终的 config.js
文件内容为:
module.exports = { title: 'VuePress 文档', description: 'VuePress + Github 30分钟搭建属于自己的博客网站', base: '/example-blog/', themeConfig: { nav: [ {text: '首页', link: '/'}, { text: '我的博客', items: [ {text: 'GitHub', link: 'https://github.sheincorp.cn/'}, {text: 'CSDN', link: 'https://blog.csdn.net/weixin_41105242'}, {text: '掘金', link: 'https://juejin.cn/'} ] } ], sidebar: [ { title: '首页', path: '/' }, { title: "菜单一", path: '/blog/FirstMenu', collapsable: false, // 不折叠 children: [ { title: "菜单二", path: "/blog/SecondMenu" }, { title: "菜单三", path: "/blog/ThirdMenu" } ], } ] } }
然后我们在项目 vuepress-starter
目录下建立一个脚本文件 deploy.sh
注意修改一下对应的用户名和仓库名:
#!/usr/bin/env sh # 确保脚本抛出遇到的错误 set -e # 生成静态文件 yarn run docs:build # 进入生成的文件夹 cd docs/.vuepress/dist git init git add -A git commit -m 'deploy' git branch -M main git push -f **********:HonorSong/example-blog.git main:pages cd -
然后命令行切换到 vuepress-starter
目录下,执行:
# 输入命令: sh deploy.sh
项目就会开始构建,然后提交到远程仓库,注意这里提交到了 pages
分支,我们查看下对应仓库分支的代码:
我们可以在仓库的
Settings -> Pages
中看到最后的地址:
像我最后生成的地址就是 ****
至此,我们完成了 VuePress 和 Github Pages 的部署。
此后每一次文章内容的更新,配置的更新,仅需要重新执行 sh deploy.sh
命令重新部署,即可对线上页面进行更新。