本文介绍如何将Laravel应用部署到函数计算。与传统的部署方法相比,您可以跳过购买机器等步骤,将传统的Laravel应用一键部署至云端直接用于生产,并且拥有弹性伸缩、按量付费及免运维等特性。

操作步骤

  1. 在目标目录中执行以下命令,初始化项目。
    s init devsapp/start-laravel -d start-laravel
    -d用于指定项目名称,本示例以项目名称start-laravel为例,介绍如何将Laravel迁移到函数计算。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-laravel@0.0.2.zip file decompression completed
    
        .__                                  .__
        |  | _____ ____________ ___  __ ____ |  |
        |  | \__  \\_  __ \__  \\  \/ // __ \|  |
        |  |__/ __ \|  | \// __ \\   /\  ___/|  |__
        |____(____  /__|  (____  /\_/  \___  >____/
                  \/           \/          \/
    
    
        Welcome to the start-laravel application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
         This application can help you quickly deploy the Laravel project:
             Full yaml configuration: https://github.com/devsapp/laravel#%E5%AE%8C%E6%95%B4yaml
             Laravel development docs : https://learnku.com/docs/laravel/8.x
         This application homepage: https://github.com/devsapp/start-laravel
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-laravel] and enjoy your serverless journey!
    ️ If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录。
    cd start-laravel
  3. 编辑s.yaml文件。
    由于该示例是使用Container模式部署的,Container模式会将Web工程代码和运行环境(例如Nginx+PHP-FPM)构建build到一个镜像作为Custom Container的镜像,所以在部署该示例前,您需要修改s.yaml文件的项目名称name字段,该字段需要修改为您的命名空间。例如当您的命名空间为serverlessdevs188077086902****,则该字段需要修改为name: serverlessdevs188077086902****
  4. 执行以下命令,部署项目。
    s deploy
    输出示例:
    [2021-09-03T15:07:58.647] [INFO ] [S-CLI] - Start ...
      Run: docker build -t fc-example/serverless-devs-laravel.laravel:LATEST-1630652880499 -f Dockerfile .
      Run: docker push registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1630652880499
    [2021-09-03T15:08:03.610] [INFO ] [WEB-FRAMEWORK] - The configuration of the domain name is not detected, and a temporary domain name is generated.
    (node:12020) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
      End of request
      Deployed.
      End of request
    [2021-09-03T15:08:07.710] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-09-03T15:08:07.710] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-03T15:08:07.710] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-03T15:08:07.711] [INFO ] [FC-DEPLOY] - Using accessKeySecret: KdcUEJLoGto****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-03T15:08:08.237] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-laravel exists
    [2021-09-03T15:08:08.390] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-09-03T15:08:08.719] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-03T15:08:08.951] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-03T15:08:09.076] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-03T15:08:09.205] [INFO ] [FC-DEPLOY] - Checking Function laravel exists
    [2021-09-03T15:08:09.388] [INFO ] [FC-DEPLOY] - Using image registry: registry.cn-shenzhen.aliyuncs.com
    [2021-09-03T15:08:09.548] [INFO ] [FC-DEPLOY] - Try to use a temporary token for docker login
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    
    Login to registry: registry.cn-shenzhen.aliyuncs.com with user: cr_temp_user
    Pushing docker image: registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1630652880499...
    The push refers to repository [registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel]
    525f007fda01: Layer already exists
    4fd11e800920: Layer already exists
    176b3d17b74f: Layer already exists
    d2f0f5772b2e: Layer already exists
    a1a00fb53fe2: Layer already exists
    878012e6ca96: Layer already exists
    a2d3463119d7: Layer already exists
    9dfc54546eb9: Layer already exists
    38d27ebecaac: Layer already exists
    052274e6c62b: Layer already exists
    58eed48d658d: Layer already exists
    c9b07fcdb99d: Layer already exists
    f3e7cafd1d54: Layer already exists
    b6c4e5a72df7: Layer already exists
    53b44c8b01cc: Layer already exists
    0768b5d062c4: Layer already exists
    27a0adc0a8a6: Layer already exists
    92c354d70bb1: Layer already exists
    14a1ca976738: Layer already exists
    LATEST-1630652880499: digest: sha256:906f1776e0eabd7cb46a16cac57f39e1a2900fbdb9f02208efc799467f1a018d size: 4291
    [2021-09-03T15:08:11.276] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-laravel exists
    [2021-09-03T15:08:11.330] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-laravel exists
    [2021-09-03T15:08:11.413] [INFO ] [FC-DEPLOY] - Creating service: serverless-devs-laravel
    [2021-09-03T15:08:11.414] [INFO ] [FC-DEPLOY] - Creating function: laravel
    [2021-09-03T15:08:11.414] [INFO ] [FC-DEPLOY] - Creating triggers: ["serverless-devs-laravel"]
      Make service serverless-devs-laravel success.
      Make function serverless-devs-laravel/laravel success.
      Make trigger serverless-devs-laravel/laravel/serverless-devs-laravel success.
    [2021-09-03T15:08:12.043] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-laravel exists
    [2021-09-03T15:08:12.148] [INFO ] [FC-DEPLOY] - Checking Function laravel exists
    [2021-09-03T15:08:12.318] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-laravel exists
    [2021-09-03T15:08:12.559] [INFO ] [FC-DEPLOY] - Creating custom domain: laravel.serverless-devs-laravel. 188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-09-03T15:08:13.520] [INFO ] [FC-DOMAIN] - Creating custom domain: laravel.serverless-devs-laravel. 188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-laravel
      Try container acceleration
    laravelExample:
      region: cn-shenzhen
      serviceName: serverless-devs-laravel
      functionName: laravel
      customDomains:
        - >-
          http://laravel.serverless-devs-laravel. 188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://laravel.serverless-devs-laravel.188077086902****.cn-shenzhen.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名