阿里云-Serverless-函数计算FC-Next.js 14、Node20 部署示例

826 阅读2分钟

Serverless-函数计算FC

现在阿里云免费试用,费用如果只是静态页面,感觉几千次访问是花不到几毛钱的,当然我的试用期还没过,过了之后我会再发文章。

现在也有很多AIGC相关的模板agentcraft-docs.serverless-developer.com/user-manual… 搞AI开发和用nextjs 不妨参考一下 这个项目, 本身也是用nextjs + python 构建的, 另外把国产的LLM跟 open的数据格式做了一个统一,方便大家用不同的基础模型测试自己的项目!感谢@寒斜

也是踩了一些坑,现在FC3.0 node20版本是有问题的,所以本示例是基于FC2.0的

感谢钉钉群Serverless 开发中心群的 @黄明锋 @寒斜 @悟空 @耐思 @刘志远 @连喆人 阿里函数计算官网客户群的 @封崇 @Vine 提供的帮助!

1. 初始化next项目

npx create-next-app@latest

create-next.png

2.修改next.config.mjs

2.1 修改文件名

不知道为什么默认生成的是mjs哈,部署到函数计算mjs会报错,不过没关系,坑我踩完了

修改next.config.mjsnext.config.js

2.2 修改文件内容

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  output: "standalone",
};

module.exports = nextConfig;

3. 创建s.yaml文件

因为是Serverless Devs 应用,所以需要s.yaml配置文件来告诉阿里云配置信息,想问为什么这么配置可以评论区留言

edition: 1.0.0
name: web-framework-app
access: "undefined"

vars:
  region: "cn-hangzhou"
  functionName: "next"
  service:
    name: "web-framework"
    description: 'Serverless Devs Web Framework Service'
services:
  framework:
    component: fc 
    actions:
      pre-deploy:
        - run: ls /usr/local/versions/node
        - run: export PATH=/usr/local/versions/node/v20.8.1/bin:$PATH && npm install
        - run: export PATH=/usr/local/versions/node/v20.8.1/bin:$PATH && npm run build
        - run: cp -r public .next/standalone/public
        - run: mv static standalone/.next/static
          path: ./.next
    props:
      region: ${vars.region}
      service: ${vars.service}
      function:
        name: ${vars.functionName}
        description: 'Serverless Devs Web Framework Function'
        codeUri: './.next/standalone'
        runtime: custom.debian10
        timeout: 60
        caPort: 3000
        layers:
          - acs:fc:${vars.region}:official:layers/Nodejs20/versions/1
        environmentVariables:
          PATH: /opt/nodejs20/bin:/usr/local/bin/apache-maven/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/ruby/bin
        customRuntimeConfig:
          command:
            - node
            - server.js
      triggers:
        - name: httpTrigger
          type: http
          config:
            authType: anonymous
            methods:
              - GET
      customDomains:
        - domainName: auto
          protocol: HTTP
          routeConfigs:
            - path: '/*'

4.把代码上传到GitHub(以下平台都可以)

iShot_2024-04-11_16.26.12.png

5.创建Serverless-函数计算FC应用

  1. 前提你已经开通了函数计算FC
  2. 根据表单授权GitHub平台权限
  3. 选择项目
  4. 创建并部署默认环境
  5. 完成后会自动跳转,期间不要刷新页面

iShot_2024-04-11_16.35.59.png

创建应用.png

创建成功.png

6. 部署成功

iShot_2024-04-11_16.36.55.png

7. 预览

iShot_2024-04-11_16.37.37.png

最后

测试域名可能会在30 天后进行回收,自定义域名我在FC3.0搞过,2.0还没仔细研究,最后希望国产的Serverless服务越来越好吧。

广告

如果你想用2块钱一天的chat鸡踢屁4.0可以私信我~