Rails 3.1 application deployment tutorial
Here's my way of deploying Rails:
- Installing Ruby
I would not use RVM because it's very tricky to get it running properly in combination with stuff like cron jobs. Doable (with wrappers for example), but a bit of a hassle. If you don't need other Ruby versions on that machine, just install it from source yourself.
- Gems
Just let Bundler work its magic. Remember to install with the flags --without test development --deployment
. I wouldn't do that up front though. Just make sure you have bundler.
- Web server
Using Passenger (with either Apache or Nginx) is a fine and easy choice. When you install Apache from apt, it will run in a special user.
Apache is configured correctly automatically on Ubuntu. It will start on reboot.
- Website configuration
All configuration be in /etc/apache2. Place your virtual host configuration in /etc/apache2/sites-available and use a2ensite
to enable it.
Put your app in /var/www
, since that is the default location in Ubuntu. I usually make a deploy user.
Make sure that user can access your application by assigning your app to the www-data
group.
- Database
MySQL has its own user system. Log in as root user and create a new user with SQL: GRANT ALL ON 'application_production'.* TO 'deploy' IDENTIFIED BY 'some password';
- Assets
Assets should be generated as the user owning the application. You should add any css and javascript files to production.rb. For example:
config.assets.precompile += %w(backend.css)
- Conclusion
It helps to use a deploy tool like Capistrano. When you run capify
, uncomment the appropriate line in the Capfile
to get assets compilation. Here's mine:
ENV['RAILS_ENV'] = 'production'load 'deploy' if respond_to?(:namespace) # cap2 differentiatorload 'deploy/assets'load 'config/deploy'require 'capistrano/ext/multistage'require "bundler/capistrano"require 'capistrano_colors'
This is just how I normally install my rails apps. I hope this will get you going. Recently I've written a gem for integrating Chef-solo with Capistrano, called capistrano-chef-solo. It's very alpha and might be a bit too complicated if you're just starting with deployment, but it might help you.