193

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
Adler Medrado
  • 2,088
  • 2
  • 15
  • 14
  • 2
    It seems that the second answer has lots more votes, would you be willing to give this a quick review and accept the second answer unless you have any issue with it. Will just help future visitors. Thx :) – Evolve Aug 02 '17 at 23:06

15 Answers15

352

This would now be

rails server -e production

Or, more compact

rails s -e production

It works for rails 3+ projects.

kairos
  • 103
  • 8
BandsOnABudget
  • 3,936
  • 2
  • 15
  • 19
75

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.

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

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

  3. Reboot the server (or Apache at the very least - service httpd restart )

  4. 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).

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

  6. RAILS_ENV=production rake db:migrate

  7. RAILS_ENV=production rake assets:precompile if you are serving static assets. This will push js, css, image files into the /public folder.

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

Wayne Conrad
  • 90,071
  • 22
  • 147
  • 183
etusm
  • 3,774
  • 1
  • 26
  • 26
  • 2
    I feel like this answer should be migrated to stackoverflow documentation. – Whitecat May 08 '17 at 18:23
  • I would add that if you don't want to use a web content deliver like apache, you could add a RAILS_SERVE_STATIC_FILES=1 next to the RAILS_ENV=production, that would mean that rails would serve every file so won't recommend this for a real-production state... – 3d0 Nov 10 '17 at 17:39
62

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)

Community
  • 1
  • 1
Dan McNevin
  • 21,970
  • 5
  • 33
  • 28
54

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'
Community
  • 1
  • 1
Pete
  • 9,450
  • 7
  • 50
  • 57
  • What about cloud services like Heroku? May `ENV['RAILS_ENV'] ||= 'production'` be applied on them too? – Green Apr 29 '13 at 01:51
  • I edited this file long back and forgot.. thanks for reminding.. saved lot of time – Agnes Feb 22 '17 at 05:21
19
$> export RAILS_ENV=production
Evolve
  • 8,466
  • 11
  • 46
  • 58
19

Change the environment variable RAILS_ENV to production.

mipadi
  • 359,228
  • 81
  • 502
  • 469
18

You can also pass the environment to script/server:

$ script/server -e production
foz
  • 5,050
  • 2
  • 21
  • 14
12
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.

tomd
  • 4,094
  • 2
  • 30
  • 37
Prasanna
  • 8,390
  • 2
  • 25
  • 36
  • Sorry ... voted down when meant to upvote. Made slight edit in order to correct error and upvote. – tomd Sep 29 '15 at 23:37
8
RAILS_ENV=production rails s

OR

rails s -e production

By default environment is developement.

puneet18
  • 4,537
  • 2
  • 18
  • 25
5

As others have posted: rails server -e production

Or, my personal fave: RAILS_ENV=production rails s

vishes_shell
  • 17,549
  • 6
  • 59
  • 68
alex1sz
  • 330
  • 4
  • 10
2

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

halfer
  • 18,701
  • 13
  • 79
  • 158
  • 3
    Link rot... available on wayback machine though: [link](http://web.archive.org/web/20121022083645/http://ofps.oreilly.com/titles/9780596521424/production_id35801033.html) – rosuav Mar 20 '13 at 02:53
2

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
RSK
  • 16,650
  • 13
  • 52
  • 74
1

for default server : rails s -e production

for costum server port : rails s -p [port] -e production, eg. rails s -p 3002 -e production

1

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

paper1111
  • 4,245
  • 2
  • 21
  • 40
0

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.

Rakesh
  • 1
  • 1