1 Star 0 Fork 1

飒龘 / fc-thinkphp

forked from liuwave / fc-thinkphp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

fc-thinkphp

这是一个使用阿里云函数计算 php runtime搭建serverless thinkphp6.0项目的插件,只需要在函数计算的http触发器入口函数添加几行代码就能让thinkphp6.0运行在函数计算的php运行环境下。

示例:http://test-think.oldmen.cn/

快速开始

首先在thinkphp6.0项目中安装插件:

composer require liuwave/fc-thinkphp

修改thinkphp项目的入口文件/tp/public/index.php(仅在cgi模式下需要修改(支持cli模式,默认cgi模式)):

<?php

// [ 应用入口文件 ]
namespace think;


require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
// $http = (new App())->http;
$app=new App();
$app->setRuntimePath(getenv('PHP_RUNTIME_PATH')?:'/tmp/');
$http = $app->http;

$response = $http->run();

$response->send();

$http->end($response);

函数计算入口index.php中,添加:


function handler($request, $context) : Response
{   
       //设置thinkphp根目录
    $appPath=__DIR__ . '/tp';
    require $appPath . '/vendor/autoload.php';
    return (new FcThink($request, $context,['root'=> $appPath, 'runtime_path'=>'/tmp/']))->run();
}

API Reference

FcThink::__construct

\Psr\Http\Message\ServerRequestInterface $fcRequest,array $context,$config = []

$fcRequest

函数计算中http触发器传入的$request参数,遵循 PSR(HTTP message interfaces)标准。更多详情请参见 PSR-7-http-message。具体的代码定义遵循 PSR Http Message

$context

函数计算中http触发器传入的context 参数,参见PHP 事件函数 中的 $context参数

$config

配置参数,默认值为:


 [
      'is_cli'       => false,//是否为cli模式,默认为cgi模式
      'ignore_file'  => false,//是否检测请求路径为存在的文件,如果忽略,则交由thinkphp 入库函数处理,默认为false,即若请求路径为文件(php后缀名除外),直接返回文件内容
      'root'         => '/code/tp',//thinkphp项目的root_path
      'runtime_path' => '/tmp/',//缓存目录
    ];

新手入门

首次使用函数计算,可以参照一下步骤:

1、准备

  • 开通 函数计算,每月免费100万次请求,免费40万(CU-秒)执行时间,日IP在1000以下的博客站足够用。
  • (可选)开通oss
  • (可选)开通NAS
  • 域名

注:NAS和OSS主要用于储存上传文件,如无需要可不开通

2、安装和配置fun工具

安装和配置参见:安装Fun

3、在函数计算控制台创建服务

进入控制台创建服务1.png

输入名称test-think,可选择绑定日志,点击创建。

输入名称2.png

创建成功后,可在服务配置中修改相关配置(角色权限、日志、NAS等等),具体参见函数计算和RAM相关文档。

4、创建函数和http触发器

在上个步骤中创建的test-think服务器下创建一个函数fc

创建函数4-1.png

选择HTTP函数

选择HTTP函数4-2.png

输入函数名称test-fun,选择php7.2运行环境,其他可默认,触发器中,输入触发器名称test-fun,认证方式选择anonymous,请求方式按照需求选择,这里全选。

配置函数和触发器4-3.png

5、配置自定义域名

在控制台自定义域名下,创建一个自定义域名,绑定到上个步骤中创建的函数

配置自定义域名5-1.png

6、下载代码安装

回到第4步创建的函数test-fun概览界面,点击导出,选择导出配置和代码。

导出配置和代码6-1.png

下载并解压到本地,目录结构如下:

    /
      test-think
        test-fun
          index.php
      template.yml

进入 test-fun目录:

cd test-think/test-fun

创建tp应用(如果已有项目,可跳过此步骤,直接复制项目到此test-think/test-fun目录下)

composer create-project topthink/think tp

注:thinkphp 6.0.3中,session的file驱动中缓存目录指定为/rumtime,而这个目录在函数计算时不可写的,会出现错误。 最新的6.0.x-dev开发版中,已经修复这个bug,在发布新版本之前需要使用这个版本:composer require topthink/framework 6.0.x-dev 参见:https://github.com/top-think/framework/pull/2300/commits/264a79521b3062788f5e6cd3c603974ceb63df7e

转到项目目录tp,并安装liuwave/fc-thinkphp

cd tp
composer require liuwave/fc-thinkphp

7.修改test-think/test-fun/index.php:

<?php
use RingCentral\Psr7\Response;
use liuwave\fc\think\FcThink;

function handler($request, $context) : Response
{
        //设置thinkphp根目录
    $appPath=__DIR__ . '/tp';
    require $appPath . '/vendor/autoload.php';
    return (new FcThink($request, $context,['root'=> $appPath, 'runtime_path'=>'/tmp/']))->run();
}

修改tp入口文件test-think/test-fun/tp/public/index.php(仅在cgi模式下需要修改):

<?php

// [ 应用入口文件 ]
namespace think;


require __DIR__ . '/../vendor/autoload.php';

// 执行HTTP应用并响应
// $http = (new App())->http;
$app=new App();
$app->setRuntimePath(getenv('PHP_RUNTIME_PATH')?:'/tmp/');
$http = $app->http;

$response = $http->run();

$response->send();

$http->end($response);

8、部署代码

转到根目录/:

# 当前是`test-think/test-fun/tp`
cd ../../../

执行部署命令(权限策略配置请参考阿里云相关文档):

fun deploy -y

执行结果8-1.png

