serverlesslife.cn The story behind the launch ～
In this paper, the source ： ServerlessLife official account
2021 At the beginning of year , Use Serverless Framework Tencent cloud in the deployment of a personal blog ：serverlesslife.cn.
On the whole, I realized Serverless Some of the conveniences ：
No need to manage or operate the server
Pay per use （ There is a free quota ）
Also experienced Serverless Framwwork CLI The convenience of ： It greatly reduces the operational complexity , The user experience is better than the console .
In the whole practice , I learned a lot of new knowledge , Understand some of the logic behind , This article will share them .
This practice involves Tencent cloud services
COS（ Object storage ）
DNSPod Domain name registration 、 Domain name filing 、 Domain name resolution
CDN（ Content distribution network ）
Domain name purchase and filing
2021/01/21 stay DNSPod（2011 It was acquired by Tencent in ） Bought a domain name serverlesslife.cn And real name authentication .
After buying a domain name , On Chinese mainland , To use domain names to provide services , We also need to record the domain name .
2021/1/31 stay 「 Tencent cloud website for the record 」 I submitted an application for filing on a small program ;
After two rounds of auditing （ Tencent cloud audit + Reviewed by the authority ） after , stay 8 The day after 2021/02/07 Approved by .
Serverless Framework brief introduction
Serverless Framework yes serverless.com A popular Serverless frame , It can be Serverless function / Applications deployed to different cloud vendors Serverless platform . In China, Tencent cloud and serverless.com Achieve strategic cooperation , It's a lot of customization , Made a lot of components , Make it easy to Serverless function / Application deployment to Tencent cloud .
Use Hugo Build a personal blog
To build a personal independent blog , There are many open source site building tools available , such as ：WordPress、Hexo、Jekyll、Hugo wait , be too numerous to enumerate .
Because I used it before Hugo, And I like it more , It's used here Hugo To build a personal blog .
Hugo Is written by a Go Language implementation of static website generator . Simple 、 Easy to use 、 Efficient 、 Easy to expand 、 Rapid deployment .
Hugo Extensibility means that it has a wide range of topics to choose from , This is one of the reasons why I like it , Everyone can choose their favorite theme .
stay Hugo After the site is initialized , The theme is chosen from many themes ： LoveIt（ What a nice name ！）.
Themes also have many parameters , You can configure it according to your own needs .
After the basic framework is built , If you want to write a blog , Just add Markdown Files and related static resource files .
Hugo Support local real-time preview , You can write markdown file , You can see the effect in real time through the browser .
Of course, deploy to the production server , Need to compile , There is only one task to compile into a site hugo Orders can be done .
The site source code is hosted in GitHub On ： https://github.com/serverlesslife-cn/serverlesslife
Use at the same time GitHub Actions Synced the code to Gitee： https://gitee.com/serverlesslife/serverlesslife
After the site is compiled , Use Serverless Framework CLI You can deploy it to Tencent cloud .
Serverless Framework CLI Need a profile serverless.yml, The configuration file is as follows ：
component: website # ( Required ) quote component The name of , What is currently used is tencent-website Components name: serverlesslife-2021 # ( Required ) The website The name of the instance created by the component app: serverlesslife-2021 # ( Optional ) The website apply name stage: prod # ( Optional ) Used to distinguish environmental information , The default value is dev inputs: src: src: ./public index: index.html region: ap-guangzhou bucketName: serverlesslife-2021 protocol: https
among public Directory is hugo The directory of the site file after compilation .
Use serverless deploy Command to deploy , If there's no local .env To configure secretid and secretkey, You need to use wechat to scan and log in .
During deployment , Will be in COS Create a bucket And will public Upload the files in the directory to this bucket in , Then it will generate an access address of Tencent cloud level 4 domain name . Among them bucket The prefix is serverlesslife-2021, The tool will automatically add Tencent cloud account APPID As a suffix .
Serverless Framework Under the console , There will be an app called serverlesslife-2021 Application , This application will have an access address of Tencent cloud level 4 domain name , As shown below ：
stay COS Bucket list page for object storage , You can see that there is a name called serverlesslife-2021-1259061164 Of bucket, As shown below ：
Custom domain name + SSL certificate + Automatically refresh CDN
To configure 「 Custom domain name + SSL certificate + Automatically refresh CDN」 Still need to be in serverless.yml Add some configuration information in , Whole serverless.yml The documents are as follows ：
component: website # ( Required ) quote component The name of , What is currently used is tencent-website Components name: serverlesslife-2021 # ( Required ) The website The name of the instance created by the component app: serverlesslife-2021 # ( Optional ) The website apply name stage: prod # ( Optional ) Used to distinguish environmental information , The default value is dev inputs: src: src: ./public index: index.html region: ap-guangzhou bucketName: serverlesslife-2021 protocol: https hosts: - host: serverlesslife.cn autoRefresh: true # Turn on auto CDN Refresh , Used to quickly update and synchronize the site content displayed in the domain name onlyRefresh: false # It is recommended that after the first deployment , Configure this parameter to true, That is to ignore the others CDN To configure , Just refresh https: switch: on http2: on certInfo: certId: 'kBM9GLPt'
The above configuration file supports the configuration of multiple domain names , Other information can be configured under each domain name , Such as SSL certificate ID、 Automatically refresh CDN etc. .
When using a custom domain name , If you need to configure SSL certificate , Then you have to use CDN, Because in CDN The certificate can be configured under .
SSL Certificates need to be issued in advance SSL Certificate console application , Here you apply for a free certificate , The certificate is valid for one year , After successful application, there will be a ID：kBM9GLPt, As shown below ：
After configuring the domain name information ,Serverless Framework A lot of things have been done after deployment , Greatly simplifies the configuration cost .
If not Serverless Framework Words , Then you need to configure it back and forth in multiple places without cloud services .
Let's take a look at the configuration 「 Custom domain name + SSL certificate 」 behind ,Serverless Framework What has been done ：
1、CDN Under the console , Certificate management —> Configure certificate , Associate the domain name with the certificate , As shown below ：
2、CDN Under the console , Domain name management —> Add domain name , A new record has been added , by serverlesslife.cn It turns on static acceleration , take COS Static sites as source sites , And will generate CNAME：
3、COS Under the console , Click in serverlesslife-2021-1259061164 bucket , In the domain name transmission and management —> Customize CDN Speed up the domain Office , You'll also see a record ：
here , To access the domain name normally , You also need to manually configure the domain name resolution ： open DNSPod Console , by serverlesslife.cn Add one CNAME Record , The recorded value is CDN Generated CNAME：serverlesslife.cn.cdn.dnsv1.com.
Be careful ： After the first deployment , take onlyRefresh Parameter is configured as true, That is to ignore the others CDN To configure , Just refresh , Otherwise, the deployment time will be relatively long .