LoginSignup
7
6

More than 5 years have passed since last update.

CapistranoでCakePHPのデプロイ時にはまったこと

Last updated at Posted at 2014-12-12

常識かもしれませんが、はまったので書きます。

TL;DR

Capistranoを使用したCakePHPのデプロイ時には

  • キャッシュを削除
  • webサーバを再起動

したほうが良さげ?

前提

  • CakePHP 2.4.10
  • 引継ぎ案件、運用中
  • 訳あってファイルキャッシュ
  • もろもろサーバ数は6+α。やってられないのでcap3導入
  • 微妙なディレクトリ構成
  • AWS ELB使用

DocumentRootは、/var/www/html
デプロイ先は、/home/cap
以下のようなシンボリックリンクを貼って対応

/var/www/html/cake -> /home/cap/current/path/to/app

問題1 デプロイ直後にPHP Fatal error: Class 'XXX' not found in ...

app/tmp/cache/persistent/xxx_cake_core_file_map にデプロイ前のパスが設定されたままで、アクセス時に古いリリースを参照するためエラーになっていた。

対応

デプロイ時にファイルキャッシュを削除する(すみません、力技です)。

deploy.rb
set :cake_cache, '/path/to/cake/app/tmp/cache'
set :cache_dirs, %W{#{fetch(:cake_cache)}/models #{fetch(:cake_cache)}/persistent #{fetch(:cake_cache)}/views}

namespace :misc do

  desc "Clear cache in :cache_dirs."
  task :clear_cache do
    on roles(:web) do
      dirs = fetch(:cache_dirs)
      if dirs.is_a?(Array)
        dirs.each do |d|
          path = shared_path.join(d)
          if test "[ -d #{path} ]"
            execute "rm -f #{path}/*"
          end
        end
      end
    end
  end

end

after 'deploy:updated', 'misc:clear_cache'
after 'deploy:reverted', 'misc:clear_cache'

問題2 たま〜にデプロイ直後にPHP Fatal error:(ry

キャッシュ削除前のapacheのプロセスが残っていたため、デプロイ後にデプロイ前のパスが設定されたキャッシュが作成された、と考えた。

対応

apacheを再起動する

deploy.rb
namespace :deploy do

  desc 'Restart httpd'
  task :restart do
    on roles(:web) do
      execute :sudo, :service, "httpd", "graceful"
    end
  end

  after :publishing, :restart
  after :finishing, 'deploy:cleanup'

end

まとめ

とりあえず、問題2の対応以後は、問題なくデプロイ出来ています。
あまりCakePHP関係なくてすみません。

7
6
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
7
6