Rails& Capistrano 3 - 尝试用户生产数据库的登台服务器

时间:2015-06-07 21:05:46

标签: ruby-on-rails capistrano staging

我使用NGINX& amp;为我的登台环境设置了服务器乘客。我还设置了一个staging.rb文件,该文件与环境下的production.rb文件重复。在我的database.yml文件中,我已经进入了一个暂存配置:

  staging:
  adapter: mysql2
  database: myapp_staging
  username: root
  password: xxxxxxxxxxxxx
  port: 3306
  pool: 15
  timeout: 5000

环境/ staging.rb:

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


# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server definition into the
# server list. The second argument is a, or duck-types, Hash and is
# used to set extended properties on the server.

server '111.111.111.111', user: 'deploy', roles: %w{web app db}, port: 0001

我使用cap staging deploy进行部署,但应用无法启动,并在日志中显示:Unknown database 'myapp_production'

如何强制它使用登台数据库?

修改

Deploy.rb:

set :application, 'dispatch'
set :repo_url, 'myapp'

set :deploy_to, '/home/deploy/myapp'

set :scm, :git
set :branch, 'master'
set :keep_releases, 5
set :format, :pretty
set :log_level, :debug
set :pty, true
set :passenger_restart_with_sudo, true

set :stages, ["staging", "production"]
set :default_stage, "staging"

set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('rvmsudo')

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  after :publishing, 'deploy:restart'
  after :finishing, 'deploy:cleanup'
end

4 个答案:

答案 0 :(得分:5)

我设置了第3章,我有一个config/deploy/production.rb文件,其中设置了环境。你是否同样​​进行舞台演出和演出生产

set :stage, :staging
server 'example.com', user: 'aaron', roles: %w{web app db}
set :rails_env, :staging

需要确保它设置了适当的环境,因此db任务将知道要连接的数据库。

答案 1 :(得分:2)

您应该在staging.rb中设置环境

set :stage, :staging
set :rails_env, :staging

然后你还应该在apache虚拟主机文件中使用“PassengerAppEnv”变量设置环境

  <VirtualHost *:80>                                                           
    ServerName myserver.com                                                 
    # Tell Apache and Passenger where your app's 'public' directory is       
    DocumentRoot /var/www/your_app/current/public                         

    PassengerAppEnv staging
    <Directory /var/www/your_app/current/public>                          
      Allow from all                                                         
      Options -MultiViews                                                    
      Require all granted                                                    
    </Directory>                                                             
</VirtualHost> 

这为我解决了同样的问题。我希望它也能帮助每个人面对它。

答案 2 :(得分:1)

好的,这是一个非常愚蠢的错过,但我没有将我的NGINX配置文件设置为“暂存”

     server_name localhost;
     passenger_enabled on;
     rails_env    staging;
     root         /home/myapp;

答案 3 :(得分:0)

我遇到了类似的问题。就我而言,问题与登台服务器中.bashrc文件中的ENV变量有关。在此文件中设置了变量:

RAILS_ENV=production

我更改为:

RAILS_ENV=staging

需要重新加载变量后:

source ~/.bashrc