您现在的位置是:首页 > 文章详情

用阿里云函数计算部署thinkphp5.1

日期:2018-10-16点击:534

thinkphp国内还是用的很多的哈,我自己就用哈。所以研究了一下用函数计算部署tp5。
其中借鉴和学习了@rsong 的文章:《十分钟上线-函数计算玩转 WordPress

这里是TP5的演示:https://31199382.cn-beijing.fc.aliyuncs.com/2016-08-15/proxy/web-site/tp5/
其实也没什么好演示的,就是纯净的tp5 ( ̄▽ ̄)~*

步骤:

1.后台创建php7.2环境的函数,创建http触发器,这些就不详细写了,可以去看文档。

2.打开tp5的入口文件(/public/index.php)在namespace think; 这句的下面,加如下代码:

if(!$_SERVER['PATH_INFO']) $_SERVER['PATH_INFO'] = preg_replace("/^(\?s\=\/)/","",$_SERVER['REQUEST_URI']); 

3.准备tp5源码,在根目录创建index.php文件(作为函数的入口文件),代码如下:
(第一次在阿里云博客上写文章哈,有点手生)

<?php #自定义的域名,绑定了自定义域名的,可以换成自己自定义的。 $MY_HOST = "31199382.cn-beijing.fc.aliyuncs.com"; #web目录,默认是tp5的public目录,绝对路径,如:/code/public $WWW_DIR = '/code/public'; function handler($request, $context){ #如果不使用函数计算后台提供的那个厂厂的域名,这句可以注释掉。 if(strpos($request->getAttribute("requestURI"),"/2016-08-15/proxy") !== false) $request = clearFcHost($request,$context);#兼容 fc后台的url地址 $uri = $request->getAttribute("requestURI"); $file = explode("?", $uri)[0]; if($file=='/') $uri='/';# $file = $GLOBALS['WWW_DIR'].$file; if(file_exists($file) and $uri!='/'){ if(strpos($uri,".php")) return php_run(basename($file), $request, $context);#php_run return static_run($uri);#static_run } $request = $request->withAttribute("requestURI", "?s=".$uri); return php_run('index.php', $request, $context);# php_run } function php_run($name,$request, $context) { return $GLOBALS['fcPhpCgiProxy']->requestPhpCgi($request, $GLOBALS['WWW_DIR'], $name,['SERVER_NAME' => $GLOBALS['MY_HOST'], 'SERVER_PORT' => '80', 'HTTP_HOST' => $GLOBALS['MY_HOST']],['debug_show_cgi_params' => false, 'readWriteTimeout' => 15000]); } use RingCentral\Psr7\Response; function static_run($uri): Response{ $file_dir = $GLOBALS['WWW_DIR'].$uri; #完整文件路径 $file_dir = explode("?", $file_dir)[0]; #去掉动态路径 if(is_dir($file_dir)) $file_dir .= '/index.html';# 可以这里定义目录的默认索引页 $handle = fopen($file_dir, "r"); $contents = fread($handle, filesize($file_dir)); fclose($handle); return new Response(200, ['Content-Type' => $GLOBALS['fcPhpCgiProxy']->getMimeType($file_dir),'Cache-Control' => "max-age=8640000",'Accept-Ranges' => 'bytes'], $contents); } function clearFcHost($request,$context){ $uri = $request->getAttribute("requestURI"); $uri = str_replace("/2016-08-15/proxy/".$context['service']['name']."/".$context['function']['name'],"",$uri); $request = $request->withAttribute("requestURI", $uri); return $request; } #错误处理 function error($code) { #if($resp->getStatusCode() !=200) return error($resp->getStatusCode()); return '这里还木有写哈~~'; } 

4.由于函数计算只有/tmp 目录可写,所以要改一下tp5的配置文件
编辑/config/log.php

'path' => '/tmp/log',

编辑/config/cache.php

'path' => '/tmp/cache',

好了,至此就部署好了。

ps.简单的测试了一下,应该没什么问题。首次发布在阿里云博客,有什么问题请来原文留言哈。

原文链接:https://yq.aliyun.com/articles/653669
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章