2015/01/16 shin1x1
GoAzure 2015
Azure Websites で作る
スケーラブルな

PHPアプリケーション
2015/01/16 shin1x1
GoAzure 2015
LAMP(LAPP)ユーザが使う
Azure Websites
Agenda
(c) 2015 Masashi Shinbara @shin1x1
• Azure Websites とは
• Azure Websites の PHP環境
• スケーラブルな PHP アプリケーション
• まとめ
(c) 2015 Masashi Shinbara @shin1x1
Azure Websites
Azure Websites
(c) 2015 Masashi Shinbara @shin1x1
• Azure の PaaS

(Platform as a Service)
• デプロイするだけで、すぐに公開できる
• 柔軟なスケール / オートスケール
• .NET、Java、PHP、Node.js、Python
詳しくは
(c) 2015 Masashi Shinbara @shin1x1
http://azure.microsoft.com/ja-jp/services/websites/
おさえておく特徴
(c) 2015 Masashi Shinbara @shin1x1
• 東日本 / 西日本リージョン
• 多様なデプロイ方法( Git あり)
• コマンドラインツール( azure コマンド)
• Windows Server + IIS
• DB や KVS 等は、別サービスと連携
Webインスタンスを作って、デプロイする
$ git init
$ echo “<?php phpinfo();” > index.php
$ git add .
$ git commit -m ‘init’
$ azure
$ azure site create shin1x1-goazure --location "Japan
East"
$ git push azure master
$ open http://shin1x1-goazure.azurewebsites.net
PHPバージョン変更
$ azure site set --php-version 5.5
$ azure portal
管理サイト(ポータル)を開く
Monaco でシェル操作
操作ツール
(c) 2015 Masashi Shinbara @shin1x1
• 現ポータルサイト
• 新ポータルサイト
• コマンドラインツール
• Visual Studio Online Monaco
• Kudu
(c) 2015 Masashi Shinbara @shin1x1
PHP環境
PHP
(c) 2015 Masashi Shinbara @shin1x1
• Windows 版 PHP
• 32bit / 64bit
• 5.3 / 5.4 / 5.5 / 5.6
• ランタイムを追加することも可能
PHPランタイムの追加
(c) 2015 Masashi Shinbara @shin1x1
http://azure.microsoft.com/ja-jp/documentation/articles/
web-sites-php-configure/#UseCustomPHP
PHP拡張
(c) 2015 Masashi Shinbara @shin1x1
• 主要なものは有効

(mbstring / pdo / opcache 等)
• DLL を追加して、利用可能
PHP拡張の追加
(c) 2015 Masashi Shinbara @shin1x1
http://azure.microsoft.com/ja-jp/documentation/articles/
web-sites-php-configure/#EnableExtDefaultPHP
Composer
(c) 2015 Masashi Shinbara @shin1x1
• デフォルトでは、未対応
• Websites 上で直接実行する

(Monaco / Kudo)
• デプロイスクリプトを利用すれば、

デプロイ時に composer install
デプロイスクリプトのひな形を生成
$ azure site deploymentscript --php -t bash
# .deployment deploy.sh が生成される
deploy.sh を編集
$ vim deploy.sh
# 下記を DEPLOYMENT セクション上に追加
curl -sS https://getcomposer.org/installer | php
php composer.phar install --prefer-dist --no-dev
デプロイ時に composer install 実行
$ git push azure master
(snip)
remote: Loading composer repositories with package information
remote: Installing dependencies from lock file
remote: - Installing shin1x1/sample (dev-master 565c62e)
remote: Downloading: connection... Downloading: 100%
remote: Extracting archive
remote:
(snip)
.htaccess
(c) 2015 Masashi Shinbara @shin1x1
• IIS なので、.htaccess は対応しない
• 設定を Web.Config に記述
• .htaccess -> Web.Config 変換サイト
http://cbsa.com.br/tools/online-convert-htaccess-to-web-config.aspx
Azure SDK for PHP
(c) 2015 Masashi Shinbara @shin1x1
• Azure サービスを PHP から操作
• Composer / PEAR でインストール
• 開発が、ややのんびり?
(c) 2015 Masashi Shinbara @shin1x1
スケーラブルな

