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.
Before using SCF, you need to sign up for a Tencent Cloud account and complete identity verification first.
WebFunc
in the search box to filter all web function templates, select Laravel Framework Template, and click Next as shown below:composer create-project --prefer-dist laravel/laravel blog
$ 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
http://0.0.0.0:9000
in a browser, and you can access the sample Laravel project locally as shown below: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:
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 of755
or777
.- If you want to output environment variables in the log, you need to add the
-u
parameter before the bootstrap command, such aspython -u app.py
.
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
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:
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:
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.
Was this page helpful?