首页 文章详情

如何使用 Serverless Devs 部署静态网站到函数计算?

Serverless | 112 2022-04-17 13:18 0 0 0
UniSMS (合一短信)

后台回复 手册 即刻免费下载 2022 Serverless 工具书

作者 | 邓超(Serverless Devs 开源贡献者)


前言


公司经常有一些网站需要发布上线,对比了几款不同的产品后,我决定使用阿里云函数计算(FC) 来托管构建出来的静态网站。FC 弹性实例自带的 500 Mb 存储空间[1]对静态网站来说简直是太充足了 。


函数计算资源使用:

https://help.aliyun.com/document_detail/51907.html


部署静态网站


首先假设我们现在已有如下结构的前端工程:


/├ dist/ 待部署的构建产物│  └ index.html ├ src/package.json


01

编写一个简单的 HTTP 服务器


Express 为例, 首先添加依赖到工程:


yarn add express

然后新建 app.js 并写入:


let Express = require("express");let app = new Express();app.use(Express.static('dist')); //使用 dist 文件夹中的内容对外提供静态文件访问app.use((req, res) => { res.redirect("/"); }); // 重定向无法处理的请求到网站的根目录let port = 9000;app.listen(port, () => { console.log(`App started on port ${port}`); }); // 监听 FC custom 运行时默认的 9000 端口

通过 node app.js 启动这个简单的 Express 服务器, 并访问 http://localhost:9000 ,确认 /dist/index.html 能被访问到。


接下来就是把 app.js 和 dist 一起发布到函数计算上就行了。


02

编写 bootstrap


函数计算 custom 运行时要求用户提供一个 bootstrap 文件用于启动自定义的 HTTP 服务器, 所以我们需要在根目录下创建这个文件:


#! /bin/bashnode app.js

注意第一行的 #! /bin/bash 是必须的, 不然函数计算不知道该用哪一个解释器来执行脚本中的内容。Windows 用户记得把这个文件的换行符从 /r/n 改成 /n , 不然会遇到函数计算启动超时的问题。


03

安装 @serverless-devs/s 并编写 s.yaml


添加 @serverless-devs/s 命令行工具到工程:


yarn add @serverless-devs/s -D

然后在根目录下创建一个基础的 s.yaml 配置文件:


# https://github.com/devsapp/fc/blob/main/docs/zh/yaml/edition: 1.0.0name: my-awesome-website-projectservices:  my-service: # 任意的名称    component: devsapp/fc       # 使用 fc 组件    props:      region: cn-shenzhen       # 部署到任意的可用区, 例如深圳.      service:        name: my-awesome-websites   # 深圳可用区的 my-awesome-websites 服务      function:        name: www-example-com     # my-awesome-websites 服务下的一个函数        runtime: custom        # 使用 custom 运行环境        handler: dummy-handler     # 由于使用了 custom 运行环境, 所以这里可以随便填        codeUri: ./          # 部署当前文件夹下的全部内容      triggers:        - name: http          type: http        # 创建一个 HTTP 类型的触发器, 以便客户端可以通过 HTTP 协议进行访问          config:            authType: anonymous    # 允许匿名访问            methods: [ HEAD, GET ]  # 静态网站只需要处理 HEAD 和 GET 请求就够了


04

部署到函数计算


配置好 AccessKey 和 AccessSecret[2] 后, 执行命令:


s deploy

你的网站就部署上去了。


接下来就是配置自定义域名了, 配置好以后就可以通过你自己的域名访问到这个网站了。


05

配置自定义域名


以自定义域名 deploy-static-website-to-fc.example.dengchao.fun 为例;


首先添加 CNAME 记录, 解析值填写 ${UID}.${REGION}.fc.aliyuncs.com


因为我们的 s.yaml 中设置的 region 是 cn-shenzhen, 所以对应的值就是 xxxxxx.cn-shenzhen.fc.aliyuncs.com 


 

接下来设置函数计算控制台上的自定义域名:



访问一下试试看

http://deploy-static-website-to-fc.example.dengchao.fun(opens new window)


样本工程


本文中的样本工程已经上传到 GitHub: 

https://github.com/DevDengChao/deploy-static-website-to-fc-example(opens new window)


相关链接:


[1] 500 Mb 存储空间
https://help.aliyun.com/document_detail/51907.html

[2] 配置好 AccessKey 和 AccessSecret
https://www.serverless-devs.com/serverless-devs/command/config

[3] 阿里云函数计算-产品简介
https://help.aliyun.com/document_detail/52895.html

[4] 资源使用限制
https://help.aliyun.com/document_detail/51907.html

[5] 自定义运行环境
https://help.aliyun.com/document_detail/132044.html

[6] 配置自定义域名
https://help.aliyun.com/document_detail/90763.html

[7] Serverless devs 官网

https://www.serverless-devs.com/








RECRUITMENT


极速上手 Serverless



随着 Serverless 热度不断升高,越来越多人期望在实际工作中能快速上手。为了让更多 Serverless 初学者真正学会 Serverless 理论知识,在工作中根据需要灵活应用 Serverless 技术,阿里云 Serverless 团队推出技术图谱,本课程包含视频课程、动手实验、电子书、开源项目多种形式内容,让各位开发者即学即用,跑步入场享受 Serverless 技术红利!点击下方卡片,即刻免费学习。


RECRUITMENT


READ MORE 



基于 Serverless 架构的头像漫画风处理小程序


硬核调试实操 | 手把手带你实现 Serverless 断点调试

 

若您对产品使用或者技术学习有任何疑问 & 建议,欢迎后台回复 “进群” 加入阿里云 Serverless 开发者技术学习群交流。


戳下方,详细了解 Serverless Devs!

good-icon 0
favorite-icon 0
收藏
回复数量: 0
    暂无评论~~
    Ctrl+Enter