PHPアプリケーション
The Twelve-Factor App
(c) 2015 Masashi Shinbara @shin1x1
• アプリケーションは、プロセスとして実行
• プロセスは、ステートレスで、共有しない
• データは、バックエンドサービスに保存すべき
• Sticky session には依存しない
http://12factor.net/processes
サンプルアプリケーション
(c) 2015 Masashi Shinbara @shin1x1
• 会員制画像投稿サイト
• Laravel 4.2
• ログイン / 画像登録 / 画像閲覧
会員制画像投稿サイト
Laravel 4.2
ログイン
画像登録 / 画像閲覧
アプリケーションデータ
(c) 2015 Masashi Shinbara @shin1x1
• ユーザデータ = ClearDB(MySQL)Free
• セッション = Webインスタンス(ファイル)
• 画像 = Webインスタンス(ファイル)
構成
(c) 2015 Masashi Shinbara @shin1x1
セッション
ClearDB
写真
ユーザデータ
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
セッション
ClearDB
写真
セッション 写真
ユーザデータ
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
ログイン
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
セッション
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
セッション
会員ページ
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
セッション
セッションが無い!
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
セッション
ログアウト
検証
(c) 2015 Masashi Shinbara @shin1x1
• 2インスタンス構成
• ログインして、会員ページを何度もリロード
• ログアウトするはず
ログアウトしない!
Sticky Session
(c) 2015 Masashi Shinbara @shin1x1
• デフォルトで、LB の Sticky Sessionが有効
• リロードだと、常に同じインスタンスへアクセス
• Arr-Disable-Session-Affinity ヘッダで

無効にできる
http://blogs.msdn.com/b/windowsazurej/archive/2013/11/25/blog-
disabling-arrs-instance-affinity-in-windows-azure-web-sites.aspx
検証2
(c) 2015 Masashi Shinbara @shin1x1
• WebサイトにインスタンスIDを表示
• Sticky Session はオフに
• インスタンスID が変化することは確認済
ログアウトしない!!
なぜか???
インスタンス2台構成
(c) 2015 Masashi Shinbara @shin1x1
セッション 写真
セッション 写真
おそらく…
(c) 2015 Masashi Shinbara @shin1x1
セッション
写真
(c) 2015 Masashi Shinbara @shin1x1
セッション
写真
Azure Storage ??
おそらく…
インスタンスのストレージ?
(c) 2015 Masashi Shinbara @shin1x1
C:
共有されていない
D:
共有されている?
(c) 2015 Masashi Shinbara @shin1x1
C:
D:
デプロイしたアプリケーション

D:homesitewwwroot
インスタンスのストレージ?
(c) 2015 Masashi Shinbara @shin1x1
C:
D:
Laravel でのセッション
D:homesitewwwroot
appstoragesessions
インスタンスのストレージ?
(c) 2015 Masashi Shinbara @shin1x1
C:
D:
PHPデフォルトのセッション
C:DWASFilesSitesxxxxTemp
インスタンスのストレージ?
DEMO2
スケーラブルにするには
(c) 2015 Masashi Shinbara @shin1x1
データが D: にあれば
何もしなくて良い
スケーラブルにするには
(c) 2015 Masashi Shinbara @shin1x1
データが C: にあれば
別サービスに逃す
(セッションなら sticky session でも可)
デフォルト構成のまま
で良いか?
負荷を計測
(c) 2015 Masashi Shinbara @shin1x1
• jmeter で計測(大阪)
• 画像表示ページ

(DB = Read / セッション = Read / Write)
• 1インスタンス(Basic / S / Japan East)
ケース1
(c) 2015 Masashi Shinbara @shin1x1
ClearDB
• データベース = ClearDB (Free)
• Thread = 10 / Rumpup = 60 / Loop = 10
• Response time = 1,217ms
ケース2
(c) 2015 Masashi Shinbara @shin1x1
CentOS

MySQL
• データベース = Azure VM (Basic_A0)
• Thread = 10 / Rumpup = 60 / Loop = 10
• Response time = 188ms
1クエリの速度
(c) 2015 Masashi Shinbara @shin1x1
Azure VM(MySQL) = 2ms ∼ 20ms
ClearDB(Free)= 3ms ∼ 200ms
ケース3
(c) 2015 Masashi Shinbara @shin1x1
CentOS

MySQL
• セッション = ファイル(共有ディスク?)
• Thread = 60 / Rumpup = 60 / Loop = 10
• Response time = 3,892ms
ケース4
(c) 2015 Masashi Shinbara @shin1x1
CentOS

MySQL
• セッション = Azure Redis Cache
• Thread = 60 / Rumpup = 60 / Loop = 10
• Response time = 223ms
(c) 2015 Masashi Shinbara @shin1x1
スループット
req/sec
0
10
20
30
40
ケース1 ケース2 ケース3 ケース4
(c) 2015 Masashi Shinbara @shin1x1
• とりあえずは、そのままで良い

(Sticky session / 共有ディスク)
• 負荷に応じて、アプリケーションデータを

別サービスに逃がしていく
• Azure サービスなら、利用しやすい
スケーラブルにするには
(c) 2015 Masashi Shinbara @shin1x1
CDNによるコンテンツ配信
• Azure CDN
• Azure Websites をオリジンサーバに
• エッジロケーションから高速配信
(c) 2015 Masashi Shinbara @shin1x1
まとめ
(c) 2015 Masashi Shinbara @shin1x1
• 日本国内にある PHP の PaaS
• 無料枠で、簡単に試せる
• 元からスケーラブルな構成
• Azure サービスとの親和性が高い
• Windows ユーザだけに使わせるのは勿体無い
まとめ
@shin1x1
(c) 2015 Masashi Shinbara @shin1x1

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