Laravel + Serverless Framework to quickly create CMS content management system

Keywords: PHP Laravel npm Database

Today, I will bring you a comprehensive practical battle of Laravel + Serverless Framework. There is a bit more information in it. Please take a closer look

First of all, let me introduce the main local environment:

  • Git: not much, as long as you can knock the code, you should know
  • Node.js: due to Tencent cloud Serverless Framework The main programming language is Node.js, so please make one locally
  • NPM: NPM is a package management tool installed with Node.js. It can solve many problems in Node.js code deployment. Our sls is also downloaded through this tool
  • PHP: the best language
  • Composer: a package management tool in PHP world, similar to npm
  • Laravel: laravel is a concise and elegant PHP Web development framework

There seems to be a bit more. Let's look down step by step.

1. Install Serverless

$ npm install -g serverless

Our most important and simplest step is done.

2. Configure Composer

There are two ways. Let's use the first one here to show you

The first is to download the phar file:

$ wget https://getcomposer.org/download/1.9.1/composer.phar

The second type of direct installation:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'baf1608c33254d00611ac1705c1d9958c817a1a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.phpphp -r "unlink('composer-setup.php');"

For convenience, I download the composer.phar file directly here. The specific usage is as follows:

$ php composer.phar

3. Create a costercms

$php composer.phar create-project web-feet/coastercms

First, we use the installed composer to create our project. If you install composer directly in the environment variable, please replace php composer.phar by yourself. Then, we need to modify the Laravel project

Since the cloud function is only readable and writable by / tmp during execution, we need to write the storage directory of laravel framework runtime to this directory. Therefore, we need to modify the bootstrap/app.php file, and add it after $app = new Illuminate\Foundation\Application:

$app->useStoragePath($_ENV['APP_STORAGE'] ?? $app->storagePath());

Finish style:

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->useStoragePath($_ENV['APP_STORAGE'] ?? $app->storagePath());
/*
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
$app->useStoragePath($_ENV['APP_STORAGE'] ?? $app->storagePath());
/*
# View file compilation path
VIEW_COMPILED_PATH=/tmp/storage/framework/views

# Because there is no service function, it is impossible to store sessions on the hard disk. If you do not need sessions, you can use array
# If you need to, you can store the session in a cookie or database
SESSION_DRIVER=array

# It is recommended to output the error log to the console for convenient viewing in the cloud
LOG_CHANNEL=stderr

# The storage directory of the app must be / tmp
APP_STORAGE=/tmp

# View file compilation path
VIEW_COMPILED_PATH=/tmp/storage/framework/views
 
# Because there is no service function, it is impossible to store sessions on the hard disk. If you do not need sessions, you can use array
# If you need to, you can store the session in a cookie or database
SESSION_DRIVER=array
 
# It is recommended to output the error log to the console for convenient viewing in the cloud
LOG_CHANNEL=stderr
 
# The storage directory of the app must be / tmp
APP_STORAGE=/tmp

4. Configure MySQL database

Here we suggest using Tencent cloud's CDB cloud database:

DB_CONNECTION="mysql"
DB_HOST="gz-cdb-qla00XXX.sql.tencentcdb.com"
DB_PORT=639XX
DB_DATABASE="coaster_db"
DB_USERNAME="coaster_usr"
DB_PASSWORD="secret"
DB_PREFIX=

Then rename. env.example to. env. At this point, our Laravel configuration is complete.

5. Deploy Serverless

Next, we create the serverless.yml file in the root directory of the project and configure it as follows

$ touch serverless.yml
# serverless.yml

MyComponent:
component: "@serverless/tencent-laravel"
inputs:
region: ap-guangzhou 
functionName: laravel-function
code: ./
functionConf:
timeout: 10
memorySize: 128
environment:
variables:
TEST: vale
vpcConfig:
subnetId: ''
vpcId: ''
apigatewayConf:
protocol: https
environment: release

When finished, we run in the current directory:

serverless --debug

After bootstrap installation, you can enter the management background:

Portal:

Welcome to: Serverless Chinese network , you can Best practices Experience more about Serverless application development!

Recommended reading: Serverless architecture: from principle, design to project implementation

Posted by ripcurlksm on Tue, 25 Feb 2020 19:19:28 -0800