tencent cloud

Feedback

Quickly Deploying Laravel Framework

Last updated: 2021-08-20 16:21:27

    Overview

    This document describes how to use a web function to quickly migrate a local Laravel service to the cloud.

    Note:

    This document mainly describes how to deploy in the console. You can also complete the deployment on the command line. For more information, please see Deploying Framework on Command Line.

    Prerequisites

    Before using SCF, you need to sign up for a Tencent Cloud account and complete identity verification first.

    Directions

    Template deployment - quick deployment of Laravel project

    1. Log in to the SCF console and click Function Service on the left sidebar.
    2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
    3. Select Template for Creation Method, enter WebFunc in the search box to filter all web function templates, select Laravel Framework Template, and click Next as shown below:
    4. On the Configuration page, you can view and modify the specific configuration information of the template project.
    5. Click Complete. After creating the web function, you can view its basic information on the Function Management page.
    6. You can access the deployed Laravel project at the access path URL generated by API Gateway. Click Trigger Management on the left to view the access path as shown below:
    7. Click the access path URL to access the Laravel project.

    Custom deployment - quick migration of local project to cloud

    Local development

    1. Refer to Getting Started on macOS to set up the Laravel development environment locally.
    2. Create the sample Laravel project locally. Enter the project directory and run the following command to initialize it.
      composer create-project --prefer-dist laravel/laravel blog
    3. Run the following command to start the sample project locally. Below is the sample code:
      $ php artisan serve --host 0.0.0.0 --port 9000
      Laravel development server started: <http: 0.0.0.0:9000="">
      [Wed Jul 7 11:22:05 2021] 127.0.0.1:54350 [200]: /favicon.ico
    4. Visit http://0.0.0.0:9000 in a browser, and you can access the sample Laravel project locally as shown below:

    Deployment in cloud

    Add the following content to the file (which is used to configure environment variables and start services. Here is only a sample. Please adjust the specific operations according to your actual business scenario):

    Next, perform the following steps to make simple modifications to the initialized project, so that it can be quickly deployed through a web function. The steps of project transformation are as follows:

    1. Add the scf_bootstrap bootstrap file
      Create the scf_bootstrap bootstrap file in the project root directory. This file is used to configure environment variables, specify service bootstrap commands, and make sure that your service can be started normally through this file.

      Note:

      • scf_bootstrap must have the executable permission of 755 or 777.
      • If you want to output environment variables in the log, you need to add the -u parameter before the bootstrap command, such as python -u app.py.
    2. Modify the file read/write path
      In the SCF environment, only files in the /tmp directory are readable/writable. If you select other directories, write will fail due to the lack of permissions. Therefore, you need to inject environment variables in the scf_bootstrap file to adjust the output directory of the Laravel framework:

      #!/bin/bash
      # Inject the SERVERLESS flag
      export SERVERLESS=1
      # Modify the template compilation cache path, as only `/tmp` is readable/writable in SCF
      export VIEW_COMPILED_PATH=/tmp/storage/framework/views
      # Modify `session` to store it in the memory (array type)
      export SESSION_DRIVER=array
      # Output logs to `stderr`
      export LOG_CHANNEL=stderr
      # Modify the application storage path
      export APP_STORAGE=/tmp/storage
      # Initialize the template cache directory
      mkdir -p /tmp/storage/framework/views
      
    3. Modify the listening address and port
      The listening port in the web function must be 9000, so you need to specify the listening port in scf_bootstrap through the following command:

      /var/lang/php7/bin/php artisan serve --host 0.0.0.0 --port 9000
      

      The content of scf_bootstrap is as follows:

    4. Deploy in the cloud
      After the local configuration is completed, run the bootstrap file and make sure that your service can be normally started locally. Then, perform the following steps to deploy Laravel:

      1. Log in to the SCF console and click Function Service on the left sidebar.
      2. Select the region where to create a function at the top of the page and click Create to enter the function creation process.
      3. Select Custom Creation for Creation Method and configure the options as prompted as shown below:
        • Function Type: select Web function.
        • Function Name: enter the name of your function.
        • Region: enter your function deployment region, such as Chengdu.
        • Deployment Method: select Code deployment and upload your local project.
        • Runtime environment: select "Php7".
      4. After the deployment is completed, click the generated URL to access your Laravel application as shown below:

    Development management

    After the deployment is completed, you can quickly access and test your web service in the SCF console and try out various features of SCF, such as layer binding and log management. In this way, you can enjoy the advantages of low cost and flexible scaling brought by the serverless architecture.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support