みーのぺーじ

みーが趣味でやっているPCやソフトウェアについて.Python, Javascript, Processing, Unityなど.

DrupalをGoogle App Engineにデプロイする

Drupalでサイトを運営するにはサーバーの管理が必要で,nginxの設定とかアップロードとかが面倒なうえに余計なことをするとサイトが停止してしまう.以前,Amazon EC2のUbuntuでnginxとphp-fpmとDrupal 7という記事でPaaS (Platform as a Service)なサーバーについてまとめたが,ここでは SaaS (Software as a Service)なサーピスであるGoogle App Engine (以下GAE)に Drupal 7をデプロイする方法をまとめる.

GAE用のDrupal 7を作る

GAEではPHPが使えるためDrupalが動く.しかしデプロイしたサーバーのファイルは読み込み専用になるなど,特殊な仕様が幾つかあるのでGAE用のDrupal 7を作る必要がある.

drush (Drupal Shell)を使える環境を用意する.Ubuntuならapt-getで一発なので,みーはUbuntuを使った.デスクトップにDrupalが出力されるフォルダ(drupal-gae)を作成し,drush makeを実行する.

sudo apt-get install drush
cd ~/Desktop
mkdir drupal-gae
cd drupal-gae
drush make http://drupalcode.org/project/google_appengine.git/blob_plain/refs/heads/7.x-1.x:/root/drupal-full.make

これで最新のDrupal 7がGAE用に修正される.(参考: https://www.drupal.org/project/google_appengine)

GAEにDrupalをデプロイする

GAEにデプロイするには,Google App Engine SDK [download]を使う.Macなら,SDKに含まれる GoogleAppEngineLauncher.app を使うのが簡単だ.drupal-gaeの中身をデプロイするわけだが,幾つか設定が必要なので,順に行う.

sites/default/settings.php

sites/default/default.settings.phpを複製してリネームする.

以下を Database settingsの後あたりに追加する.Google Cloud SQL (GCS)でDrupal用データベースを1つ作成する.GCSの使い方はここにまとめた.usernameはroot,passwordは""でよい.

    $databases["default"]["default"] = array(
        "database" => "<database name>",
        "username" => "root",
        "password" => "",
        "unix_socket" => "/cloudsql/<instance ID (xxxx:xxx)>",
        "port" => "",
        "host" => "",
        "driver" => "mysql",
        "prefix" => "",
        );

memcacheを有効にするために,以下を最後に追加する.memcache_key_prefixは固有の文字列ならなんでもよい.

    $conf["cache_backends"][] = "sites/all/modules/memcache/memcache.inc";
    $conf["cache_class_cache_form"] = "DrupalDatabaseCache";
    $conf["cache_default_class"] = "MemCacheDrupal";
    $conf["memcache_key_prefix"] = "xxxx";

(参考: http://blog.boombatower.com/drupal-integration-module-google-app-engine)

File Systemはあらかじめ設定しておくのが簡単のようだ.以下を最後に追加する.

$conf["file_public_path"] = "drupal/sites/default/files";
$conf["file_private_path"] = "private/default";
$conf["file_temporary_path"] = "temp/default";

app.yaml

    application: <application ID>
    version: 1
    runtime: php
    api_version: 1
    threadsafe: true

    handlers:
    - url: /system/(files|public|temporary)/(.+)
      script: mod_rewrite.php

    - url: /(.*\.(ico|jpg|png|gif|htm|html|css|js))$
      static_files: \1
      upload: (.*\.(ico|jpg|png|gif|htm|html|css|js))$
      application_readable: true

    - url: /system/queue
      script: mod_rewrite.php
      login: admin

    - url: /.*
      script: mod_rewrite.php

    instance_class: B1
    basic_scaling:
      max_instances: 2
      idle_timeout: 10m

GAEは自動でスケーリングしてくれるのだが,初期設定では費用がかかるので,Automatic ScalingではなくBasic Scalingとした.インスタンスは最大2つに制限している.

以上の設定ができたら,GAEにデプロイする.

Drupalのインストール

デプロイが正常にできていれば,https://xxx.appspot.com/install.phpにアクセスすると,Drupalのインストール画面が表示される.この状態では,Drupalはファイルシステムに書き込みができないため,Warning: file_put_contents(public:///.htaccess): failed to open stream という以下の様な警告が表示されるが,そのまま進める.

    Warning: file_put_contents(public:///.htaccess): failed to open stream: "DrupalPublicStreamWrapper::stream_open" call failed in file_create_htaccess() (line 494 of /base/data/home/apps/s~xxxxxxx/1-00.xxzxxxxxxxxx/includes/file.inc).
    Warning: file_put_contents(temporary:///.htaccess): failed to open stream: "DrupalTemporaryStreamWrapper::stream_open" call failed in file_create_htaccess() (line 494 of /base/data/home/apps/s~xxxxxxx/1-00.xxzxxxxxxxxx/includes/file.inc).

GAE用のmoduleを有効化

/admin/reports/statusにアクセスすると,以下のようにエラーが出ているので設定をしていく.

f:id:atsuhiro-me:20151104005450p:plain:w300

/admin/modulesにアクセスし,以下のmoduleを有効化する.

  • App Engine - Push Task Queues
  • Google App Engine
  • Memcache

/admin/config/media/file-systemにアクセスし,Google Cloud Storage の Default bucket を設定する.gs://は不要.

private

    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2183 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).
    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2183 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).
    Warning: mkdir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in drupal_mkdir() (line 2419 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/includes/file.inc).
    Warning: mkdir(): Invalid path in drupal_mkdir() (line 2419 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/includes/file.inc).
    The directory private:// does not exist and could not be created.
    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2189 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).
    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2189 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).
    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2194 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).
    Warning: is_dir(): Unable to find the wrapper "private" - did you forget to enable it when you configured PHP? in system_check_directory() (line 2194 of /base/data/home/apps/s~xxxxxxxxxxxx/1-10.cccccccccccccc/modules/system/system.module).

というエラー