我正在尝试使用Capistrano 3在EC2实例上部署我的第一个Rails 4应用程序。我已经关注了一些教程,该教程介绍了如何在Ubuntu上使用Nginx和Passenger设置Web服务器。 我也有MySQL数据库的RDS实例。 但是,当我尝试运行cap deploy
它在assets:precompile
阶段给了我一个错误:
INFO [d5d05621] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as deploy@[EC2 PUBLIC IP]
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy@[EC2 PUBLIC IP]: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Mysql2::Error: Access denied for user 'root'@'[EC2 PRIVATE IP]' (using password: NO)
据我了解,它正在尝试使用开发环境中的凭据连接到localhost上的数据库。
这是我的database.yml
文件的样子:
development:
adapter: mysql2
encoding: utf8
pool: 5
socket: /tmp/mysql.sock
username: root
password:
database: Store_development
production:
adapter: mysql2
encoding: utf8
database: Store_production
username: <%= ENV['STORE_DATABASE_USERNAME'] %>
password: <%= ENV['STORE_DATABASE_PASSWORD'] %>
host: [RDS INSTANCE ENDPOINT]
port: 3306
pool: 5
timeout: 5000
我四处寻找可以指定环境的所有地方,最后发现了类似的东西。 在我的deploy/production.rb
我有:
set :stage, :production
set :rails_env, :production
我在服务器上的/etc/nginx/nginx.conf
的http
部分中也有此文件:
passenger_app_env production;
而这在/etc/nginx/sites-available
中的虚拟主机配置中
passenger_enabled on;
rails_env production;
最后,我在部署用户的.bashrc
中拥有了这个:
export RAILS_ENV=production
我是否缺少使部署在生产环境下工作并使用来自database.yml
文件的凭据连接到RDS实例的东西? 或者是别的什么?
确保在服务器上设置了STORE_DATABASE_USERNAME和STORE_DATABASE_PASSWORD环境变量,并且这些变量对您的Rails应用程序可见。 当您通过SSH进入EC2实例并运行以下命令时,您是否获得了预期的值?
echo $STORE_DATABASE_USERNAME
echo $STORE_DATABASE_PASSWORD
如果没有,请将这些导出添加到部署用户的.bashrc
中:
export STORE_DATABASE_USERNAME=< your username >
export STORE_DATABASE_PASSWORD=< your password >