Rails + Unicorn + Nginx + Capistrano 3 + Linode VPS:无法识别暂存环境

Rails + Unicorn + Nginx + Capistrano 3 + Linode VPS: Not Recognizing Staging Environment

这几天我绞尽脑汁,对这个问题研究得精疲力竭。

一点背景知识:我有一个 rails 应用程序在生产中运行良好。我在不同目录下的同一台服务器上添加了一个暂存环境。我能够到达暂存站点。

我注意到的:

问题:

我的暂存站点似乎认为它是生产站点。我觉得我没有在某处正确设置暂存环境。发生了一些奇怪的事情:

代码:(我已经替换了实际域 names/IP)

config/deploy.rb

# config valid only for current version of Capistrano
lock '3.3.5'

set :stages, %w(production staging)
set :default_stage, 'staging'

set :repo_url, 'git@github.com:test/test.git'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets public/system/members}

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

config/deploy/staging.rb

set :rails_env, 'staging'
set :application, 'test_staging'
set :deploy_to, '/var/www/staging.test.co'
set :branch, 'staging'

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

config/unicorn.rb

if ENV["RAILS_ENV"] == "production"
    root = "/var/www/test.co/current"
else
    root = "/var/www/staging.test.co/current"
end
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

if ENV["RAILS_ENV"] == "production"
    listen "/tmp/unicorn.test.sock"
else
    listen "/tmp/unicorn.test_staging.sock"
end
worker_processes 1
timeout 30

[在服务器上]/etc/init.d/unicorn_test_staging

#!/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=/var/www/staging.test.co/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c      $APP_ROOT/config/unicorn.rb -E staging"
AS_USER=deploy
set -u

OLD_PIN="$PID.oldbin"

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

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

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

case "" 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: [=15=] "
  exit 1
  ;;
esac

config/database.yml

development:
 adapter: postgresql
 encoding: unicode
 database: test_dev
 host: localhost
 pool: 5
 username: test
 password: password

staging:
 adapter: postgresql
 encoding: unicode
 database: test_staging

production:
 adapter: postgresql
 encoding: unicode
 database: test_production

如果您需要任何其他代码来帮助我找到这里的问题,请告诉我。我感谢任何人对此的帮助。

谢谢!

很可能您的登台服务器正在调用您的生产服务器的路由,因为它们在同一台主机上。

您需要确保设置您的子文件夹:config.relative_url_root 或 RAILS_RELATIVE_URL_ROOT 环境变量。

Configuring Rails Applications

中查找 relative_url_root

终于明白了。我在屏幕上写了 Rails.env,它在我的暂存站点上返回了 "production"。这就是它使用我的生产数据库和日志的原因。

我最初在 /etc/init.d/unicorn_myapp 中忘记将我的独角兽初始化脚本更改为 -E staging 当我第一次创建我的登台站点时。我几天前将其设置为暂存,但并没有解决问题。

所以今天出于绝望,我进入了我的服务器并尝试了一个完整的独角兽停止和启动

sudo service unicorn_myapp stop
sudo service unicorn_myapp start

问题就解决了!我的 Rails.env 开始返回 "staging",现在一切正常。

TLDR:如果您更改独角兽初始化脚本,请始终确保停止并启动独角兽应用程序。简单的重启对我不起作用。