1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【環境構築】[CakePHP 2.10.x] を [AWS Elasticbeanstalk] にデプロイする

Last updated at Posted at 2017-11-04

はじめに

下記のような経緯で記事を書くことにした。

  • AWS Elasticbeanstalk に CakePHP 2.x デプロイしたい
  • AWS公式に CakePHP 3.x の記事はあるけど、2.xがなくて困った
  • AWS Elasticbeanstalk 自体に詳しくない
  • compser をあまり使ったことない
  • 運用時のデプロイを考えると、 AWS Elasticbeanstalk は本気で便利
  • だから出来るようになりたい

想定読者

想定読者は下記の通りである。

  • AWS の概要と基本操作が分かる
  • bashコマンドが分かる
  • composerが分かる
  • CakePHP 2.x を使っている
  • あわよくば AWS Elasticbeanstalk に CakePHP をデプロイしてみたい

実行環境

実行環境は下記の通りである。

項目 内容 補足
OS Amazon Linux *Elasticbeanstalk で適当なPHPプラットフォームのEC2を起動しました。
shell bash Amazon Linux デフォルト
PHP PHP 5.6 -
CakePHP CakePHP 2.10.x -

事前準備

  • AWSアカウント発行済み
  • AWS Elasticbeanstalk を使用して適当なPHP-Webアプリケーション環境を起動済み
  • 起動したEC2にsshで接続済み

手順

1. プロジェクトファイルの作成と CakePHP 2.x のインストール

Command
// working directory
$ pwd
/home/ec2-user

// Create Project File & change directory
$ mkdir cakephpapp && cd $_

// Install Composer
$ curl -s https://getcomposer.org/installer | php

// Create composer.json
$ cat << EOT > composer.json
{
    "name": "example-app",
    "repositories": [
        {
            "type": "pear",
            "url": "http://pear.cakephp.org"
        }
    ],
    "require": {
        "cakephp/cakephp": ">=2.6.4,<3.0.0"
    },
    "require-dev": {
        "phpunit/phpunit": "3.7.37",
        "cakephp/debug_kit" : ">=2.2.4,<3.0.0"
    },
    "config": {
        "vendor-dir": "vendors/"
    },
    "extra": {
        "installer-paths": {
            "./plugins/{$name}/": [
                "cakephp/debug_kit"
            ]
        }
    }
}
EOT

// Install by composer
$ php composer.phar install

// bake by CakePHP bake
$ vendors/bin/cake bake project app
Welcome to CakePHP v2.10.4 Console
---------------------------------------------------------------
App : cakephpapp
Path: /home/ec2-user/cakephpapp/
---------------------------------------------------------------
Skel Directory: /home/ec2-user/cakephpapp/vendors/cakephp/cakephp/lib/Cake/Console/Templates/skel
Will be copied to: /home/ec2-user/cakephpapp/app
---------------------------------------------------------------
Look okay? (y/n/q)
[Mon Oct 30 06:16:33.160898 2017] [suexec:notice] [pid 3259] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Oct 30 06:16:33.179514 2017] [lbmethod_heartbeat:notice] [pid 3259] AH02282: No slotmem from mod_heartmonitor
[Mon Oct 30 06:16:33.226011 2017] [mpm_prefork:notice] [pid 3259] AH00163: Apache/2.4.27 (Amazon) configured -- resuming normal operations
[Mon Oct 30 06:16:33.226028 2017] [core:notice] [pid 3259] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Oct 30 07:11:52.345323 2017] [autoindex:error] [pid 3268] [client 155.94.88.58:41612] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
[Mon Oct 30 10:59:17.302442 2017] [autoindex:error] [pid 3269] [client 221.243.136.230:62412] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
[Mon Oct 30 10:59:21.981894 2017] [autoindex:error] [pid 3269] [client 221.243.136.230:62412] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
[Mon Oct 30 10:59:35.698374 2017] [autoindex:error] [pid 3269] [client 221.243.136.230:62412] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive
...skipping...
[y] > y
---------------------------------------------------------------
Created: app in /home/ec2-user/cakephpapp/app
---------------------------------------------------------------
 * Random hash key created for 'Security.salt'
 * Random seed created for 'Security.cipherSeed'
 * Cache prefix set
 * app/Console/cake.php path set.
CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.
You can fix this by adding CakePHP to your `include_path`.
 * CAKE_CORE_INCLUDE_PATH set to /home/ec2-user/cakephpapp/vendors/cakephp/cakephp/lib in webroot/index.php
 * CAKE_CORE_INCLUDE_PATH set to /home/ec2-user/cakephpapp/vendors/cakephp/cakephp/lib in webroot/test.php
   * Remember to check these values after moving to production server
Project baked successfully!

2. CakePHP のPATH調整

bakeした際に絶対パス指定になっているため、下記のファイルの記載を修正する。

app/webroot/index.php
define('CAKE_CORE_INCLUDE_PATH',  ROOT . DS . 'vendors' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

app/webroot/test.php
define('CAKE_CORE_INCLUDE_PATH',  ROOT . DS . 'vendors' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

3. Elasticbeanstalk で確認出来るようPATHを調整

Command
// Elasticbeanstalk のアプリケーション・ディレクトリにプロジェクト・ファイルを移動させる
$ sudo su -
# mv /home/ec2-user/cakephpapp /var/app
# cd /var/app
# chown -R webapp:webapp cakephpapp

// WebRootとしているディレクトリへのショートカット・リンクを上書きする
# cd /var/www/
# ln -sfn /var/app/cakephp/app/webroot html

ここまで来たら、AWS Elasticbeanstalk の Webコンソールに表示されたURLから、正常にhttpアクセス出来るかを確認する。

問題がなければ、下記の手順に進む。

4. .ebextionsionsの作成

Command
# pwd
/var/app/cakephpapp

// .ebextionsions ディレクトリの作成
# mkdir .ebextensions && cd $_

// WebRootのショートカット・リンクを、デプロイ時に自動で設定されるように記述する。  
# cat << EOT > change_webroot.config
commands:
  change_webroot:
    command: ln -sfn /var/app/current/app/webroot html
    cwd: /var/www
EOT

5. bundleファイルの作成

Command
# pwd
/var/app/cakephpapp

// bundle ファイルの作成
# zip ../cakephpapp.zip -r * .[^.]*
  • zipファイルをローカルにダウンロード
  • Elasticbeanstalk の Webコンソールから、bundleファイルをアップロード
  • 動作確認
  • httpアクセスして、正常にWebページを見ることが出来たら完了!

6. 残タスク

基本的には前述の手順まででOK。プラスアルファで下記をやっておくと更に良い。

  • .gitignoreの導入

関連技術

今回の記事の主要技術は下記の通り。何か不明点などあれば、これらの単語からググると解決出来るはず。

  • composer
  • AWS Elasticbeanstalk
  • .ebextensions

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?