AWSSAM

Try to run WordPress on AWS Lambda

Today, AWS announced a new feature about AWS Lambda. We can create Lambda function by custom runtime. So, I&#8 […]

広告ここから
広告ここまで

Today, AWS announced a new feature about AWS Lambda. We can create Lambda function by custom runtime.

So, I’ll try to create AWS Lambda using PHP runtime and put the WordPress.

Getting started

We can create PHP function by following project.

PHP Layer For AWS Lambda

The example can deploy source code by SAM.

$ mkdir my-php-app
$ cd my-php-app
$ touch template.yaml

You can create SAM templage (template.yaml) by following.

AWSTemplateFormatVersion: 2010-09-09
Description: My PHP Application
Transform: AWS::Serverless-2016-10-31
Resources:
  phpserver:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-phpserver
      Description: PHP Webserver
      CodeUri: src/server
      Runtime: provided
      Handler: router.php
      MemorySize: 3008
      Timeout: 30
      Tracing: Active
      Layers:
        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:3
      Events:
        api:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY

The property Layercan define a custom library, and the example selected PHP layer.

Prepare PHP files

Next, we need to create php files.

$ mkdir -p src/server
$ touch src/server/router.php

And src/server/router.php is following code.

<?php
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$_SERVER['SCRIPT_NAME'])) {
  // Serve static files directly
  return false;
}

// Run index.php for all requests
$_SERVER['SCRIPT_NAME'] = '/index.php';
require 'index.php';
?>

And get WordPress source code and place these files.

$ wget https://github.com/WordPress/WordPress/archive/master.zip
$ unzip master.zip
$ mv WordPress-master/ src/server

The project directory is following structure.

$ tree -L 3
.
├── src
│   └── server
│       ├── index.php
│       ├── license.txt
│       ├── readme.html
│       ├── router.php
│       ├── wp-activate.php
│       ├── wp-admin
│       ├── wp-blog-header.php
│       ├── wp-comments-post.php
│       ├── wp-config-sample.php
│       ├── wp-content
│       ├── wp-cron.php
│       ├── wp-includes
│       ├── wp-links-opml.php
│       ├── wp-load.php
│       ├── wp-login.php
│       ├── wp-mail.php
│       ├── wp-settings.php
│       ├── wp-signup.php
│       ├── wp-trackback.php
│       └── xmlrpc.php
└── template.yaml

Deploy it!

Finally, we can deploy the function by SAM.

$ sam package 
    --template-file template.yaml 
    --output-template-file serverless-output.yaml 
    --s3-bucket <your SAM deployment bucket created above>

$ sam deploy 
    --template-file serverless-output.yaml 
    --stack-name my-first-serverless-php-service 
    --capabilities CAPABILITY_IAM

The example does not include Output parameters, so we have to get the API endpoint from the API Gateway console. The resource name ismy-first-serverless-php-servicefor default.

You can access by the following path. https://xxxxx.execute-api.us-east-1.amazonaws.com/Prod/index

But it does not works.

I’ve checked the log file. The returned value is following.

{
  "multiValueHeaders": {
    "Host": [
      "localhost:8000",
      "localhost:8000"
    ],
    "Date": [
      "Thu, 29 Nov 2018 18:35:22 +0000",
      "Thu, 29 Nov 2018 18:35:22 +0000"
    ],
    "Connection": [
      "close",
      "close"
    ],
    "X-Powered-By": [
      "PHP/7.1.7",
      "PHP/7.1.7"
    ],
    "Location": [
      "https://localhost:8000/index.php/path/to/wp-admin/setup-config.php"
    ],
    "Content-type": [
      "text/html; charset=UTF-8"
    ],
    "Content-Type": [
      "text/html; charset=utf-8"
    ]
  },
  "body": "Your PHP installation appears to be missing the MySQL extension which is required by WordPress.",
  "statusCode": 500
}

The function returns Your PHP installation appears to be missing the MySQL extension which is required by WordPress. Error.
So we have to add MySQL extensions into the function.

And the function executed a redirection.

[Thu Nov 29 18:35:22 2018] 127.0.0.1:40630 [302]: /path/to/resource
[Thu Nov 29 18:35:22 2018] 127.0.0.1:40632 [500]: /index.php/path/to/wp-admin/setup-config.php

Conclusion

There are a lot of issues to run the WordPress into the AWS Lambda.

  • Where should we place the wp-content files?
  • How should we connect Aurora Serverless DB?
  • How should we added some extensions to the Lambda?

If you interested the topic, please try it yourself!

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts