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

前提条件

您已完成以下操作:

背景信息

ThinkPHP是一个免费开源、快速简单及面向对象的轻量级PHP开发框架,是为了敏捷Web应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简代码的同时,更注重易用性。遵循Apache 2开源许可协议发布,意味着您可以免费使用ThinkPHP,甚至允许把您基于ThinkPHP开发的应用开源或商业产品发布、销售。

操作步骤

  1. 在目标目录中执行以下命令,初始化项目。
    s init devsapp/start-thinkphp -d start-thinkphp

    -d用于指定项目名称,本示例以项目名称start-thinkphp为例,介绍如何将thinkphp迁移到函数计算。

    执行输出:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     devsapp!start-thinkphp@0.0.2.zip file decompression completed
     please select credential alias default
    
        __  .__    .__        __           .__
        _/  |_|  |__ |__| ____ |  | ________ |  |__ ______
           __  |  |  |/    |  |/ /____ |  |  \____
         |  | |   Y    |   |      < |  |_> >   Y    |_> >
         |__| |___|  /__|___|  /__|_ |   __/|___|  /   __/
                   /        /     /|__|        /|__|
    
    
        Welcome to the start-thinkphp 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 ThinkPHP project:
             Full yaml configuration: https://github.com/devsapp/thinkphp
             ThinkPHP development docs : https://docs.topthink.com/think-api/1835085
         This application homepage: https://github.com/devsapp/start-thinkphp
    
    
    ‍ Thanks for using Serverless-Devs
     You could [cd /test/start-thinkphp] 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-thinkphp
  3. 编辑s.yaml文件。

    由于该示例是使用Container模式部署的,Container模式会将Web工程代码和运行环境(例如Nginx+PHP-FPM)构建build到一个镜像作为Custom Container的镜像,所以在部署该示例前,您需要修改s.yaml文件的项目名称name字段,该字段需要修改为您的命名空间。例如当您的命名空间为serverlessdevs188077086902****,则该字段需要修改为name: serverlessdevs188077086902****

  4. 执行以下命令,部署项目。
    s deploy
    说明 当您在部署时提示PHP版本较高,您需要将s.yaml文件内run字段的参数信息修改为composer install --ignore-platform-reqs
    输出示例:
    [2021-09-26T19:39:51.993] [INFO ] [S-CLI] - Start ...
    [2021-09-26T19:39:52.036] [INFO ] [S-CLI] - Start the pre-action
    [2021-09-26T19:39:52.037] [INFO ] [S-CLI] - Action: composer install --ignore-platform-reqs
    Installing dependencies from lock file (including require-dev)
    Verifying lock file contents can be installed on current platform.
    Package operations: 15 installs, 0 updates, 0 removals
      - Downloading psr/cache (1.0.1)
      - Downloading league/flysystem-cached-adapter (1.1.0)
      - Downloading psr/container (1.1.1)
      - Downloading psr/simple-cache (1.0.1)
      - Downloading psr/log (1.1.4)
      - Downloading topthink/framework (v6.0.8)
      - Downloading topthink/think-trace (v1.4)
      - Installing psr/cache (1.0.1): Extracting archive
      - Installing league/mime-type-detection (1.7.0): Extracting archive
      - Installing league/flysystem (1.1.3): Extracting archive
      - Installing league/flysystem-cached-adapter (1.1.0): Extracting archive
      - Installing psr/container (1.1.1): Extracting archive
      - Installing symfony/polyfill-php80 (v1.22.1): Extracting archive
      - Installing symfony/polyfill-php72 (v1.22.1): Extracting archive
      - Installing symfony/polyfill-mbstring (v1.22.1): Extracting archive
      - Installing symfony/var-dumper (v4.4.22): Extracting archive
      - Installing topthink/think-helper (v3.1.4): Extracting archive
      - Installing psr/simple-cache (1.0.1): Extracting archive
      - Installing psr/log (1.1.4): Extracting archive
      - Installing topthink/think-orm (v2.0.40): Extracting archive
      - Installing topthink/framework (v6.0.8): Extracting archive
      - Installing topthink/think-trace (v1.4): Extracting archive
    Generating autoload files
    > @php think service:discover
    Succeed!
    > @php think vendor:publish
    File /test/start-thinkphp/code/config/trace.php exist!
    Succeed!
    6 packages you are using are looking for funding.
    Use the `composer fund` command to find out more!
    [2021-09-26T19:40:18.199] [INFO ] [S-CLI] - End the pre-action
     Run: docker build -t fc-example/serverless-devs-thinkphp.thinkphp:LATEST-1632656423838 -f Dockerfile .
     Run: docker push registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-thinkphp.thinkphp:LATEST-1632656423838
    [2021-09-26T19:40:37.856] [INFO ] [WEB-FRAMEWORK] - The configuration of the domain name is not detected, and a temporary domain name is generated.
     devsapp_domain.zip file decompression completed
     End of request
     Deployed.
     End of request
    [2021-09-26T19:40:45.351] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-09-26T19:40:45.354] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-26T19:40:45.361] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-09-26T19:40:45.366] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
     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-26T19:40:46.394] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-thinkphp exists
    [2021-09-26T19:40:47.161] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    √ devsapp_ram.zip file decompression completed
    [2021-09-26T19:40:48.374] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-09-26T19:40:48.649] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-09-26T19:40:48.764] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-09-26T19:40:48.849] [INFO ] [RAM] - Updating plicy: AliyunFCDefaultRolePolicy
    [2021-09-26T19:40:49.219] [INFO ] [FC-DEPLOY] - Checking Function thinkphp exists
    [2021-09-26T19:40:50.226] [INFO ] [FC-DEPLOY] - Using image registry: registry.cn-shenzhen.aliyuncs.com
    [2021-09-26T19:40:50.464] [INFO ] [FC-DEPLOY] - Try to use a temporary token for docker login
    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-thinkphp.thinkphp:LATEST-1632656423838...
    The push refers to repository [registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-thinkphp.thinkphp]
    ba06e0029599: Layer already exists                                                                                                                                                           bf4575a82be2: Layer already exists                                                                                                                                                           0797ed074917: 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-1632656423838: digest: sha256:420ae9b196cc800b72792721369c5f88a4919af8a439810ee1dc231efce42bb6 size: 4292
    [2021-09-26T19:40:55.467] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-thinkphp exists
    [2021-09-26T19:40:56.545] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-thinkphp exists
     Make service serverless-devs-thinkphp success.
     Make function serverless-devs-thinkphp/thinkphp success.
     Make trigger serverless-devs-thinkphp/thinkphp/serverless-devs-thinkphp success.
    [2021-09-26T19:40:59.806] [INFO ] [FC-DEPLOY] - Checking Service serverless-devs-thinkphp exists
    [2021-09-26T19:41:00.298] [INFO ] [FC-DEPLOY] - Checking Function thinkphp exists
    [2021-09-26T19:41:01.187] [INFO ] [FC-DEPLOY] - Checking Trigger serverless-devs-thinkphp exists
    [2021-09-26T19:41:02.081] [INFO ] [FC-DEPLOY] - Creating custom domain: thinkphp.serverless-devs-thinkphp.188077086902****.cn-shenzhen.fc.devsapp.net
     devsapp_fc-domain.zip file decompression completed
    [2021-09-26T19:41:03.575] [INFO ] [FC-DOMAIN] - Creating custom domain: thinkphp.serverless-devs-thinkphp.188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: serverless-devs-thinkphp
     Try container acceleration
    thinkphpExample:
      region: cn-shenzhen
      serviceName: serverless-devs-thinkphp
      functionName: thinkphp
      customDomains:
        - >-
          http://thinkphp.serverless-devs-thinkphp.188077086902****.cn-shenzhen.fc.devsapp.net

执行结果

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

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