How to Change the environment of a rails application? - ruby-on-rails

Once I start coding a rails app, I am by default in development mode.
What should I do to change my rails environment to test or production mode ?
can I work in multiple environments simultaneously ?

to run application on production mode type in console
ruby script/server -e production
TO RUN SAME APPLICATION ON DIFFERENT ENVIORMENT you have to use different ports
like
ruby script/server -e production -p 3001
AND
ruby script/server -p 3002

It depends how you are running the application. When you run your tests then they automatically use the test environment. If you are using script/server (e,g, using WEBrick, Mongrel etc.) then you can pass the name of the environment to use on the command line e.g. script/server -e production.
If you are using Phusion Passenger then the environment can be specified using the RailsEnv configuration directive
If you are using the Rails console then specify the environment name directly: script/console production
You can create your own environments too. This Railscast has the details

script/server -e production for production mode
more in script/server --help

Related

How to force Rails 4 app into production mode in Nginx and Unicorn?

I am running a Rails 4 app on a VPS with Ubuntu, NginX and Unicorn.
When I SSL into my server and update the app via git or run rake tasks on the database, my app always switches to development mode and I can't get it into production mode.
Typing RAILS_ENV=production seems to have no effect at all.
When I do
$ rails console
$ Rails.env
I get
--> development
all the time.
What must I do to force NginX into production mode?
Actually, I don't want Nginx to ever run in development mode.
How can this be achieved?
Thanks for any help.
Your application is probably running in production mode by default. What you're doing is engaging a shell, something using a different environment.
Normally on a production server you'd put this into your profile script:
# Add to ~/.bash_profile
export RAILS_ENV=production
That way when you power up rails c you will get the correct environment.
As a note, the only way this shell is engaging in the first place is that you have a development setting in your config/database.yml. That shouldn't be there, as the configuration for your production server should be production-only.
nginx doesn't run in development or production mode - your app does, via your unicorn configuration and/or the RAILS_ENV environment variable when you launch your unicorn instances.
You should be launching your unicorn instances with the RAILS_ENV variable prefixed to the command, eg:
RAILS_ENV=production bundle exec unicorn -c config/unicorn.rb -D
rails console launches a completely different instance which may be in an different environment - it is unrelated to your unicorn instances. If you want to launch a production console instance, then either invoke RAILS_ENV=production rails console or rails console production. Note that this has no bearing on the environment that your application runs in.

Cannot set staging environment for Rails application

I'm deploying Rails application to server. I can easily accomplish everything if I need only production. But I need staging as well.
I launch my unicorn server with the following command:
bundle exec /home/deployer/apps/myapp/shared/bundle/ruby/2.0.0/bin/unicorn_rails \
-D -c /home/deployer/apps/myapp/shared/config/unicorn.config.rb -E staging
But regardless of my command, the server launches application with production environment.
Is there another place in my application where I should specify environment to be staging?
Thanks!
Set the environment variable RAILS_ENV=staging before you run the command. You can do it on the same line even (in Bash):
$ RAILS_ENV=staging bundle exec unicorn...
You would have to have the environment set up in multiple files such as config/environments/ and config/database.yml.

Rails application without port number

I am using following command for start the production server.
nohup ruby script/server webrick -e production &
My Rails applications start with port number in URL, like this one:
http://myapp.com:3000/
How to remove port no from the app URL? I think I need to install something like Passenger, If so anyone suggest me a good tutorial.
PS: My rails App hosted in http://dreamhost.com and I am using Rails 2.3r
In a production environment you probably want to use passenger instead of doing things by yourself.
"No port" for HTTP means port 80, so specify that with the -p 80 or --port=80:
nohup ruby script/server webrick -e production -p 80 &
You don't need Passenger, but using it can make things easier to manage for smaller apps and requires less babysitting.

rails3 app ENV not being recognized as production

I set up my rails app on my linode VPS, phusion passenger is installed and working, so is mysql (I know this cause my friend currently is running 2 production apps on it with the same set up). The VPS is running Ubuntu 10.10 and I'm using apache2 with passenger.
I SFTP'd the app to the server, bundle updated, set up my virtual host and specified RailsEnv to be production and paths are all accurate.
I then restarted the server (as root) with
apachectl -k restart
tried to rake db:migrate and it didn't do anything, so I figured it was because the environment didn't get changed, which is exactly what happend. If I go into the rails console and type Rails.env it gives me development.
I have no idea why, I did everything that should set it to production? Anyone know what I may have missed? Is there somewhere in the app where I'm supposed to change something to say production environment? I thought that only had to be done in rails 2.x
Thanks in advance for any and all help.
The RailsEnv setting is only for Passenger's use. It doesn't affect the commands you type in the shell.
Use
RAILS_ENV=production rake db:migrate
and
RAILS_ENV=production rails console
Or set the RAILS_ENV environment variable in your login shell to production so that you don't have to append RAILS_ENV=production to the commands you issue:
export RAILS_ENV=production
(exact command may vary depending on which shell you use; the above works in bash)
You were on the right track; all you need to do to actually run the app in production mode is set the RailsEnv as you did, assuming you are running the app using Passenger. However, to run the database migrations you need to tell Rails what environment to run within.
The rails console command defaults to the 'development' environment by default. The same goes for running database migrations.
To run the migrations on your production environment you need to run the command as such:
RAILS_ENV=production rake db:migrate
And to run the console in production mode, you need to run the console using the command:
rails console production
If you want this variable to be set automatically, put RAILS_ENV=productionat the end of your ~/.bashrc file. (This only works with a bash terminal)
Then open a new terminal, or restart your ssh connection.

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