Skip to content

Instantly share code, notes, and snippets.

@jmejia
Last active May 30, 2018 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jmejia/8f6507d3faa92ff21f0b to your computer and use it in GitHub Desktop.
Save jmejia/8f6507d3faa92ff21f0b to your computer and use it in GitHub Desktop.
  • Attempt to start your app in production
$ RAILS_ENV=production rails s
  • If you haven't run your app in production yet you will likely see an error that says something like "Missing secret_token and secret_key_base for 'production' environment". Note: you won't see helpful errors in your browser since this is production. Check your server log in your terminal. You will need to set the ENV["SECRET_KEY_BASE"] referenced in config/secrets.yml.

To generate a key you can run:

$ rake secret

It's common for students to be using Figaro to store environment values. If you'd like to use that, follow these instructions.

  • Once that's done, we'll need to set that value for our app. If you are using Figaro, set a key for SECRET_KEY_BASE. Open config/application.yml and add this line.
production:
  SECRET_KEY_BASE: <the value from rake secret goes here>

Now when you restart you server there might not be any indication of what is going wrong. Let's improve the error output so it shows up in our server output. Out of the box Rails will write to logs/environments/production.log when running in production mode which can be tricky to debug. We can adjust this behavior by adding this snippet to config/production.rb. More details here.

if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = config.log_formatter
  config.logger = ActiveSupport::TaggedLogging.new(logger)
end

Add the key RAILS_LOG_TO_STDOUT to your application.yml file with a value of true.

Depending on your version of Rails you may need to create your database. If it's a problem you will see errors that would indicate this. Run RAILS_ENV=production rake db:{create,migrate} to fix the problem.

To make sure everything is working well, edit some CSS and make sure we see the changes in production mode. Don't forget to precompile your assets.

Chances are you aren't seeing the changes in the browser. This is because most of the time when we are running Rails in production it's on a web server using something like Apache or Nginx to serve the CSS, JS, and images. Your local machine is probably not configured this way so we need to add one more thing to config/environments/production.rb.

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?

Make sure to add RAILS_SERVE_STATIC_FILES to your application.yml.

Different versions of Rails handle things slightly differently so YMMV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment