Running a Rails application in the production environment is a crucial part of any project. It represents the successful completion of a working application and delivery of that application to its customer base. It is rarely the final stage. All applications we have worked on have needed modifying from their initial production incarnation.
So, what is needed to get a Rails application into the production environment?
This chapter will describe how to create a Rails production environment. In particular, the decisions we need to make to successfully get a small business application up and running. Error handling in the production environment is a little different to that of the development environment and therefore some coverage of error handling is presented. We will also describe some systems that will make it easier to backup and restore your application.
In many Rails tutorials and guides there is a common theme: don't worry about the production environment, it is something you can sort out later. I strongly believe this is a mistake.
Our application may be the most beautifully crafted piece of coding ever created, but if we cannot present it to our users, we might as well have created a chocolate teapot.
Therefore, it is important, before we spend time developing our application, that we create and test our production environment. This is particularly important with Rails because at the time of writing, creating a good production environment can be the most problematic part of Rails development. The good news is that solutions to most of the common problems are being developed and are coming on line all the time. In particular, the creation of Mongrel (a Ruby web server that will be discussed later in the chapter) has greatly simplified the process and removed the worst headaches.
Rory comes to similar conclusions. He does not want to spend all week creating his application just to find that it only works on his computer. The whole point of the task Ken has set for him is to make the contact data available to everyone in the company. Even if others can browse his computer, he does not want his system to slow down to provide everyone else with a new service. However, he also spots a conundrum: "I need an application to test the production environment, but I want to do the test before I create an application." The solution is to create a simple test application that can be passed to the production system and tested there. Scaffold makes it very easy to create a basic working application. So, Rory creates and develops a test application:
$ rails TestApp
$ ruby script/generate model TestThing
001_create_test_things.rb
by removing the comments. He thereby creates the migration code shown below. As this is a very basic test, only one database field is required.class CreateTestThings < ActiveRecord::Migration def self.up create_table :test_things do |t| t.column :name, :string end end def self.down drop_table :test_things end end
database.yml
to suit. The resulting database.yml
is shown below. Notice that all environments use the same database schema and therefore have the same content.development: adapter: mysql database: Test username: root password: password host: localhost test: adapter: mysql database: Test username: root password: password host: localhost production: adapter: mysql database: Test username: root password: password host: localhost
$ rake migrate
"http://localhost:3000/test_things/list"
$ ruby script/generate scaffold TestThing $ ruby script/server
Rory, now, has a working Rails application that he can test in his production environment. It has only the minimum of functionality, but it is all that is needed to demonstrate the viability of the production environment.
3.15.237.164