快速开始

通过 Serverless Website 组件快速构建一个 Serverless Hexo 站点

1. 安装

安装前提:

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

如您未安装上述应用程序,可以参考排Hexo 安装说明

安装 Serverless Framework

  1. $ npm install -g serverless

安装 Hexo

  1. $ npm install -g hexo-cli

安装 Hexo 完成后,请执行下列命令,Hexo 将会在指定文件夹中新建所需要的文件。

  1. $ hexo init hexo # 生成hexo目录
  2. $ cd hexo
  3. $ npm install

新建完成后,指定文件夹的目录如下:

  1. .
  2. ├── _config.yml
  3. ├── package.json
  4. ├── scaffolds
  5. ├── source
  6. | ├── _drafts
  7. | └── _posts
  8. └── themes

安装完成后,可以通过hexo g命令生成静态页面

  1. $ hexo g # generate

注:如果希望在本地查看效果,也可以运行下列命令,通过浏览器访问 localhost:4000 查看页面效果。

  1. $ hexo s # server

2. 配置

hexo目录下,创建serverless.yml文件,在其中进行如下配置

  1. $ touch serverless.yml
  1. # serverless.yml
  2. myWebsite:
  3. component: '@serverless/tencent-website'
  4. inputs:
  5. code:
  6. src: ./localhexo/public # Upload static files generated by HEXO
  7. index: index.html
  8. error: index.html
  9. region: ap-guangzhou
  10. bucketName: my-bucket

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

  1. .
  2. ├── .serverless
  3. ├── hexo
  4. | ├── public
  5. | ├── ...
  6. | ├── _config.yml
  7. | ├── ...
  8. | └── source
  9. └── serverless.yml

3. 部署

通过sls命令进行部署,并可以添加—debug参数查看部署过程中的信息

如您的账号未登陆注册腾讯云,您可以直接通过微信扫描命令行中的二维码进行授权登陆和注册。

  1. $ serverless --debug
  2. DEBUG Resolving the template's static variables.
  3. DEBUG ─ Collecting components from the template.
  4. DEBUG ─ Downloading any NPM components found in the template.
  5. DEBUG ─ Analyzing the template's components dependencies.
  6. DEBUG Creating the template's components graph.
  7. DEBUG ─ Syncing template state.
  8. DEBUG ─ Executing the template's components graph.
  9. DEBUG Starting Website Component.
  10. Please scan QR code login from wechat
  11. Wait login...
  12. Login successful for TencentCloud
  13. DEBUG Preparing website Tencent COS bucket my-bucket-1250000000.
  14. DEBUG Deploying "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  15. DEBUG "my-bucket-1250000000" bucket was successfully deployed to the "ap-guangzhou" region.
  16. DEBUG Setting ACL for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  17. DEBUG Ensuring no CORS are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  18. DEBUG Ensuring no Tags are set for "my-bucket-1250000000" bucket in the "ap-guangzhou" region.
  19. DEBUG Configuring bucket my-bucket-1250000000 for website hosting.
  20. DEBUG Uploading website files from D:\hexotina\localhexo\public to bucket my-bucket-1250000000.
  21. DEBUG Starting upload to bucket my-bucket-1250000000 in region ap-guangzhou
  22. DEBUG Uploading directory D:\hexotina\localhexo\public to bucket my-bucket-1250000000
  23. DEBUG Website deployed successfully to URL: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com.
  24. myWebsite:
  25. url: https://my-bucket-1250000000.cos-website.ap-guangzhou.myqcloud.com
  26. env:
  27. 13s » myWebsite » done

访问命令行输出的 website url,即可查看您的 Serverless Hexo 站点

注:如果希望更新 hexo 站点中的文章,需要在本地重新运行hexo g进行生成静态页面,再运行serverless更新到页面

4. 移除

可以通过以下命令移除 hexo 网站

  1. $ sls remove --debug
  2. DEBUG Flushing template state and removing all components.
  3. DEBUG Starting Website Removal.
  4. DEBUG Removing Website bucket.
  5. DEBUG Removing files from the "my-bucket-1250000000" bucket.
  6. DEBUG Removing "my-bucket-1250000000" bucket from the "ap-guangzhou" region.
  7. DEBUG "my-bucket-1250000000" bucket was successfully removed from the "ap-guangzhou" region.
  8. DEBUG Finished Website Removal.
  9. 6s » myWebsite » done

账号配置(可选)

当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 .env 文件

  1. $ touch .env # 腾讯云的配置信息

.env 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存

如果没有腾讯云账号,可以在此注册新账号

如果已有腾讯云账号,可以在API 密钥管理中获取 SecretIdSecretKey.

  1. # .env
  2. TENCENT_SECRET_ID=123
  3. TENCENT_SECRET_KEY=123