How to Develop Function Compute with WordPress Applications

Background Information

Create a Fun Project

<?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");
$uriArr = explode("?", $uri);
// default php / or /wp-admin/
if (preg_match('#/$#', $uriArr[0]) && !(strpos($uri, '.php'))) {
$uriArr[0] .= "index.php";
$uri = implode($uriArr);
if (startsWith($uri, "/2016-08-15/proxy/share/wp-func/wp-admin/")) {
// wordpress admin entrypoint
$request = $request->withAttribute("requestURI", $uri);
}
}
$proxy = $GLOBALS['fcPhpCgiProxy'];
$root_dir = '/mnt/www';

//php script
if (preg_match('#\.php.*#', $uri)) {
$format = '%s.%s.fc.aliyuncs.com';
$host = sprintf($format, $context['accountId'], $context['region']); // maybe user define domain
$resp = $proxy->requestPhpCgi($request, $root_dir, "index.php",
['SERVER_NAME' => $host, 'SERVER_PORT' => '80', 'HTTP_HOST' => $host],
['debug_show_cgi_params' => false, 'readWriteTimeout' => 15000]
);
return $resp;
} else {
// static files, js, css, jpg ...
$filename = $root_dir . explode("?", $uri)[0];
$filename = rawurldecode($filename);
$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);
}
}
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
share:
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'fc wordpress test'
Policies:
- AliyunECSNetworkInterfaceManagementAccess
VpcConfig:
VpcId: 'vpc-bp12hm92gdpcjtai7ua82'
VSwitchIds: [ 'vsw-bp1gitru7oicyyb4uiylj' ]
SecurityGroupId: 'sg-bp1243pi65bw4cjj4bks'
NasConfig:
UserId: -1
GroupId: -1
MountPoints:
- ServerAddr: '012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com:/'
MountDir: '/mnt/www'
wp-func:
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: php7.2
CodeUri: './index.php'
Timeout: 100
Events:
http-test: # trigger name
Type: HTTP # http trigger
Properties:
AuthType: ANONYMOUS
Methods: ['GET', 'POST', 'PUT']
  1. Download WordPress.
  2. Decompress the downloaded WordPress.
  3. Copy the content of the decompressed WordPress to the locally simulated NAS directory.
mkdir -p .fun/nas/012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com/2016-08-15/proxy/share/wp-func
cp -r wordpress/ .fun/nas/012194b28f-ujc20.cn-hangzhou.nas.aliyuncs.com/2016-08-15/proxy/share/wp-func/

Original Source

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com