访问http://test-think.oldmen.cn/,已成功安装:

执行结果8-2.png

参考

Cli模式 vs cgi模式

默认为cgi模式,可切换为cli模式。

CGI模式

cgi不能在thinkphp项目中调用函数计算内置功能的对象如fcLogger/fcPhpCgiProxy/fcSysLogger。

cgi是通过函数计算提供的$GLOBALS['fcPhpCgiProxy'] 对象实现:


 requestPhpCgi($request, $docRoot, $phpFile = "index.php", $fastCgiParams = [], $options = []);
  • $request: 跟 php http 触发器 入口的参数一致
  • $docRoot: thinkphp项目的根目录
  • $phpFile: 用于拼接 cgi 参数中的 SCRIPT_FILENAME 的默认参数
  • $fastCgiParams: 函数计算内部尽量根据fastCgiParams`覆盖一些参数 (reference: cgi)
  • $options: array类型,可选参数, debug_show_cgi_params 设为 true ,会打印每次请求 php 解析时候的 cgi 参数, 默认为 false ;readWriteTimeout 设置解析的时间, 默认为 5 秒

CLI模式

CLI模式支持在thinkphp项目中调用函数计算内置功能的对象如fcLogger/fcPhpCgiProxy/fcSysLogger。

参见liuwave/think-log-driver-sls

VPC权限

如需访问OSS、NAS等资源,需要配置VPC访问权限:配置 VPC 功能

使用OSS上传文件

参考:liuwave/think-filesystem-driver-oss

使用NAS

参见:挂载NAS访问

日志服务

兼容函数计算

参考:liuwave/think-log-driver-sls

开启调试、数据库配置

使用fun部署代码时,会忽略.env,若要开启thinkphp的调试功能、配置数据库,可参照以下方法:

方法一、在部署之后手动添加env文件

在函数计算管理后台,将相关配置写入.env文件(test-think/test-fun/tp/.env(以上文为例))。

方法二、在函数计算项目的根目录下的/template.yml设置环境变量(以上文为例):

注: 需要在添加PHP_前缀,同时需要把.换成_, 并转化成大写,如 PHP_APP_DEBUGPHP_DATABASE_TYPEPHP_DATABASE_HOSTNAME

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources: 
  test-think: #这是服务
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Role: 'acs:ram::****:role/fc-think-test-role'
      LogConfig:
        Project: aliyun-fc-cn-beijing-*****
        Logstore: function-log
      InternetAccess: true
    test-fun: # 函数名
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: index.handler
        Runtime: php7.2
        Timeout: 60
        MemorySize: 128
        EnvironmentVariables: #环境变量
          LD_LIBRARY_PATH: >-
            /code/.fun/root/usr/local/lib:/code/.fun/root/usr/lib:/code/.fun/root/usr/lib/x86_64-linux-gnu:/code/.fun/root/usr/lib64:/code/.fun/root/lib:/code/.fun/root/lib/x86_64-linux-gnu:/code/.fun/root/python/lib/python2.7/site-packages:/code/.fun/root/python/lib/python3.6/site-packages:/code:/code/lib:/usr/local/lib
          NODE_PATH: '/code/node_modules:/usr/local/lib/node_modules'
          PATH: >-
            /code/.fun/root/usr/local/bin:/code/.fun/root/usr/local/sbin:/code/.fun/root/usr/bin:/code/.fun/root/usr/sbin:/code/.fun/root/sbin:/code/.fun/root/bin:/code:/code/node_modules/.bin:/code/.fun/python/bin:/code/.fun/node_modules/.bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin:/bin
          PYTHONUSERBASE: /code/.fun/python
          PHP_APP_DEBUG: true
          PHP_DATABASE_TYPE: mysql
          PHP_DATABASE_HOSTNAME: 127.0.0.1
          PHP_DATABASE_DATABASE: 'test'
          PHP_DATABASE_USERNAME: root
          PHP_DATABASE_PASSWORD: 'test'
          PHP_DATABASE_HOSTPORT: 3306
          PHP_DATABASE_CHARSET: utf8
          PHP_DATABASE_PREFIX: 'p_'
        CodeUri: ./test-think/test-fun
      Events:
        test-fun:
          Type: HTTP
          Properties:
            AuthType: anonymous
            Methods:
              - GET
              - POST
              - PUT
              - DELETE
              - HEAD
              - PATCH


方法三、在函数计算后台更改

函数 概览 界面中,点击 配置 ,在最下方 添加 或 删除环境变量,的格式 参照 方法二

若使用并开启了liuwave/think-log-driver-sls可在函数计算的对应函数的日志查询功能中查看错误日志。

PHP环境配置

可在根目录 添加 extension文件夹,在其中放入额外的配置 .ini,比如修改文件上传大小的限制:

#文件 /extension/filesize.ini
upload_max_filesize = 6m

文件上传,支持单文件和多文件上传

参考:

注,函数计算限制,仅支持不超过6M大小的文件上传或下载,参见使用限制

默认环境中,upload_max_filesize为 2M,可以参照上文PHP环境配置进行更改

BUG提交

Issue

开源许可

The MIT License

MIT License Copyright (c) 2020 葡萄架 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

这是一个使用阿里云函数计算 php runtime搭建serverless thinkphp6.0项目的插件,只需要在函数计算的http触发器入口函数添加几行代码就能让thinkphp6.0运行在函数计算的php运行环境下。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/sakay/fc-thinkphp.git
git@gitee.com:sakay/fc-thinkphp.git
sakay
fc-thinkphp
fc-thinkphp
master

搜索帮助