【问题标题】:Capistrano runs without errors but my page still doesn't showCapistrano 运行没有错误,但我的页面仍然没有显示
【发布时间】:2015-12-16 14:40:11
【问题描述】:

我已经设置了我的 rails 应用程序并着手部署它,但我遇到了一个我不知道如何解决的问题。我运行cap production deploy,它运行没有任何错误。

我的文件:

...
require 'capistrano/deploy'
require 'capistrano/rails'

生产.rb

role :app, %w{deploy@ip}
role :web, %w{deploy@ip}
role :db,  %w{deploy@ip}

部署:

set :application, 'mainapp'
set :repo_url, 'git@github.com:almnes/mainapp.git'
set :deploy_to, '/opt/www/mainapp'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets}

namespace :deploy do

  %w[start stop restart].each do |command|
    desc 'Manage Unicorn'
    task command do
      on roles(:app), in: :sequence, wait: 1 do
        execute "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
      end      
    end
  end

  after :publishing, :restart

end

我的 unicorn.log 在生产中:

I, [2015-12-16T09:15:39.575346 #1128]  INFO -- : master complete
I, [2015-12-16T09:15:50.979431 #1131]  INFO -- : listening on addr=/tmp/unicorn.mainapp.sock fd=10
I, [2015-12-16T09:15:50.983388 #1131]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:15:50.984460 #1131]  INFO -- : master process ready
I, [2015-12-16T09:15:50.989710 #1134]  INFO -- : worker=0 spawned pid=1134
I, [2015-12-16T09:15:50.989947 #1134]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:15:55.351175 #1134]  INFO -- : worker=0 ready
I, [2015-12-16T09:18:54.364835 #1131]  INFO -- : reloading config_file=/opt/www/mainapp/current/config/unicorn.rb
I, [2015-12-16T09:18:54.373599 #1131]  INFO -- : done reloading config_file=/opt/www/mainapp/current/config/unicorn.rb
I, [2015-12-16T09:18:54.527631 #1131]  INFO -- : reaped #<Process::Status: pid 1134 exit 0> worker=0
I, [2015-12-16T09:18:54.527945 #1131]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:18:54.530843 #3986]  INFO -- : worker=0 spawned pid=3986
I, [2015-12-16T09:18:54.531103 #3986]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:18:57.170960 #3986]  INFO -- : worker=0 ready
I, [2015-12-16T09:23:19.949319 #1131]  INFO -- : reaped #<Process::Status: pid 3986 exit 0> worker=0
I, [2015-12-16T09:23:19.949764 #1131]  INFO -- : master complete
I, [2015-12-16T09:23:30.987309 #1132]  INFO -- : listening on addr=/tmp/unicorn.mainapp.sock fd=10
I, [2015-12-16T09:23:30.987889 #1132]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:23:30.988912 #1132]  INFO -- : master process ready
I, [2015-12-16T09:23:31.000132 #1135]  INFO -- : worker=0 spawned pid=1135
I, [2015-12-16T09:23:31.000368 #1135]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:23:35.040186 #1135]  INFO -- : worker=0 ready

生产日志只包含迁移(成功)。

nginx:

upstream unicorn_mainapp {
  server unix:/tmp/unicorn.mainapp.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name: ip_address;
  root /opt/www/mainapp/current/public;

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

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

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

和我的独角兽生产端配置:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage unicorn server
# Description:       Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/opt/www/mainapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deploy
set -u

OLD_PIN="$PID.oldbin"

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

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

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 "
  exit 1
  ;;
esac

最后是我的应用程序 unicorn.rb 文件:

root = "/opt/www/mainapp/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.mainapp.sock"
worker_processes 1
timeout 30

如果它可能有用,我试图在 IP 上运行它,而不是域。我不知道出了什么问题,但我感觉这与 nginx 有关。

【问题讨论】:

  • 您的 nginx 错误日志中有什么内容? (在 Ubuntu 上,它在这里=> /var/log/nginx/error.log )

标签: ruby-on-rails nginx capistrano unicorn


【解决方案1】:

在 nginx.conf 中设置 Unicorn

Unicorn 上有 4 个定义,在您的示例中,unicorn_mainapp 需要匹配,否则套接字将错位,您将收到 nginx 套接字错误。我已将它们添加到下面的示例初始化文件中。

  upstream unicorn_mainapp {
      server unix:/tmp/unicorn.mainapp.sock fail_timeout=0;
    }

server {
  listen 80 default deferred;
  server_name: example.com
  root /opt/www/mainapp/current/public;

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

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

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

套接字文件权限可能是另一个棘手的问题,但如上一致地设置独角兽,让我知道你看到了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多