This topic describes how to deploy a ThinkPHP application to Function Compute. Function Compute provides a method different from traditional deployment methods. You can deploy a traditional ThinkPHP application to Function Compute and use the application for production without performing operations such as purchasing a server. The deployed ThinkPHP application can use the auto-scaling, pay-as-you-go, and O&M-free features of Function Compute.

Prerequisites

The following operations are complete:

Background information

ThinkPHP is a free, open source, fast, simple, lightweight, and object-oriented PHP development framework. ThinkPHP is designed to develop agile web applications and simplify enterprise application development. ThinkPHP complies with the guidelines of simplicity and practicality. ThinkPHP features excellent performance and is simple in code, and also pays more attention to ease of use. You can use ThinkPHP free of charge because it is distributed under the Apache2 open source license. You can release applications that you develop in ThinkPHP as open source projects or commercial products.

Procedure

  1. Run the following command in the specified directory to initialize a project:
    s init devsapp/start-thinkphp -d start-thinkphp

    The -d parameter specifies the project name. In this example, the project name is start-thinkphp.

    Command output:
     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. Run the following command to go to the project directory:
    cd start-thinkphp
  3. Edit the s.yaml file.

    In this example, the project is deployed by using a container. The project code and the runtime environment, such as NGINX and PHP-FPM, are built to an image from which the container is created. Therefore, before you deploy the project, you must modify the name field in the s.yaml file to the name of your namespace. For example, if the name of your namespace is serverlessdevs188077086902****, modify the name field to name: serverlessdevs188077086902****.

  4. Run the following command to deploy the project:
    s deploy
    Note If you are prompted that the PHP version is later than the required version when you deploy the project, you must change the value of the run field in the s.yaml file to composer install --ignore-platform-reqs.
    Sample command output:
    [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

Result

After the project is deployed, you can view the temporary domain name generated by Function Compute in the execution output. The temporary domain name can be used to access the newly deployed application. For example, run the following command to access the application:

curl http://thinkphp.serverless-devs-thinkphp.188077086902****.cn-shenzhen.fc.devsapp.net
Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Configure a custom domain name.