头图

PHP is one of the commonly used development languages for building web services. Many web development frameworks have been derived based on PHP. Laravel is an excellent representative among them. It has expressive and concise syntax and provides many functions, such as a template engine. , MVC architecture support, high security, developer tools, database migration, etc., can meet different scenarios or scales of Web application development.

This tutorial will guide you how to quickly deploy your Laravel framework to the cloud SCF Web Function

01. Template deployment-no need to change business code, one-click deployment

  1. Log in to the Serverless console, click "Function Service" in the left navigation bar, select the region where you want to create a function at the top of the main interface, and click "New" to enter the function creation process.
  2. Select "Template Creation" to create a new function, enter " WebFunc " in the search box, filter all Web function templates, select "Laravel Framework Template" , and click "Next", as shown below:

  1. On the "Configuration" page, you can view and modify the specific configuration information of the template project;
  2. Click " complete " to create the function. After the function is created, you can view the basic information of the Web function on the "Function Management" page, and access it through the access path URL generated by the API gateway to view your deployed Laravel project.

02. Custom deployment-3 steps to quickly migrate local projects to the cloud

1. Local development

  1. First, please complete the Laravel development environment setup in the local environment, refer to official website document
  2. Create a Laravel sample project locally, in the project directory, initialize the Laravel sample application through the following instructions:
composer create-project --prefer-dist laravel/laravel blog
  1. After starting the sample project locally, visit http://0.0.0.0:9000 in the browser to complete the access to the Laravel sample project locally
$ 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

2. Deploy to the cloud

Next, we will partially modify the locally created project so that it can be quickly deployed through Web Function. For Laravel, the specific transformation steps are as follows:

1. Add scf_bootstrap startup file

scf_bootstrap in the project root directory, complete the environment variable configuration, specify service startup commands and other custom operations in it to ensure that your service can be started normally through this file.

note:

  • scf_bootstrap must have executable permissions of 755 or 777

2. Modify the file read and write path
In the SCF environment, only the /tmp file can be read and written, and other directories will fail to write due to lack of permissions. Therefore, you need to scf_bootstrap in 060e7db9004a1c in the form of environment variables to adjust the output directory of the Laravel framework:

#!/bin/bash

# 注入 SERVERLESS 标识
export SERVERLESS=1
# 修改模板编译缓存路径,云函数只有 /tmp 目录可读写
export VIEW_COMPILED_PATH=/tmp/storage/framework/views
# 修改 session 以内存方式(数组类型)存储
export SESSION_DRIVER=array
# 日志输出到 stderr
export LOG_CHANNEL=stderr
# 修改应用存储路径
export APP_STORAGE=/tmp/storage

# 初始化模板缓存目录
mkdir -p /tmp/storage/framework/views

3. Modify the listening address and port

In the Web function, the listening port must be 9000 , so you need to specify the listening port scf_bootstrap

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

The complete scf_bootstrap content is as follows:

4. After the local configuration is complete, execute the startup file

Make sure that your service can be started locally. Next, log in to the Tencent Cloud Cloud Function Console and create a new Web function to deploy your Laravel project:

After the deployment is complete, click the generated URL to access your Laravel application:

3. Development Management

After the deployment is complete, you can quickly access and test your web services on the SCF console, and experience multiple features of cloud functions such as layer binding, log management, etc., and enjoy the low cost and flexible expansion and contraction brought by the serverless architecture. Advantage.

Web Function Experience Officer Recruitment Order

Surprise benefits are full, click to view event details

Web Function experience

It has been released and launched in all major regions of the country. Welcome to experience and use it!

One More Thing

Experience Tencent Cloud Serverless Demo now and receive the Serverless New User Gift Pack 👉 Tencent Cloud Serverless Newbie Experience .


Serverless
127 声望15 粉丝

Serverless 中文社区是由一群拥有相同价值观和理念的志愿者们共同发起,于 2019 年 12 月正式成立。