Rails app not loading new environment variable added to /etc/environment - ruby-on-rails

I'm trying to add an environment variable to use in my Rails App. Currently, our app uses several environment variables defined in /etc/environment. They work fine. So I've added a new variable to /etc/environment (and rebooted for the hell of it). But when I try to access the new variable, its undefined. Printing out ENV.inspect shows all the original variables, but not my newly added one. Why oh why?
Running Apache 2 / Passenger 4 / Rails 4. Recently upgraded from Passenger 2 / Rails 3 if its significant.
Edit
Turns out the ones I thought 'work fine' don't work either (they're redefined in my Rails app). So none of the variables in /etc/environment are loaded into the app. Not sure why I thought that system worked, but it doesn't.

Might not be good news to you, but you have to reboot the instance to see it happen (or check the other tricks at that question).
You can also use SetEnv at your Apache config and just restarting apache itself will make your app see the new values.

ENV
Upgrading from Rails 3 - Rails 4 should not be a major concern for the ENV variables, unless the way Rails processes them will have changed.
We use the following:
This works for us with Rails 4, Ubuntu 12.04, Passenger, Apache 2
--
The problem you may have is that your Rails application has not updated since you added the new variables.
When you run a Rails app, it essentially loads an "instance" of the application, and then keeps loading it somehow. I'm not totally sure how it works, but I do know the likes of Passenger essentially "cache" a rails app whilst in production
This means if you're adding new Environment variables, perhaps the likes of Passenger has made it so the Rails application has not been updated? If this is the case you may wish to use the touch tmp/restart.txt command:

Related

Rails console won't load environment variables in secrets.yml

I am trying to debug a problem with secrets.yml loading environment variables, by setting some environment variables in development and running rails c to inspect things. When I load Rails.applications.secrets this way, it is not picking up any of the environment variables I have set (namely, SECRET_KEY_BASE)
If I run the application with the same environment variables set, it picks them up fine (I'm using RubyMine to run the application, but running rails c from the terminal)
In my rails console, I can see the environment variable I've set using ENV['SECRET_KEY_BASE'], but it doesn't show up in Rails.application.secrets. Why?
TL;DR: spring stop
It turns out, as has happened so many times when things aren't making any sense, Spring is the culprit! I solved this problem (thanks to a related discussion) by running spring stop and then trying again, after which it worked perfectly!
Apparently Spring was caching the environment, or certain pieces of the Rails application, and neglecting to reload them when the environment variables changed.

Rails app deployed on Centos w/ standalone Passenger and Capistrano can't access ENV variables

I set up a Centos 7 VM using this tutorial (standalone passenger) and RVM. I am deploying the rails app via Capistrano.
https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/standalone/oss/install_language_runtime.html
Everything seems to work, except no matter where I set environment variables, the ENV["myvar"] can't be read in Rails.
I've tried export myvar=test SSHed as the "deployers" as well as root. I've also tried adding it to bashrc. If I login as deployer and do the following:
#symlink to current capistrano deploy
cd ~/rails/railsapp/current/
rails r "puts ENV['myvar']"
It gives me the correct ENV output. However, if I try to output ENV['myvar'] from my actual deployed via capistrano rails app, I get nothing.
Where am I supposed to set these ENV vars? I know the ENV vars in rails are done correctly, because the app deployed to heroku, as well as on my dev machine, correctly output ENV['myvar'].
Generally, your setup should work. Since version 4, a standalone Passenger should inherit all variables defined in the shell startup scripts. There is a nice documentation about environment variables in various scenarios related to using Passenger.
I would check or two things:
That your .bashrc is loaded from .profile. If it weren't, then your variables would be loaded only in an interactive shell but not in passenger, which would explain the behavior your describe when you tried to log in as the deployers. Let me quote from the doc:
Make sure your ~/.bashrc is actually included by your ~/.profile, which might not be the case if you created the user with useradd instead of adduser for example.
Also, take a look at this section of the docs and check that you obey the conditions upon which Passenger actually passes the environment vars to the application.

Why are my Rails initializers caching ENV variables in the console (Foreman, Dev env)

What started as a minor annoyance has now turned into a headache. I am building a Rails 4 app and am using Foreman for my dev setup with a Procfile and .env file for configuration. When I set an ENV variable in the .env file, it is correctly picked up by my app. In this case I am setting some ENV options for Paperclip in an initializer.
The problem surfaces when I go to change the value of the ENV variables. In the console, if I type ENV["MY_VAR"], it shows the new value. However, the value that was used in my initializer, which presumably was run when I started the console, shows the old value! Nowhere in my project is the old value listed anywhere. This leads me to believe that the environment is being cached somehow or that the env variables are being exported to my shell. I'm running out of places to look so any help would be greatly appreciated! I am developing on a Mac (10.9.4) with Ruby 1.9.3-p374 and Rails 4.1.0.
Example:
ROOT/.env
S3_BUCKET=mybucket
config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:s3_credentials] = {bucket: ENV["S3_BUCKET"]}
If I change the value of S3_BUCKET to "newbucket" and run "foreman run rails c" or "rails c" to enter the console, this is what happens:
ENV["S3_BUCKET"] # => "newbucket"
Paperclip::Attachment.default_options[:s3_credentials] # => {bucket: 'mybucket'}
I should mention that this behavior also occurs in my classes that I've put in /lib. I imagine this is all due to something silly that I've overlooked. Any ideas?
If you're using Rails 4 out of the box, it comes with a gem called Spring that's intended to make your life easier by preloading an instance of your application in the background and reloading it as your code and configuration files change.
Spring, however, only monitors Rails' default configuration files, so you'll need to configure Spring to monitor additional any other files that you wish to trigger a reload.
Spring reads ~/.spring.rb and config/spring.rb for custom settings. You can add add the following line to the file of your choosing to watch your .env file for changes:
Spring.watch '.env'
See Spring's configuration documentation in the README for more info.

