Ruby on Rails + Passenger, ENV not changing - ruby-on-rails

First of all, our project structure is a bit different than a regular ruby on rails project therefore we don't have anything like environment.rb.
I am running into an issue changing the ENV variables. The server keeps starting under development mode.
Environment (value of RAILS_ENV, RACK_ENV, WSGI_ENV, NODE_ENV and PASSENGER_APP_ENV)
development
I tried changing those variables like this export RAILS_ENV=production and restarting the server using service httpd restart but for some reason none of these variables will change.
My question is, where can I possibly find/override those variables in a file?

Try setting the environment variables in the /etc/environment file on your server. I had a similar issue recently and that seemed to resolve it for me. It would look something like FOO=bar

Found it. It was under my httpd.conf where I just changed variable RailsEnv to production

In Rails 5, these lines are moved from .htaccess to apache configuration file.
RackBaseURI /
PassengerAppRoot /path/to/public_html
RailsEnv production
I am running on apache 2.4.18, Phusion Passenger 5.2.1.

Related

Rake not using the same environment value as phusion passenger setting

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
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.
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?

How do I set RAILS_ENV to 'Production' on a DreamHost shared server?

I have a shared server running passenger to server my Rails app. For some reason my RAILS_ENV variable seems to be stuck as 'Development'. How do I set it to 'Production'? Thanks
In a shared hosting environment, you will want to do this in a .htaccess file, placed inside public/. Mine looks like this:
PassengerEnabled on
PassengerAppRoot /home/myuser/myapp/current
RailsEnv production

Rails won't serve static files in production

I recently updated my app from 2.3.8 to 3.0.rc, but after a while in development environment I tried the production environment. But now it won't serve static files.
I use standard development and production environment settings, and have no unique gems for either environments. Everything works like a charm in development, but it won't serve static files in production.
Using ruby-1.8.7-p299 and every gem is up to date. Using WEBrick on my server, running rails s -e production -p 3001
Anyone with some solutions or tips to this ?
After some extra digging I found a setting in the production.rb file, config.serve_static_assets which was set to false, and since I'm not running apache or nginx the static files where not served. blush
For rails 5 you should set environment variable RAILS_SERVE_STATIC_FILES

Passenger 2.2.9 Ruby EE Ubuntu.. Can't get it to use 'default' production environment

Using Apache2 (2.2.12) / Passenger (2.2.9)..
Have 2 apps running.. One runs fine in production mode the other insists on being in development mode.
Have booted mongrel into production mode and the app works fine.. (script/server -e production)
Tried the following with no result...
Forced the RailsEnv in the apache virtual host.
Forced the RailsEnv in the passenger.conf in /etc/apache2/mods-available
=> Although production is the default mode I cannot get it to use production
I suspect the issue is somewhere between apache / passenger (and not in the rails app)
Any ideas? or suggestions
This is probably because you have a config.ru file in your RAILS_ROOT which means that Passenger is running your Rails application as a Rack application, which means that you need to set RackEnv and not RailsEnv in your apache configuration.
Thanks Jason.. set me on the right track.. Actually had to remove the config.ru to get it working.. Think the issue had to do with the passenger version, since I have it working with the config.ru on other servers (only diff is the passenger version)

Change a Rails application to production

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?
This would now be
rails server -e production
Or, more compact
rails s -e production
It works for rails 3+ projects.
How to setup and run a Rails 4 app in Production mode (step-by-step) using Apache and Phusion Passenger:
Normally you would be able to enter your Rails project, rails s, and get a development version of your app at http://something.com:3000. Production mode is a little trickier to configure.
I've been messing around with this for a while, so I figured I'd write this up for the newbies (such as myself). There are a few little tweaks which are spread throughout the internet and figured this might be easier.
Refer to this guide for core setup of the server (CentOS 6, but it should apply to nearly all Linux flavors): https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6
Make absolute certain that after Passenger is set up you've edited the /etc/httpd/conf/httpd.conf file to reflect your directory structure. You want to point DocumentRoot to your Rails project /public folder Anywhere in the httpd.conf file that has this sort of dir: /var/www/html/your_application/public needs to be updated or everything will get very frustrating. I cannot stress this enough.
Reboot the server (or Apache at the very least - service httpd restart )
Enter your Rails project folder /var/www/html/your_application and start the migration with rake db:migrate. Make certain that a database table exists, even if you plan on adding tables later (this is also part of step 1).
RAILS_ENV=production rake secret - this will create a secret_key that you can add to config/secrets.yml . You can copy/paste this into config/secrets.yml for the sake of getting things running, although I'd recommend you don't do this. Personally, I do this step to make sure everything else is working, then change it back and source it later.
RAILS_ENV=production rake db:migrate
RAILS_ENV=production rake assets:precompile if you are serving static assets. This will push js, css, image files into the /public folder.
RAILS_ENV=production rails s
At this point your app should be available at http://something.com/whatever instead of :3000. If not, passenger-memory-stats and see if there an entry like 908 469.7 MB 90.9 MB Passenger RackApp: /var/www/html/projectname
I've probably missed something heinous, but this has worked for me in the past.
If you're running on Passenger, then the default is to run in production, in your apache conf:
<VirtualHost *:80>
ServerName application_name.rails.local
DocumentRoot "/Users/rails/application_name/public"
RailsEnv production ## This is the default
</VirtualHost>
If you're just running a local server with mongrel or webrick, you can do:
./script/server -e production
or in bash:
RAILS_ENV=production ./script/server
actually overriding the RAILS_ENV constant in the enviornment.rb should probably be your last resort, as it's probably not going to stay set (see another answer I gave on that)
If mipadi's suggestion doesn't work, add this to config/environment.rb
# force Rails into production mode when
# you don't control web/app server and can't set it the proper way
ENV['RAILS_ENV'] ||= 'production'
Change the environment variable RAILS_ENV to production.
$> export RAILS_ENV=production
You can also pass the environment to script/server:
$ script/server -e production
rails s -e production
This will run the server with RAILS_ENV = 'production'.
Apart from this you have to set the assets path in production.rb
config.serve_static_assets = true
Without this your assets will not be loaded.
RAILS_ENV=production rails s
OR
rails s -e production
By default environment is developement.
As others have posted: rails server -e production
Or, my personal fave: RAILS_ENV=production rails s
In Rails 3
Adding Rails.env = ActiveSupport::StringInquirer.new('production') into the application.rb and rails s will work same as rails server -e production
module BlacklistAdmin
class Application < Rails::Application
config.encoding = "utf-8"
Rails.env = ActiveSupport::StringInquirer.new('production')
config.filter_parameters += [:password]
end
end
It is not a good way to run rails server in production environment by "rails server -e production", because then rails runs as a single-threaded application, and can only respond to one HTTP request at a time.
The best article about production environment for rails is Production Environments - Rails 3
for default server : rails s -e production
for costum server port : rails s -p [port] -e production, eg. rails s -p 3002 -e production
By default server runs on development environment: $ rails s
If you're running on production environment: $ rails s -e production or $ RAILS_ENV=production rails s
Please make sure you have done below in your environment.rb file.
ENV['RAILS_ENV'] ||= 'production'
If you application runs in shared hosting environment or phushion passenger, you might need to need make changes in .httaccess (inside public folder) and set mode as production.

Resources