Quickly Deploying Laravel Framework

Last updated: 2023-09-28 17:03:47

Operational Overview

This document describes how to use a HTTP-triggered 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 Tencent Cloud SCF, sign up for a Tencent Cloud account and complete identity verification.

Procedure

Template deployment: Quick deployment of Laravel project

1. Log in to the Serverless console and click on Function Service in the left navigation bar.
2. Select the region and namespace where to create a function at the top of the page and click Create to enter the function creation process.
3. Choose to create a new function using Template Creation. Filter for WebFunc in the search box to select all HTTP-triggered Function templates. Choose the Laravel Framework Template and click Next.
4. On the Create page, you can view and modify the specific configuration information of the template project.
5. Click Complete. After creating the HTTP-triggered Function, you can view its basic information on the Function Management page.
6. Click on Trigger Management in the left menu bar, view the access path URL, and access your deployed Laravel project, as shown in the following figure:



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. Establish a sample Laravel project locally. Navigate to the project directory and execute the following command to initialize the Laravel sample application:
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. Access http://0.0.0.0:9000 in your browser to locally view the Laravel sample project, as illustrated below:


Deployment in cloud

Perform the following steps to make simple modifications to the initialized project, so that it can be quickly deployed through an HTTP-triggered function. The steps of project transformation are as follows:
1. Add a new scf_bootstrap startup file Create a scf_bootstrap startup file in the root directory of your project. This file should be used to configure environment variables, specify service start commands, and other custom operations to ensure that your service can be launched 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 the /tmp directory is writable. Writing to other directories will fail due to lack of permissions. Therefore, it is necessary to inject the output directory of the Laravel framework as an environment variable in the scf_bootstrap file:
#!/bin/bash

# Injecting the SERVERLESS Identifier
export SERVERLESS=1
# Modify the template compilation cache path, as only the /tmp directory is readable and writable in the cloud function.
export VIEW_COMPILED_PATH=/tmp/storage/framework/views
# Modify the session to be stored in memory (array type)
export SESSION_DRIVER=array
# Log Output to stderr
export LOG_CHANNEL=stderr
# Modifying the Application Storage Path
export APP_STORAGE=/tmp/storage

# Initialize Template Cache Directory
mkdir -p /tmp/storage/framework/views
3. Modify the Listening Address and Port Within the HTTP-triggered Function, the listening port is restricted to 9000. Therefore, it is necessary to specify the listening port in the scf_bootstrap file using the following command:
/var/lang/php7/bin/php artisan serve --host 0.0.0.0 --port 9000
The complete content of the scf_bootstrap file is as follows:
#scf_bootstrap
#!/bin/bash

###############################
# Inject Environment Variables in the Serverless Environment
###############################
# Injecting the SERVERLESS Identifier
export SERVERLESS=1
# Modify the template compilation cache path, as only the /tmp directory is readable and writable in the cloud function.
export VIEW_COMPILED_PATH=/tmp/storage/framework/views
# Modify the session to be stored in memory (array type)
export SESSION_DRIVER=array
# Log Output to stderr
export LOG_CHANNEL=stderr
# Modifying the Application Storage Path
export APP_STORAGE=/tmp/storage

# Initialize Template Cache Directory
mkdir -p /tmp/storage/framework/views

# The HTTP function runs based on a Docker image, so the listening address must be set to 0.0.0.0 and the port to 9000.
# The executable file path in the cloud is /var/lang/php7/bin/php.
/var/lang/php7/bin/php artisan serve --host 0.0.0.0 --port 9000
Note:
The example runs in the Php7.2 environment. If you are using a different language version, please refer to Standard Language Environment Absolute Path to modify the cloud executable file path in the scf_bootstrap file. 
4. Deploying Laravel After the local configuration is completed, run the bootstrap file to ensure that your service can be started normally locally. Follow the steps below to deploy Laravel:
4.1 Log in to the Serverless console and click on Function Service in the left navigation bar.
4.2 Select the region where to create a function at the top of the page and click Create to enter the function creation process.
4.3 Choose to start from scratch to create a new function, and configure the relevant options according to the page prompts.
Function Type: Select "HTTP-triggered Function".
Function name: Enter the name of your function.
Region: enter your function deployment region, such as Chengdu.
Runtime Environment: Select "Php7.2".
Submission Method: Select "Upload Local Folder" and upload your local project.
Function Code: select the specific local folder where the function code is.
4.4 Upon completion of the deployment, 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.