【问题标题】:Rails + Unicorn + Nginx + Capistrano 3 + Linode VPS: Not Recognizing Staging EnvironmentRails + Unicorn + Nginx + Capistrano 3 + Linode VPS:不识别暂存环境
【发布时间】:2015-04-12 14:17:40
【问题描述】:

我已经绞尽脑汁几天了,已经用尽了我对这个问题的研究。

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

我注意到的:

  • 我的代码更改(暂存中)在暂存站点上正确显示。
  • 我的暂存数据库已成功创建。

问题:

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

  • 写入生产日志
  • 它正在使用生产数据库

代码:(我已经替换了实际的域名/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 -$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

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/database.yml 是什么样的?
  • @PhilipHallstrom - 我将我的 database.yml 添加到帖子中。

标签: ruby-on-rails nginx unicorn capistrano3


【解决方案1】:

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

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

在Configuring Rails Applications中查找relative_url_root

【讨论】:

  • 嗯,这不是假设我的登台站点是我的生产应用程序的子目录吗?我实际上正在将生产部署到 /var/www/production 并暂存到 /var/www/staging。我正在使用一个子域进行分期,例如 staging.mydomain.com。这仍然适用吗?
  • 你是对的,我把你上面写的解释为一个子目录,而不是一个带有子域的单独目录。我的回答不相关。
  • 明白了,我应该在那里描述得更清楚一点。无论如何感谢您的意见!
  • 你解决了吗?它仍然可能与我所说的有关,因为您是否将环境中的 routes.default_url_options[:host] 设置为子域?如果你登录到你的 $ rails 控制台,app.root_url 会给出什么?
  • 还没有解决这个问题。我在我的生产/暂存文件夹的控制台中运行了它,并且都返回了“example.com”。
【解决方案2】:

终于想通了。我将 Rails.env 写到屏幕上,它在我的登台站点上返回“生产”。这就是它使用我的生产数据库和日志的原因。

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

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

sudo service unicorn_myapp stop
sudo service unicorn_myapp start

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 2012-12-20
    • 2014-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    相关资源
    最近更新 更多