Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a Rails site: development vs production

I'm learning Ruby on Rails. At the moment I'm just running my site locally with rails server in the OS X Terminal. What changes when a Rails site is run on a production box?

  • Is the site still started with rails server?
  • Any differences with how the db is setup?

Note: I'm running Rails 3.

like image 262
SundayMonday Avatar asked Nov 26 '11 17:11

SundayMonday


People also ask

How is rails ENV set?

The ENV hash in your Rails application is set when your Rails application starts. Rails loads into ENV any environment variables stored in your computer and any other key-value pairs you add using Figaro gem.


2 Answers

A rails app can be run in production calling rails server -e production, although 99% of the time you'll be serving on something like passenger or thin instead of WEBrick, which means there's a different command to start the server. (thin start -e production for instance)

This is a complicated question, but the best place to start learning about the differences would be to look at the specific environment.rb files. When rails boots up it starts with the environment file that matches the called environment, ie if you start it in development it begins by loading your development.rb file, or if you're in production it will load the production.rb file. The differences in environments are mostly the result of these differences in the various environment config files.

Basically if a Rails 3.1 app is in production mode, then by default it is not going to be compiling assets on the fly, and a lot of caching will be going on that isn't happening in development. Also, when you get error messages they will be logged but not rendered to the user, instead the static error page from your public directory will be used.

To get more insight into this, I would suggest reading the relevant rails guides:

Rails Initialization Guide: http://guides.rubyonrails.org/initialization.html

Rails Configuration Guide: http://guides.rubyonrails.org/configuring.html

like image 89
Andrew Avatar answered Sep 19 '22 19:09

Andrew


There are two contexts you can use the word "production" here. One of them is running the server in production mode. You can do this locally by,

RAILS_ENV=production ./script/server 

The configuration for this is picked up from config/environments/production.rb. Try comparing this file with config/environments/development.rb. There are only subtle differences like caching classes. Development mode makes it easier so that it will respond to any changes you make instantly. Plus there are two different databases (by default) will be used namely yourproject_development and yourproject_production if you choose to run your server in either of these modes.

On the other hand, rails deployment to a production box is something different. You will need to pick your server carefully. You may have to deal with a deployment script may be capistrano. You may also need a load balancer such as netgear. The database also may require a deep consideration like size expectation, master/slave clustering etc.,

Note: I have never used Rails 3. This answer is biased towards 2.3.x.

like image 35
bragboy Avatar answered Sep 17 '22 19:09

bragboy