63
2014/06/19 shin1x1 第16回関西PHP勉強会 Heroku で作る スケーラブルな PHP アプリケーション

Heroku で作るスケーラブルな PHP アプリケーション

Embed Size (px)

DESCRIPTION

2014/06/19 第16回関西PHP勉強会

Citation preview

Page 1: Heroku で作るスケーラブルな PHP アプリケーション

2014/06/19 shin1x1

第16回関西PHP勉強会

Heroku で作るスケーラブルな

PHP アプリケーション

Page 2: Heroku で作るスケーラブルな PHP アプリケーション

Agenda

(c) 2014 Masashi Shinbara @shin1x1

• Heroku とは • PHPアプリケーションのデプロイ • スケーラブルな構成を作る • 大事なこと • まとめ

Page 3: Heroku で作るスケーラブルな PHP アプリケーション

Herokuとは

(c) 2014 Masashi Shinbara @shin1x1

Page 4: Heroku で作るスケーラブルな PHP アプリケーション

• Salesforce 社が運営する PaaS • 2007年サービスイン • 3,000,000以上のアプリケーションが稼働 • Ruby をはじめとして、複数言語の実行環境 • PHPが 2014/04 に正式対応

(c) 2014 Masashi Shinbara @shin1x1

Herokuとは

Page 5: Heroku で作るスケーラブルな PHP アプリケーション

• git push だけで、デプロイ完了 • ミドルウェアのインストールや設定などが不要 • 無料枠があり、かなり試すことができる • 豊富な動作実績があり、ノウハウも多数ある

(c) 2014 Masashi Shinbara @shin1x1

Herokuのうれしいところ

Page 6: Heroku で作るスケーラブルな PHP アプリケーション

• PHP 5.5.12 / 5.5.13 • HHVM 3.1.0 • 拡張も追加可能(mbstring, memcached等) • Composer が使える

(c) 2014 Masashi Shinbara @shin1x1

HerokuでのPHP

Page 7: Heroku で作るスケーラブルな PHP アプリケーション

• PostgreSQL / MySQL / KVS • メール送受信 • ログ転送 • 監視 • キュー • etc

(c) 2014 Masashi Shinbara @shin1x1

豊富なアドオン

Page 8: Heroku で作るスケーラブルな PHP アプリケーション

PHPアプリケーションのデプロイ

(c) 2014 Masashi Shinbara @shin1x1

Page 9: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

PHPアプリケーション

$ ls!index.php!!$ cat index.php!<?php phpinfo();!!$ git init!$ git add .; git commit -m “init”

Page 10: Heroku で作るスケーラブルな PHP アプリケーション

1. heroku.com でサインアップ(サインアップ)

2. Heroku Toolbelt インストール

3. Heroku でアプリケーションを作成

4. PHPアプリケーションをデプロイ

(c) 2014 Masashi Shinbara @shin1x1

Quick Start

Page 11: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

サインアップ

https://id.heroku.com/signup

Page 12: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Heroku Toolbelt

• Heroku クライアントツールセット • OSX, Windows, Debian / Ubuntsu • Heroku の操作を CLI で行う • Web 画面でも可能だが、あると便利

Page 13: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Heroku Toolbelt

https://devcenter.heroku.com/articles/quickstart

Page 14: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Heroku Toolbelt

$ heroku login!Enter your Heroku credentials.!Email: [email protected]!Password (typing will be hidden):!Two-factor code: xxxxx!Authentication successful.!

• ログイン

2要素認証も可能

Page 15: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

アプリケーション作成!$ heroku create -r heroku!Creating xxx-xxxx-xxxx... done, stack is cedar!http://xxx-xxxx-xxxx.herokuapp.com/ | [email protected]:xxx-xxxx-xxxx.git!

•アプリ名は、ランダムで自動生成 •̶app NAME で、アプリ名を指定することも可能 •-r heroku で、「git remote add heroku URL」を実行

Page 16: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

デプロイ!$ git push heroku master!Initializing repository, done.!Counting objects: 3, done.!Writing objects: 100% (3/3), 218 bytes | 0 bytes/s, done.!Total 3 (delta 0), reused 0 (delta 0) !(snip)!-----> Setting up runtime environment...! - PHP 5.5.12! - Apache 2.4.9! - Nginx 1.4.6!

Page 17: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

完了!

!$ heroku open!

Page 18: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

管理画面

Page 19: Heroku で作るスケーラブルな PHP アプリケーション

スケーラブルな構成を作る

(c) 2014 Masashi Shinbara @shin1x1

Page 20: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

アプリケーション

• 画像アップロードサイト • ログイン認証 • 画像アップロード • Laravel 4.2

https://github.com/shin1x1/laravel-on-heroku

Page 21: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

アプリケーション

https://github.com/shin1x1/laravel-on-heroku

Page 22: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

シンプルな構成

Apache / PHP

Log DB

Session File

Page 23: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

シンプルな構成

• 全てが 1 台のサーバにある • Apache(nginx) / PHP / DB etc • スケールアップしかできない

Page 24: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Apache / PHP

Log DB

Session File

スケーラブルな構成

Page 25: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

スケーラブルな構成

DB

FileSession

Log Apache / PHP

Page 26: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

スケーラブルな構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Page 27: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

スケーラブルな構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Page 28: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuでの構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Dyno

Page 29: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuでの構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Dyno

Dyno

Dyno

Page 30: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuでの構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Dyno

Dyno

Dyno

3 Dynos

Page 31: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuでの構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Add-ons Add-ons

Page 32: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuでの構成

Apache / PHP

DB

FileSession

Log Apache / PHP

Apache / PHP

Page 33: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

