0

In my vhost, I have:

<Directory /var/www/prod/myapp/myapp/public>
  ...
  RailsEnv production
  ...
</Directory>

and while any code dependent on it being production is correctly running there in the app itself (ex: display the Google Analytics code if Rails.env.production?), when I run rake about from /var/www/prod/myapp/myapp, I get:

Application root          /var/www/prod/myapp/myapp
Environment               development
Database adapter          mysql2

which means that I have to prefix any deployment related rake stuff with RAILS_ENV=production. Granted, it's all in a deployment script at this point so it doesn't matter much, but why isn't Rake aware that it's production? Shouldn't the Passenger setting be enough? And if not, how do I fix it so I won't need to specify the environment manually?

Side-note: I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.

EDIT: Phusion Passenger version 4.0.20

tirdadc
  • 4,603
  • 3
  • 38
  • 45

2 Answers2

1

rake is entirely unaware of passenger's configuration. It doesn't even know that you're using passenger. Since it isn't launched by passenger, it would have to (assuming it knew you were using apache/passenger) parse the apache config files to find this out, which would get pretty complicated, especially in the presence of multiple apps.

You could set this in one of your shell's startup files, however that doesn't sound like a good idea if you have multiple environments on the same machine.

You could stick

ENV['RAILS_ENV']='production'

At the top of one of Rails' startup files - boot.rb seems to do the trick. This would make passenger's setting ineffective though, and obviously you would only want to do this on the production deploy of your script.

Personally (especially on a machine with multiple environments in action) I'd stick to typing RAILS_ENV=production.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
0

but why isn't Rake aware that it's production?

Because rake and all rails related commands internally do this

ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"

which means that if you didnt have RAILS_ENV or RACK_ENV environment, then it will use development environment by default.

Shouldn't the Passenger setting be enough?

NO. Passenger setting is just for making your app up/live in desire or say virtual environment. It doesn't change any system configuration.

And if not, how do I fix it so I won't need to specify the environment manually?

That's very easy. Just set environment variable. If you are using bash (normally people do) you can simple do this

echo "export RAILS_ENV=production" >> ~/.bashrc ; source ~/.bashrc

This will make your server as rails production server, so if you run rails c or rake db:migrate etc or any rails or rake command it will run in production server for all available applications.

I am running the development instance of the app on the same box, with Environment set to development in its corresponding vhost configuration.

This is a problem. If you set the above environment variable, then whenever you run any code like rake db:migrate inside this app ..that will run in production environment. To avoid this either you dont set environment variable or specify environment on every command. There is one hack available . Include this line at the top of config/boot.rb

  ENV["RACK_ENV"] = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development"

Future reading : How can I make a custom environment in rails a default environment?

Community
  • 1
  • 1
Paritosh Piplewar
  • 7,982
  • 5
  • 26
  • 41