Deployed Rails app always coming up in development environment

Using Rails 3.0.1, Apache 2.2.9, and Passenger 3.0.0 on Debian.
For some mysterious reason, the Rails app is coming up in the development environment. I've double-checked the Apache configuration, the VirtualHost files, and so forth, and there are zero instances of "RailsEnv" or "RAILS_ENV" either in /etc/apache2 or in the app directory itself.
Worse, even if I do put a "RailsEnv production" line into the Directory block in the VirtualHost file, it makes absolutely no difference, and the app still comes up in the development environment.
Nothing interesting is logged if I set PassengerLogLevel to 1.
Any help either with what the problem is, or at least a course of action to identify the problem, would be greatly appreciated.
That's because with config.ru your app is detected as a Rack app, not a Rails app, and thus you need to set RackEnv instead of RailsEnv. A "Rails" app refers to a "Rails 1 or 2" app. Rails 3 apps are considered to be Rack apps.
After thrashing around for a while and trying random things off the 'net, I've found that deleting the config.ru file Rails generated solves the problem without otherwise causing the app to cease functioning. Why this was causing the problem remains unknown to me, but anybody coming across this having the same issue might try that solution.
Same symptom here, but none of the suggested solutions work for me. I first noticed this when I set my <body> tag to be <body class="<%= Rails.env %>"> and defining a background-color for the development class that is distinctly different from the background-color in the production class. All I ever see (production & development) is the development color. And of course viewing the source of the web page in the browser shows that the class being generated is development. I renamed config.ru to ~config.ru and restarted Apache2 and that didn't have an impact. I added RackEnv production to my *:80 virtual host file where the other Passenger attributes are set. That didn't have an impact. So I reset ~config.ru to be config.ru, restarted Apache2 and still don't see a change. I appreciate the explanation given by #Hongli of a Rack startup versus a Rails startup. I just wish my server would listen to him! I even did a recursive listing grepping for .htaccess -- none found. At least I think I know now where to keep poking around.
Apache 2.2.15, Rails 3.0.3, Passenger 3.0.2, Mac OS X Server 10.6.6

Setting up Rails for the first time - is this normal?

I am trying to setup a Rails environment via CPanel. I've tried on several distinct hosting environments (on all of which I used CPanel to create the project) and I always get the following:
Is this something I've done wrong - I have to first create models/controllers etc - or something I need to bark up my hosting provider's tree about?
Have you tried running your app locally? If I remember correctly, for security reasons, the "about your application's environment" won't load unless it's from localhost.
If you are running your app in production mode (so it is recognized as it doesn't run locally), rails won't show this information, instead it will show this message.
I had this issue and wondering what's wrong when I first setup a new application in a server with passenger. The default environment of passenger is the production so it doesn't show this info. You have to set it up to development mode if you want to see them, to do that go to your virtual host file and add this:
RailsEnv development
When you start creating your application, delete the index.html from the public.
Maximum supported versions at this time are:
Ruby 1.8.7
RubyGems 1.8.25
Rails 2.3.18
Anything newer than that is a near guaranteed breakage and cPanel & WHM will be incapable of utilizing it in any way, shape, or form.
http://tickets.cpanel.net can assist you with removing your existing Ruby on Rails installations and reverting them back to cPanel supported and sanctioned versions (Ruby 1.8, RubyGems 1.8, and Rails 2). That is the only thing they can do for you at this time.
If you want to use any versions newer than this, then you will be unable to use the cPanel & WHM interfaces or management tools for it -- they simply will not work. You will then have to manually manage your RoR install by yourself through command line exclusively. None of it would fall under the scope of cPanel support.

Resources