首页 > 解决方案 > Rails 应用程序在应该处于暂存模式时进入生产模式

问题描述

Ubuntu 18.04    
Rails 5.2.3
Apache > 2.4
Phusion Passenger
Webbpacker

我的应用程序在我的开发机器上运行良好,但我正在尝试将 abd 部署到登台服务器。

应用程序部署到

/home/myappstaging

当我部署它时,我使用用户名 myappstaging 登录,如果我这样做,则从命令 shell 登录:

printenv

我得到:

RAILS_MASTER_KEY=xxxxxxxxxxxxxxxxxxxxx
USER=myappstaging
RAILS_ENV=staging
RAILS_USER=myappstaging
RAILS_DB_NAME=myappstaging
RAILS_DB_PWD=xxxxxxxxxxxx   
RAILS_DB_USER=myappstaging

在我的 config/database.yml 文件中,我有:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: localhost
  database: <%= ENV["RAILS_DB_NAME"]%>
  username: <%= ENV["RAILS_DB_USER"]%>
  password: <%= ENV["RAILS_DB_PWD"] %>

staging:
  <<: *default

当我将代码上传到登台服务器时,我会:

RAILS_ENV=staging bundle install
RAILS_ENV=staging bundle exec rails webpacker:install
RAILS_ENV=staging rake db:migrate
RAILS_ENV=staging rake db:seed
RAILS_ENV=staging bundle exec rake assets:precompile

但是,当我尝试从本地浏览器访问应用程序时,出现“出现问题”屏幕。

查看日志目录,我看到两个日志文件:

staging.log
production.log

在 staging.log 中,我看到了数据库迁移和数据库播种的记录。

在 production.log 中,有一个错误导致“出现问题”屏幕:

Started GET "/users/sign_in" for xx.xxx.xxx.xxx at 2019-09-11 14:02:11 +0000
Processing by User::SessionsController#new as HTML
Completed 500 Internal Server Error in 1ms

Mysql2::Error::ConnectionError (Access denied for user 'myappstaging'@'localhost' (using password: NO)):

显然,它认为它在生产模式下运行,这就是它无法连接到数据库的原因。知道为什么会这样吗?

PS:如果我从命令行启动 rails 服务器,我不会收到任何错误消息。

阿帕奇配置文件:

<VirtualHost *:80>
   ServerName myserver.com

   DocumentRoot /home/myappstaging/public

   PassengerRuby /path-to-ruby

   <Directory /home/myappstaging/public>
     Allow from all
     Options -MultiViews
     Require all granted
   </Directory>
</VirtualHost>

标签: ruby-on-railsactiverecord

解决方案


您需要告诉乘客使用哪个轨道环境。它默认为生产。

<VirtualHost *:80>
   ServerName myserver.com

   DocumentRoot /home/myappstaging/public

   PassengerRuby /path-to-ruby

   <Directory /home/myappstaging/public>
     Allow from all
     Options -MultiViews
     Require all granted
     RailsEnv staging 
   </Directory>
</VirtualHost>

推荐阅读