LoginSignup
55
49

More than 3 years have passed since last update.

Capistranoによる自動デプロイ トラブルシューティング

Last updated at Posted at 2018-04-20

capistranoでエラーが起きた時のトラブルのまとめ

環境

unicorn + nginx + rails + capistranoなど

目次

  • bundle installでfog unf_extエラー
  • Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
  • デプロイしたのに変更が反映されない
  • The "libcurl" package isn't availableのエラー
  • rake assets:precompileでこける
  • エラーが出たときのlogの見方

bundle installでfog unf_extエラー

エラー内容

00:08 bundler:install
      01 $HOME/.rbenv/bin/rbenv exec bundle install --path /var/www/~~~~/shared/bundle --without development test --deployment --quiet
      01 Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
      01
      01 current directory:
      01 /var/www/~~~~/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
      01 /home/ec2-user/.rbenv/versions/2.3.1/bin/ruby -r
      01 ./siteconf20180420-22373-1cb7c5f.rb extconf.rb
      01 checking for main() in -lstdc++... yes
      01 creating Makefile
      01
      01 To see why this extension failed to compile, please check the mkmf.log which can
      01 be found here:
      01
      01 /var/www/~~~~/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/unf_ext-0.0.7.5/mkmf.log
      01
      01 current directory:
      01 /var/www/chat-space/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
      01 make "DESTDIR=" clean
      01
      01 current directory:
      01 /var/www/chat-space/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
      01 make "DESTDIR="
      01 compiling unf.cc
      01 cc1plus: warning: command line option '-Wdeclaration-after-statement' is valid
      01 for C/ObjC but not for C++ [enabled by default]
      01 cc1plus: warning: command line option '-Wimplicit-function-declaration' is valid
      01 for C/ObjC but not for C++ [enabled by default]
      01 virtual memory exhausted: Cannot allocate memory
      01 make: *** [unf.o] Error 1
      01
      01 make failed, exit code 2
      01
      01 Gem files will remain installed in
      01 /var/www/-----/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5 for
      01 inspection.
      01 Results logged to
      01 /var/www/-----/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/unf_ext-0.0.7.5/gem_make.out
      01
      01 An error occurred while installing unf_ext (0.0.7.5), and Bundler cannot
      01 continue.
      01 Make sure that `gem install unf_ext -v '0.0.7.5'` succeeds before bundling.
      01
      01 In Gemfile:
      01   fog was resolved to 2.0.0, which depends on
      01     fog-ovirt was resolved to 1.0.3, which depends on
      01       rbovirt was resolved to 0.1.5, which depends on
      01         rest-client was resolved to 2.0.2, which depends on
      01           http-cookie was resolved to 1.0.3, which depends on
      01             domain_name was resolved to 0.5.20180417, which depends on
      01               unf was resolved to 0.1.4, which depends on
      01                 unf_ext
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@-------: bundle exit status: 5
bundle stdout: Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/var/www/~~~~/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
/home/ec2-user/.rbenv/versions/2.3.1/bin/ruby -r
./siteconf20180420-22373-1cb7c5f.rb extconf.rb
checking for main() in -lstdc++... yes
creating Makefile

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/var/www/~~~~/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/unf_ext-0.0.7.5/mkmf.log

current directory:
/var/www/~~~~/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
make "DESTDIR=" clean

current directory:
/var/www/~~~~/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5/ext/unf_ext
make "DESTDIR="
compiling unf.cc
cc1plus: warning: command line option '-Wdeclaration-after-statement' is valid
for C/ObjC but not for C++ [enabled by default]
cc1plus: warning: command line option '-Wimplicit-function-declaration' is valid
for C/ObjC but not for C++ [enabled by default]
virtual memory exhausted: Cannot allocate memory
make: *** [unf.o] Error 1

make failed, exit code 2

Gem files will remain installed in
/var/www/~~~~/shared/bundle/ruby/2.3.0/gems/unf_ext-0.0.7.5 for
inspection.
Results logged to
/var/www/~~~~/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/unf_ext-0.0.7.5/gem_make.out

An error occurred while installing unf_ext (0.0.7.5), and Bundler cannot
continue.
Make sure that `gem install unf_ext -v '0.0.7.5'` succeeds before bundling.

In Gemfile:
  fog was resolved to 2.0.0, which depends on
    fog-ovirt was resolved to 1.0.3, which depends on
      rbovirt was resolved to 0.1.5, which depends on
        rest-client was resolved to 2.0.2, which depends on
          http-cookie was resolved to 1.0.3, which depends on
            domain_name was resolved to 0.5.20180417, which depends on
              unf was resolved to 0.1.4, which depends on
                unf_ext
bundle stderr: Nothing written

原因はメモリ不足

EC2のインスタンスを再起動してください

The "libcurl" package isn't availableのエラー

bundler:install
      01 $HOME/.rbenv/bin/rbenv exec bundle install --path /var/www/------/shared/bundle --without development test --deployment --quiet
      01 Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
      01
      01 current directory:
      01 /var/www/chat-space/shared/bundle/ruby/2.3.0/gems/ovirt-engine-sdk-4.2.4/ext/ovirtsdk4c
      01 /home/ec2-user/.rbenv/versions/2.3.1/bin/ruby -r
      01 ./siteconf20180420-3413-cbo4ab.rb extconf.rb
      01 checking for xml2-config... yes
      01 checking for curl-config... no
      01 *** extconf.rb failed ***
      01 Could not create Makefile due to some reason, probably lack of necessary
      01 libraries and/or headers.  Check the mkmf.log file for more details.  You may
      01 need configuration options.
      01
      01 Provided configuration options:
      01     --with-opt-dir
      01     --without-opt-dir
      01     --with-opt-include
      01     --without-opt-include=${opt-dir}/include
      01     --with-opt-lib
      01     --without-opt-lib=${opt-dir}/lib
      01     --with-make-prog
      01     --without-make-prog
      01     --srcdir=.
      01     --curdir
      01     --ruby=/home/ec2-user/.rbenv/versions/2.3.1/bin/$(RUBY_BASE_NAME)
      01     --with-libcurl-config
      01     --without-libcurl-config
      01     --with-pkg-config
      01     --without-pkg-config
      01 extconf.rb:40:in `<main>': The "libcurl" package isn't available. (RuntimeError)
      01
      01 To see why this extension failed to compile, please check the mkmf.log which can
      01 be found here:
      01
      01 /var/www/------/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/ovirt-engine-sdk-4.2.4/mkmf.log
      01
      01 extconf failed, exit code 1
      01
      01 Gem files will remain installed in
      01 /var/www/------/shared/bundle/ruby/2.3.0/gems/ovirt-engine-sdk-4.2.4 for
      01 inspection.
      01 Results logged to
      01 /var/www/------/shared/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/ovirt-engine-sdk-4.2.4/gem_make.out
      01
      01 An error occurred while installing ovirt-engine-sdk (4.2.4), and Bundler cannot
      01 continue.
      01 Make sure that `gem install ovirt-engine-sdk -v '4.2.4'` succeeds before
      01 bundling.
      01
      01 In Gemfile:
      01   fog was resolved to 2.0.0, which depends on
      01     fog-ovirt was resolved to 1.0.3, which depends on
      01       ovirt-engine-sdk
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ec2-user@------: bundle exit status: 5
bundle stdout: Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/var/www/------/shared/bundle/ruby/2.3.0/gems/ovirt-engine-sdk-4.2.4/ext/ovirtsdk4c
/home/ec2-user/.rbenv/versions/2.3.1/bin/ruby -r
./siteconf20180420-3413-cbo4ab.rb extconf.rb
checking for xml2-config... yes
checking for curl-config... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

原因:libcurlが足らない

sudo yum curl-develで解決

Can't connect to local MySQL server through socket ‘/---/---/mysql.sock' (2)

原因 /---/mysql.sockがないこと

具体的な場所/tmp/mysql.sockとか/var/lib/mysql.sockとかになることが多い
とりあえずmysql.sockがあるかどうかを確認しましょう。
ls /---/ で確認。(あるかないか覚えておいてください)

sudo service mysqld restartでmysqlを再起動しましょう。mysqlは再起動するとmysql.sockを再生成します。その後ls /---/ で再生成されているか確認しましょう。
ある場合には、再度デプロイしましょう
ない場合には、別のところに再生成されていると考えられるので、デプロイするアプリケーションのdatabaseのsocketパスの設定が間違えています。(Railsであればdatabase.ymlの中身)

mysql.sockを再生したけど似たようなエラーが起きる場合

mysql.sockの権限が、間違ったものになっている場合があります

chmod 777 /---/---/---/mysql.sock
でmysql.sockの権限を変更

asset:precompileでこける

javascriptがES2015(ES6)の書き方になっていないか確認する。
なっている場合には、sprockets-es6というGemをGemfileに追加する

デプロイ成功したのに変更が反映されない

まず、デプロイ先のフォルダを確認してください
Capistranoを導入すると、appフォルダ以下にcurrent, releases, sharedというフォルダが追加されます。そのcurrentフォルダ(releasesフォルダの最新の日付のものと同じ)がデプロイ先になります。
このcurrentフォルダの中身が最新のものになっているか確認し、なっていなければ再度デプロイし、エラー文を確認してください
最新のものになっている場合には、おそらくunicornの再起動に失敗していますので、ps ax | grep unicornでプロセスidを調べたのち、kill -9 プロセスidでunicornを落としてください。その後再度デプロイしましょう。
その場合には、以下の記事のような状態になっていることが多いので、以下の記事を閲覧して解決しましょう。
Capstarano3でデプロイ時にUnicornが再起動しているようで失敗している時の対処法

エラーが出たときのlogの見方

エラーの原因がnginxかunicornか判別

nginxの役割 : webサーバー URLによってサーバーの中のどこにリクエスト送るかを決める
unicornの役割: アプリケーションサーバー rails sしてるところ

unicorn関連のlogの見方

unicorn.stderrとunicorn.stdoutをみる。logフォルダの中のunicorn.stdoutと.stderrをvimで確認。最後の行が最新のエラーなので間違えないように。%dをすると全行消せるので一回全部消してからもう一度アクセスしてエラーログ吐き出させるのも一つのコツ
capistrano実装後はcurrent>log>unicorn.stdout,unicorn.stderr

nginx関連のlogの見方

var>logの中に入っているので探してvimコマンドで中身をみる。

原因がどっちかわかったら

エラーログの中身からエラーを推測して解決する
だいたい
nginxのエラー→nginx.conf
unicornのエラー→unicorn.rb
を見れば解決する

capistranoによる自動デプロイ中のエラーの場合

この場合でも基本的にはcapistranoを導入したことによる、ディレクトリ構造のズレが原因。
(導入後はappディレクトリ以下にcurrentフォルダ、releasesフォルダ、sharedフォルダが作られる。自動デプロイコマンドを打つごとにcurrentフォルダとreleasesフォルダの中身は変わるので、unicorn.sockなどはsharedフォルダを指定して置いておくことが多い)

capistranoを導入するとフォルダ構成が変化し、unicornによるrails s をする際に、使用されるunicorn.rbが、app>config>unicorn.rbではなく、app>current>config>unicorn.rbになる。
これによるエラーが非常に多いので、新メンターは確認しておくといいかもです

uncorn.rb、capistranoの設定ファイルをみて、

どこにデプロイされるか

どこのフォルダのunicorn.rbが使われてサーバーが立ち上がっているか

unicorn.sockはどこにあるか

nginx.confの設定はそもそも正しいのか

を確認しましょう

55
49
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
55
49