原文作者:txd123
原文連結:
https://developer.aliyun.com/article/727023 更多雲原生相關資訊可關注 阿裡巴巴雲原生技術圈 。前言
阿裡雲函數計算 Function Compute(FC),旨在幫助使用者采用彈性伸縮、動态配置設定資源的方式,來執行業務函數。讓使用者無需購買部署伺服器,無需考慮業務負載,就能快速搭建可處理高并發的背景服務。
本文介紹 HTTP 觸發器快速遷移 CodeIgniter 應用到函數計算。函數計算運作 PHP 架構原理可以參考一下《十分鐘上線-函數計算玩轉 WordPress》。
案例概覽
在本教程中,我們講解如何利用函數計算一步一步來建構 Web 的 Server 端,該案例是把一個 CodeIgniter 部署到函數計算,本文旨在展示函數計算做 Web Backend 能力,具體表現為以下幾點:
完善的 PHP 系統遷移到 FC 的成本不高
FC 打通了專有網絡 VPC 功能,使用者的函數可以配置通路專有網絡的雲資源,比如本案例中:nas
案例體驗入口:
體驗位址:
http://support.mofangdegisn.cn/案例開發配置步驟
- CodeIgniter 項目同步到 nas 上
在本地建立一個函數的根目錄,在該目錄下建立 template.yml 檔案,内容如下:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
Wkhtmltopdf:
Type: 'Aliyun::Serverless::Service'
Properties:
Role: 'XXXX'
LogConfig:
Project: XXX
Logstore: XXX
VpcConfig:XXX
VpcId:
VSwitchIds:
- XXX
SecurityGroupId: XXXX
NasConfig:
UserId: 10003
GroupId: 10003
MountPoints:
- ServerAddr: 'XXXX'
MountDir: /mnt/www
CodeIgniter:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: php7.2
CodeUri: './index.php'
Events:
httpTrigger:
Type: HTTP
Properties:
AuthType: ANONYMOUS
Methods:
- GET
當然您這邊也可以使用fun工具提供的 NasConfig: Auto 配置,可以自動生成 nas 相關配置,具體步驟可以參考:
https://yq.aliyun.com/articles/712693fun 工具進入之前建立的函數根目錄下後執行,fun nas info 檢視 nas 的相關資訊

我們把 CodeIgniter 項目放在剛剛執行 fun nas info 指令檢視到的本地 nas 錄下。
最後使用 fun nas sync 将本地 nas 資源同步到 nas 服務:
使用 fun 工具把本地資源同步到 nas 上具體步驟可以參考:
https://yq.aliyun.com/articles/712700- 建立函數入口檔案
在函數根目錄下建立一個 index.php 檔案,檔案内容如下:
<?php
#自定義的域名,綁定了自定義域名的,可以換成自己自定義的。
$MY_HOST = "support.mofangdegisn.cn";
#web目錄,CodeIgniter架構預設是根目錄
$WWW_DIR = '/mnt/www/CodeIgniter';
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 CodeIgniter架構入口 index.php
}
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 'Codelgniter架構測試~~';
}
執行 fun deploy 部署函數,部署成功如圖!
最後給服務函數綁定自定義域名詳情。
總結
函數計算有如下優勢:
- 無需采購和管理伺服器等基礎設施
- 專注業務邏輯的開發
- 提供日志查詢、性能監控、報警等功能快速排查故障
- 以事件驅動的方式觸發應用響應使用者請求
- 毫秒級别彈性伸縮,快速實作底層擴容以應對峰值壓力
- 按需付費。隻需為實際使用的計算資源付費,适合有明顯波峰波谷的使用者通路場景
- 除了上面所列的優勢,FC 可以做為 Web Backend,隻需要編寫一個函數實作傳統 Web 伺服器中的 conf 中的邏輯,就可以将一個完整的 Web 工程遷移到 FC ,進而從傳統的 Web 網站運維,監控等繁瑣的事務中解放出來。
“ 阿裡巴巴雲原生 關注微服務、Serverless、容器、Service Mesh 等技術領域、聚焦雲原生流行技術趨勢、雲原生大規模的落地實踐,做最懂雲原生開發者的技術圈。”