Hexo + Serverless Framework,簡單三步搭建你的個人博客


很多人都想擁有自己的個人博客,還得看起來漂亮、酷酷的。尤其對開發者來說,不僅可以分享技術(裝)心得(逼),面試的時候還能成為加分。這里介紹兩款好用的神器,不用忙前(前端)忙后(后端),簡單3min即可搞定,本文免費分享給大家。

PS:不會寫代碼?沒有備案的域名?沒有服務器?在這里,這些都不是事兒!

工具介紹

  • Serverless Framework:Serverless Framework 是業界非常受歡迎的無服務器應用框架,開發者無需關心底層資源即可部署完整可用的 Serverless 應用架構。
  • Hexo:Hexo 是一個快速、簡潔且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在幾秒內,即可利用靚麗的主題生成靜態網頁。

使用 Serverless Framework 部署一個 Hexo 網站只需三步::安裝與初始化 → 配置 yml 文件 → 部署

安裝與初始化

簡單三步,即可通過 Serverless Website 組件快速構建一個 Serverless Hexo 站點。

首先確保系統包含以下環境:

  • Node.js (Node.js 版本需不低於 8.6,建議使用 Node.js 10.0 及以上版本)
  • Git

如未安裝上述應用程序,可以參考安裝說明

1. 安裝 Serverless Framework

$ npm install -g serverless

2. 安裝 Hexo

$ npm install -g hexo-cli

3. 初始化項目

$ hexo init hexo   # 生成hexo目錄
$ cd hexo
$ npm install

初始化完成后,hexo 文件夾的目錄如下:

.
├── _config.yml
├── package.json
├── scaffolds
├── source
|   ├── _drafts
|   └── _posts
└── themes

4. 生成本地博客頁面

hexo g   # generate

可以通過 hexo g 命令生成靜態頁面,如果希望在本地查看效果,也可以運行下列命令,通過瀏覽器訪問 http://localhost:4000 查看頁面效果。

hexo s   # server

配置 yml 文件

項目目錄下,創建 serverless.yml 文件

cd.> serverless.yml

將以下內容寫入上述的 yml 文件里:

# serverless.yml

myWebsite:
  component: "@serverless/tencent-website"inputs:
    code:
      src: ./public # Upload static files generated by HEXO
      index: index.html
      error: index.html
    region: ap-guangzhou
    bucketName: my-bucket-hexo

配置完成后,文件目錄如下:

.
├── .serverless
├── hexo
|   ├── public
|   ├── ...
|   ├── _config.yml
|   ├── ...
|   └── source
└── serverless.yml

部署

通過 sls 命令進行部署,並可以添加 --debug 參數查看部署過程中的信息

如您的賬號未登陸注冊騰訊雲,您可以直接通過微信掃描命令行中的二維碼進行授權登陸和注冊。

PS serverless --debug

  DEBUG ─ Resolving the template's static variables.
  DEBUG ─ Collecting components from the template.
  DEBUG ─ Downloading any NPM components found in the template.
  DEBUG ─ Analyzing the template's components dependencies.
  DEBUG ─ Creating the template's components graph.
  DEBUG ─ Syncing template state.
  DEBUG ─ Executing the template's components graph.
  DEBUG ─ Starting Website Component.

Please scan QR code login from wechat
Wait login...
Login successful for TencentCloud
  DEBUG ─ Preparing website Tencent COS bucket my-bucket-1250000000.
  DEBUG ─ Deploying "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  DEBUG ─ "my-bucket-1250000000" bucket was successfully deployed to the "ap-guangzhou" region.
  DEBUG ─ Setting ACL for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  DEBUG ─ Ensuring no CORS are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  DEBUG ─ Ensuring no Tags are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  DEBUG ─ Configuring bucket my-bucket-1250000000 for website hosting.
  DEBUG ─ Uploading website files from D:\hexotina\localhexo\public to bucket my-bucket-1250000000.
  DEBUG ─ Starting upload to bucket my-bucket-1250000000 in region ap-guangzhou
  DEBUG ─ Uploading directory D:\hexotina\localhexo\public to bucket my-bucket-1250000000
  DEBUG ─ Website deployed successfully to URL: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com.

  myWebsite:
    url: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com
    env:

  13s » myWebsite » done

訪問命令行輸出的 website url,即可查看屬於自己的 Serverless Hexo 站點。

image

如果需要更新 hexo 站點中的文章,只需要在本地重新運行 hexo g 更新本地頁面,再運行 sls 就可以直接更新到 Hexo 網站啦~

小結

本文簡單展示了如何利用 Serverless Framework 創建一個個人博客,Hexo 擁有豐富的插件系統,大家可以基於 Serverless Framework 和 hexo 開發更多個性化功能,如自定義 Themes、博文置頂、添加小圖標等。這兩個工具結合使用,開發方便部署快捷,非常適合想要快速搭建靜態網站的開發者。

參考:

大家也可以訪問:Serverless 中文技術社區,可以在 最佳實踐 里查看更多關於 Serverless 應用的開發!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM