ruby-on-rails - Heroku - bundler : failed to load command: unicorn

标签 ruby-on-rails ruby nginx heroku unicorn

ruby 2.3.1

rails 5.1.2

一直在尝试将我的 Rails 应用程序部署到 heroku 中,方法是使用 设置它的 multi buildpack (heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git) >https://github.com/heroku/heroku-buildpack-ruby.githttps://github.com/heroku/heroku-buildpack-nginx.git。当我 git push heroku master 时,它成功地将 repo 上传到 heroku。当我尝试进入该网站时,出现了应用程序错误页面。所以我去检查了 heroku logs,我看到了以下错误:

Starting process with command `bin/start-nginx bundle exec unicorn -c config/unicorn.rb`
2017-08-10T23:41:44.799815+00:00 app[web.1]: bundler: failed to load command: unicorn (/app/vendor/bundle/ruby/2.3.0/bin/unicorn)
2017-08-10T23:41:44.799855+00:00 app[web.1]: ArgumentError: directory for pid=/app/shared/pids/unicorn.pid not writable
2017-08-10T23:41:44.799856+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/configurator.rb:100:in `block in reload'
2017-08-10T23:41:44.799857+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/configurator.rb:96:in `each'
2017-08-10T23:41:44.799857+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/configurator.rb:96:in `reload'
2017-08-10T23:41:44.799858+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/configurator.rb:77:in `initialize'
2017-08-10T23:41:44.799859+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/http_server.rb:77:in `new'
2017-08-10T23:41:44.799859+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/lib/unicorn/http_server.rb:77:in `initialize'
2017-08-10T23:41:44.799860+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/bin/unicorn:126:in `new'
2017-08-10T23:41:44.799860+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/gems/unicorn-5.3.0/bin/unicorn:126:in `<top (required)>'
2017-08-10T23:41:44.799861+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/bin/unicorn:23:in `load'
2017-08-10T23:41:44.799864+00:00 app[web.1]:   /app/vendor/bundle/ruby/2.3.0/bin/unicorn:23:in `<top (required)>'

另外,我得到一个带有以下消息的常量循环:

2017-08-10T23:41:47.331854+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:41:57.364413+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:01.380323+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:06.411012+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:11.435965+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:15.465182+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:17.477769+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:26.520073+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:34.564750+00:00 app[web.1]: buildpack=nginx at=app-initialization
2017-08-10T23:42:35.568403+00:00 app[web.1]: buildpack=nginx at=app-initialization

我会假设它是因为它试图从 Procfile 开始:

web: bin/start-nginx bundle exec unicorn -c config/unicorn.rb

unicorn_init.sh:

#!/bin/sh

### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the unicorn app server
# Description:       starts unicorn using start-stop-daemon
### END INIT INFO

set -e

USAGE="Usage: $0 <start|stop|restart|upgrade|rotate|force-stop>"

# app settings
APP_NAME="app"
APP_ROOT="$APP_NAME/"
ENV="production"

# environment settings
CMD="cd $APP_ROOT && bundle exec unicorn -c config/unicorn.rb -E $ENV -D"
PID="$APP_ROOT/shared/pids/unicorn.pid"
OLD_PID="$PID.oldbin"

# make sure the app exists
cd $APP_ROOT || exit 1

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PID && kill -$1 `cat $OLD_PID`
}

case $1 in
  start)
    sig 0 && echo >&2 "Already running" && exit 0
    echo "Starting $APP_NAME"
    su - $USER -c "$CMD"
    ;;
  stop)
    echo "Stopping $APP_NAME"
    sig QUIT && exit 0
    echo >&2 "Not running"
    ;;
  force-stop)
    echo "Force stopping $APP_NAME"
    sig TERM && exit 0
    echo >&2 "Not running"
    ;;
  restart|reload|upgrade)
    sig USR2 && echo "reloaded $APP_NAME" && exit 0
    echo >&2 "Couldn't reload, starting '$CMD' instead"
    $CMD
    ;;
  rotate)
    sig USR1 && echo rotated logs OK && exit 0
    echo >&2 "Couldn't rotate logs" && exit 1
    ;;
  *)
    echo >&2 $USAGE
    exit 1
    ;;
esac

unicorn.rb:

# set path to application
app_dir = "/app"
shared_dir = "#{app_dir}/shared"
working_directory app_dir


# Set unicorn options
worker_processes 2
preload_app true
timeout 30

# Set up socket location
listen "#{shared_dir}/sockets/unicorn.mysiteNginx.sock", :backlog => 64

# Logging
stderr_path "#{shared_dir}/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/log/unicorn.stdout.log"

# Set master PID location
pid "#{shared_dir}/pids/unicorn.pid"

nginx.conf:

upstream unicorn {
  server unix:app/shared/sockets/unicorn.mysiteNginx.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name www.mysite-webapp.herokuapp.com;
  root /app/public;

  location ^~ /assets/ {
      gzip_static on;
      expires max;
      add_header Cache-Control public;
  }

  # Feed
  location ~* \.(?:rss|atom)$ {
    expires 1h;
    add_header Cache-Control "public";
  }

  # Media: images, icons, video, audio, HTC
  location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
  }

  # CSS and Javascript
  location ~* \.(?:css|js)$ {
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
  }

  try_files $uri/home.html $uri.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

如有任何帮助,我们将不胜感激!

---更新---

2017-08-11T03:32:29.113451+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=myway-webapp.herokuapp.com request_id=35e12b8e-20b2-4243-bf63-34f1301f5806 fwd="23.240.184.146" dyno= connect= service= status=503 bytes= protocol=http
2017-08-11T03:32:32.706739+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=myway-webapp.herokuapp.com request_id=ec761651-c8da-45de-9b3e-8ae4dae6a7f7 fwd="23.240.184.146" dyno= connect= service= status=503 bytes= protocol=http

---回答---

需要将 $PORT 添加到我的 Procfile 中。

web: bin/start-nginx bundle exec unicorn -p $PORT -c config/unicorn.rb

最佳答案

如何消除参数错误:

最好的猜测是,当您使用 git push heroku 将它推送到 Heroku 时,没有正确克隆空的 pids 文件夹。

所以我要做的是首先检查它是否存在,如果不存在,则使用 mkdir pids 创建它。

如何摆脱无限循环问题

Forgot to add $PORT inside Procfile

关于ruby-on-rails - Heroku - bundler : failed to load command: unicorn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45625184/

相关文章:

ruby - + 在 ruby​​ 中的变量前面有没有做任何事情?

ruby - 我如何从控制台要求 Rails 目录中的库?

nginx - Nginx 中的日志级别 : NGX_LOG_DEBUG_HTTP vs NGX_LOG_DEBUG

ssl - 使用 ELB 和 nginx 的 wss 的 ActionCable 404 错误

ruby-on-rails - Rails 3 管理命名空间问题

ruby-on-rails - Rails image_tag 没有关闭图像标签

ruby-on-rails - 如何在 capybara 场景中添加等待条件?

Ruby local_variables 返回 :symbols?

angular - 带有 Angular 的 Kubernetes NGINX Ingress 返回错误的 MIME 类型

mysql - rails 调试