Change RAILS_ENV on shared server without modify passenger configuration - ruby-on-rails

How i change the RAILS_ENV to staging without modify the virtual host file?

You can set it in your .htaccess file, if your host allows it:
RailsEnv staging

Related

Elastic Beanstalk, Rails 5, and Passenger: Rails.env frustration

I have a stack in Elastic Beanstalk running the "Passenger with Ruby 2.5 running on 64bit Amazon Linux/2.8.3" image. Rails is v5.2.1. The desire is to make it a staging environment i.e., have 'Rails.env' return 'staging' and run off of a staging.rb configuration. The problem is, it seems to be running as 'production' no matter what I do.
I set up RACK_ENV and RAILS_ENV as EB configuration variables, both set to 'staging'. I confirmed their existence on the server with 'printenv' as ec2-user, webapp, and root.
I tried changing passenger_app_env in the Passenger config to 'staging'. I've confirmed that Passenger Standalone is using the correct config by looking at the process with 'ps aux | grep passenger'.
I've tried switching to the root server and manually doing '/etc/init.d/passenger stop' and then 'start', and the printout confirms Passenger is launching with its 'environment' set to 'staging':
=============== Phusion Passenger Standalone web server started ===============
PID file: /var/app/support/pids/passenger.pid
Log file: /var/app/support/logs/passenger.log
Environment: staging
Accessible via: http://0.0.0.0/
Serving in the background as a daemon.
Problems? Check https://www.phusionpassenger.com/documentation/Users%20guide%20Standalone.html#troubleshooting
I put this into environment.rb and added an EB config var for STAGING to be 'true'.:
if ENV['STAGING']
%w(RACK_ENV RAILS_ENV).each do |key|
ENV[key] = 'staging'
end
end
However, the test page I made in my Rails app still says 'Rails.env' is 'production', and is not using values from 'staging.rb'. And yet, that same test page says that 'ENV['RACK_ENV']' and 'ENV['RAILS_ENV'] are both set to 'staging'.
At this point, I'm out of ideas on how to force the environment in any other way.
After much hacking, I discovered that Passenger was launching with a passenger_app_env of 'production' (its default) and then switching over to 'staging'. Rails.env would get the the
'production' env and use 'production.rb', then RACK_ENV and RAILS_ENV would be overwritten to 'staging', creating the confusing duality.
The solution was moving the passenger_app_env directive higher up in the Passenger Standalone template that Passenger uses; we had it inside of a server directive inside of an http directive. Moving it up out of the server directive and into the http directive itself solved the issue.

How do I set my RAILS_ENV with Passenger and Apache?

I've been everywhere today looking for the way to properly set my RAILS_ENV for Passenger. I want to use the test environment now that the site is on my server, but I don't want it to be production because I'm having database issues.
I've been directed to this part of the documentation, but it didn't make any sense to me. I had PassengerAppEnv RAILS_ENV = 'test' in my Apache .conf file for the site and -- as expected -- that didn't' work.
I've also set RAILS_ENV = 'test' in config/environment.rb of my Rails site, but that didn't work either.
Thanks!
You're close, but not quite correct. Here is how you set it:
<Directory /path/to/app/public>
Allow from all
Options -Multiviews
# ^ for Apache 2.4+
Require all granted
RailsEnv development # < place desired environment here
</Directory>
Basically Passenger will see the line in the configuration file and then set it for you. It uses the more Apache style syntax of:
Name [space] <value>
So you don't need all the quotes or any Ruby style syntax at all.

passenger/nginx - environment variable passing, or passenger variable passing

We have 2 rails environments with the same code. One is staging, the other production. Both run using the same 'production' rails settings.
But, we'd like to know what environment we are on. I thought I could set an environment variable on the servers and read that out inside of rails, but, while that works from the console it doesn't work when running behind passenger.
in the admin users .profile file I have:
export MC_TEST=true
But the nginx's/passengers running don't seem to be inside that same environment, and that variable is not set.
I thought I could then maybe put something in the nginx.conf to get passed back to the rails environment, but obviously I am not doing that right either!
So - can anyone tell me how to properly do this? Under no circumstances do we want this variable in ruby/rails/app code.
If you are using nginx with passenger (i.e passenger included in nginx installation), the configuration is as follows:
server {
server_name www.foo.com;
root /webapps/foo/public;
passenger_enabled on;
passenger_env_var DATABASE_USERNAME foo_db;
passenger_env_var DATABASE_PASSWORD secret;
}
https://www.phusionpassenger.com/library/config/nginx/reference/#passenger_env_var
Try passenger_set_cgi_param MC_TEST true in your nginx configuration
passenger_set_cgi_param docs

Environments in Rails

I have read that deploying an application on development environment may be one of the worst cases, but i couldn't find any real information about HOW i can change the environments of my applications and make their production databases ready?
I am using Passenger/Nginx for deployment by the way.
Edit : People you get it wrong, maybe i asked wrong, i know how to change environments by nginx, but if i change it from nginx and don't touch to my app, it crashes. There are some things i have to done to my app before i change their environment from development to production, i want info about them.
In your virtual host put following environment variable:-
<VirtualHost *:80>
DocumentRoot /var/apache2/htdocs/tutorial/Web/
ServerName dev.tutorial.local
SetEnv FLOW3_CONTEXT Production
</VirtualHost>
http://www.modrails.com/documentation/Users%20guide%20Nginx.html#RailsEnv
From the Passenger documentation, RAILS_ENV defaults to production.
If not you can specify it in the nginx configuration:
In the http configuration block.
In a server configuration block.
In a location configuration block.
In an if configuration scope.
I am not sure. but try changing environment varaible in config/environment.rb
ENV['RAILS_ENV'] ||= 'production'

prevent page caching with Rails and Passenger in Development Environment

I am running Rails 3 and Passenger 2 and I don't know why but my page is still cached despite having this configuration (in one of my virtual hosts):
<VirtualHost *:80>
ServerName railstut.dev
ServerAlias *.railstut.dev
DocumentRoot "/home/ygamretuta/dev/railstut/public/"
RailsEnv "development"
<Directory "/home/ygamretuta/dev/railstut/">
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
I tried it in Firefox with the browser cache disabled (via web developer plugin) so I don't think there's a problem with the browser cache. I always need to restart the server to see the changes made (even with some minor changes like replacing a text).
What could be the problem?
EDIT
Passenger seems to be setting the environment to production despite there being a RailsEnv config option.
EDIT
Passenger runs on production environment as seen on some of my pages accessing the DB. It looks for the database name configured in the production section in the database.yml file.
http://groups.google.com/group/phusion-passenger/browse_thread/thread/ddb9dbbad0bfe679
If you have existing file cache page cache entries, they will supercede the dynamic request even after restarting. You should clear your tmp or cache directories if you have ever used the file store for caching.

Resources