スケーラブルな構成

• Dyno(PHPサーバ)は、ステートレスにする • Dyno を増やしてスケールさせる • アプリケーションデータは、アドオンに置く

Page 34: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

データベース

• Heroku Postgres(無料枠あり) • 接続情報を環境変数から取得DATABASE_URL=postgres://xxxxx:[email protected]:5432/XXXXXXX

• parse_url() でパース • アドオンは、ほぼこのパターン

Page 35: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

データベース<?php!$url = parse_url(getenv('DATABASE_URL'));

array(6) {! 'scheme' =>! string(8) "postgres"! 'host' =>! string(43) "ec2-nnn-nnn-nnn-nnn.compute-1.amazonaws.com"! 'port' =>! int(5432)! 'user' =>! string(4) "user"! 'pass' =>! string(4) "pass"! 'path' =>! string(7) "/dbname"!}

Page 36: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

データベース

<?php!$url = parse_url(getenv('DATABASE_URL'));!!$dsn = sprintf(‘pgsql:host=%s;dbname=%s',! $url['host'], substr($url['path'], 1));!!$pdo = new PDO($dsn, $url['user'], $url['pass']);

• PDO の例

Page 37: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

データベース

<?php!$postgresqlUrl = parse_url(getenv('DATABASE_URL'));!!return [! 'default' => 'pgsql',! 'connections' => [! 'pgsql' => [! 'driver' => 'pgsql',! 'host' => $postgresqlUrl['host'],! 'database' => substr($postgresqlUrl['path'], 1),! 'username' => $postgresqlUrl['user'],! 'password' => $postgresqlUrl['pass'],! 'charset' => 'utf8',! 'prefix' => '',! 'schema' => 'public',! ],! ],!];

• Laravel の例

Page 38: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

データベース

http://qiita.com/shin1x1/items/68732dcf02a93c0a0fbb

Page 39: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ログイン認証(セッション)• Redis でセッション情報を共有 • Redis To Go(無料枠あり) • 接続情報を環境変数から取得

• composer.json で redis を有効に

REDISTOGO_URL=redis://redistogo:[email protected]:PORT/

Page 40: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ログイン認証(セッション)

<?php!$url = parse_url(getenv('REDISTOGO_URL'));!!$redisServer = sprintf(‘tcp://%s:%d?auth=%s',! $url['host'], $url['port'], $url['pass']);!!ini_set('session.save_handler', 'redis');!ini_set('session.save_path', $redisServer);!

• redis セッションハンドラの例

Page 41: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ログイン認証(セッション)

http://qiita.com/shin1x1/items/43181cd0487c72cb87a3

Page 42: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

画像ファイル

• S3 に保存(アドオンではない) • AWS SDK for PHP で実装 • 接続情報は、環境変数にセットしておくと良い • heroku config:set で環境変数をセット$ heroku config:set AWS_ACCESS_ID=xxxx!$ heroku config:set AWS_ACCESS_SECRET_KEY=xxxx

Page 43: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ログ

• ログは、stdout or stderr に出力 • PaperTail などのアドオンで保存 • 複数 Dyno のログをアドオンに集約 • monolog / file_put_contents()

Page 44: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ログ

Page 45: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

デモ

アクセスする Dyno が違う

Page 46: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

デモ

アクセスする Dyno が違う

Page 47: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

デモ

アクセスする Dyno が違う

Page 48: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

デモ

アクセスする Dyno が違う

Page 49: Heroku で作るスケーラブルな PHP アプリケーション

大事なこと

(c) 2014 Masashi Shinbara @shin1x1

Page 50: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

大事なこと

• ファイルに保存したデータは消える!! • デプロイ、環境変数変更等の操作 • 1日1回の再起動時 • アクセスが無い場合、自動停止 (Dyno が 1 台構成だとアクセスが無い時間は、 自動でスリープする)

Page 51: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ファイルが消える

<?php!const FILE_PATH = '/tmp/file';!!file_put_contents(FILE_PATH, ! date('Y/m/d H:i:s') . '<br>', FILE_APPEND);!!echo file_get_contents(FILE_PATH);

• file.php として、デプロイ

Page 52: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ファイルが消える

• ブラウザでアクセス

Page 53: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ファイルが消える

• 何度かリロード

Page 54: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

ファイルが消える

$ echo “//foo” >> file.php!$ git add file.php; git commit -m “add comment”!$ git push heroku master

• 変更して、再デプロイ

• ブラウザでアクセスすると、消えている

Page 55: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuとは

Heroku では、データファイルは消える

Page 56: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuとは

Heroku では、データファイルは消える

データは全て外部に逃す

Page 57: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Herokuとは

Heroku では、データファイルは消える

データは全て外部に逃す

スケーラブルなアプリケーションに!!

Page 58: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

Heroku では、データファイルは消える

データは全て外部に逃す

スケーラブルなアプリケーションに!!

Herokuは スケーラブルな アプリケーション 矯正ギプス

Herokuとは

Page 59: Heroku で作るスケーラブルな PHP アプリケーション

まとめ

(c) 2014 Masashi Shinbara @shin1x1

Page 60: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

まとめ

• Heroku は、無料から使える • アドオンの活用が肝 • スケーラブルアプリケーション矯正ギプス

Page 61: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

参考

https://devcenter.heroku.com/categories/php

Page 62: Heroku で作るスケーラブルな PHP アプリケーション

(c) 2014 Masashi Shinbara @shin1x1

参考

http://tatsu-zine.com/books/heropro

Page 63: Heroku で作るスケーラブルな PHP アプリケーション

@shin1x1

(c) 2014 Masashi Shinbara @shin1x1