In the last post, Basics of Rails Part 1, we created and ran the Rails application "attackresearch". Next, we will change the Web Server to Unicorn as well as introduce the concept of Rake.
Something to note, Rails typically is run in three modes:
- Test - Mode typically used for Unit Tests.
- Development - Development environment, includes verbose errors and stack traces.
- Production - Settings are as if you were running in this application in a production environment.
Now obviously, if you've done something custom like `export RAILS_ENV=production` this would be different. Additionally, explicitly casting the mode in which something like the Rails console runs (example: rails console production) will change the default behavior or mode, rather.
What does all this mean? Well, really it means that you want to develop in development mode and run a production application in production mode. Pretty simple huh?
Time to configure for Unicorn versus the default Webrick web server. If you are asking yourself "why", the answer is fairly straightforward. Unicorn is meant for production and handles a large amount of requests better and overall, is more configurable. For the purposes of this tutorial, we will use Unicorn for both development and production.
I want to demonstrate two ways of doing this. The first is by using a startup shell script. The other, for the purposes of an introduction to Rake tasks, will be to actually create a Rake task to start the application in lieu of a shell script.
Startup shell file:
Modify your Gemfile by uncommenting the line with the Unicorn gem. Also, while we are at it, let's uncomment the Bcrypt gem as well:
Run `bundle install`:
Make the startup script executable and fire it up:
The line `rvmsudo bundle exec unicorn $*` means...
- rvmsudo - Allows you to run sudo commands while maintaining your RVM environment.
- bundle exec = Directs bundler to execute the program which, automatically 'require'(s) all the gems in your Gemfile.
- unicorn - Unicorn service.
- $* - Any arguments passed to the script will be executed as part of the command inside of the script. Example: ./start.sh -p 4444 translates to - `rvmsudo bundle exec unicorn -p 4444` and would start the server on port 4444.