十分鐘上線-函式計算&Laravel的那些事兒
前言
這篇文章是 ofollow,noindex" target="_blank">十分鐘上線-函式計算玩轉 WordPress 的姊妹篇,有關函式計算的優勢,php runtime 執行php 框架原理等相關內容可以先預覽 十分鐘上線-函式計算玩轉 WordPress ,本文假設您已經理解函式計算 php runtime, vpc, nas, 自定義域名等相關內容的基礎上,我們通過將github上一個star很高的larvel入門部落格教程 Laravel-5" rel="nofollow,noindex" target="_blank">Learn-Laravel-5 專案移植到在函式計算上, 展示函式計算環境驅動laravel的能力,實現 larvel web 專案 serverless 化。
案例體驗入口:
- 體驗地址: http://rayzhang.mofangdegisn.cn
-
後臺登入: http://rayzhang.mofangdegisn.cn/admin
- 賬號:[email protected]
- 密碼:larvel-pwd
FC 執行larvel 流程
-
下載 Learn-Laravel-5 ,準備好mysql資料庫。
- 修改.env, 主要是修改資料庫連線相關內容
DB_CONNECTION=mysql DB_HOST=rm-xxyyyy12583ck110swao.mysql.rds.aliyuncs.com DB_PORT=3306 DB_DATABASE=fc-larvel DB_USERNAME=haha DB_PASSWORD=pwd123456
-
php artisan migrate
,做資料庫初始化工作 -
chmod -R 777 storage
;chmod -R 777 bootstrap/cache
, 保證這兩個目錄可寫 - 執行命令
php artisan key:generate
,生成app_key
- 將 Learn-Laravel-5 工程移到上述配置的 NAS 中, www 表示 Learn-Laravel-5 的工程的根目錄。
|-- index.py |-- www
index.py程式碼:
# -*- coding: utf-8 -*- import logging import os file = "/mnt/www/" def mkdir(path): folder = os.path.exists(path) if not folder: os.makedirs(path) def lsDir(): os.system("ls -ll /mnt/www/") def handler(event, context): mkdir(file) os.system("cp -r /code/www/* /mnt/www/") os.system("cp -a /code/www/.env /mnt/www/.env") print(lsDir()) return 'ok'
基於上述程式碼創一個函式 move-larvel-nas
, 執行函式,將 Learn-Laravel-5 工程包移動到 NAS 的 /mnt/www/
目錄。
注: 在第一步初始化的時候,可以使用資料庫的公網地址; 在函式計算的環境中,使用vpc,可以修改.env, 將其改為內網地址,更加安全
-
編寫函式,程式碼如下:
larvel 是單入口框架,所有的url都是經過
public/index.php
<?php use RingCentral\Psr7\Response; function startsWith($haystack, $needle) { $length = strlen($needle); return (substr($haystack, 0, $length) === $needle); } function handler($request, $context): Response{ $uri= $request->getAttribute("requestURI"); $isPhpScript = false; $root_dir = '/mnt/www'; $filename = $root_dir . explode("?", $uri)[0]; $filename = rawurldecode($filename); $pathinfo= pathinfo($filename); if(!isset($pathinfo['extension'])){ $isPhpScript = true; }else{ $extension = strtolower($pathinfo['extension']); if($extension == 'php'){ $isPhpScript = true; } } $proxy= $GLOBALS['fcPhpCgiProxy']; //php script if ($isPhpScript) { $host= "rayzhang.mofangdegisn.cn"; $resp= $proxy->requestPhpCgi($request, $root_dir, "index.php", ['SERVER_NAME' => $host, 'SERVER_PORT' => '80', 'HTTP_HOST' => $host, 'SCRIPT_FILENAME' => $root_dir . "/public/index.php", 'SCRIPT_NAME' => "/index.php"], ['debug_show_cgi_params' => true, 'readWriteTimeout' => 20000] ); return $resp; } else { // static files, js, css, jpg ... $handle= fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $headers = [ 'Content-Type'=> $proxy->getMimeType($filename), 'Cache-Control' => "max-age=8640000", 'Accept-Ranges' => 'bytes', ]; return new Response(200, $headers, $contents); } }
- 給函式入口配置自定義域名(操作過程請參考:繫結自定義域名示例), 具體配置假設如下:
注意: 繫結自定義域名之後,不用使用控制檯來進行除錯,就只能使用瀏覽器來觸發函式,日誌服務來進行除錯。
總結
FC 可以做為 Web Backend,只需要編寫一個函式實現傳統 Web 伺服器中的 conf 中的邏輯,就可以將一個完整的 Web 工程遷移到 FC ,從而從傳統的 Web 網站運維,監控等繁瑣的事務中解放出來。
最後歡迎大家通過掃碼加入我們使用者群中,搭建過程中有問題或者有其他問題可以在群裡提出來。
函式計算官網客戶群(11721331)。
