Deploying my first rails site
Pick a Host and Sign Up
While I already have several hosts for other website work, I decided to find one with dedicated experience in Rails hosting (which none of mine have). I read around the web, looked at a number of hosts' sites, and considered my options. Different people have different opinions, needs, priorities, and experiences.
I ended up looking seriously at 2 options: slicehost.com, which gives you a virtual machine with root access to setup and destroy as you wish; and hostingrails.com, a more turnkey service with a cPanel, etc. For now, my priority is to get up quickly to experience the full develop, debug, deploy cycle, without getting too caught up in the server side details.
A basic account on hostingrails.com is only $3.59 per month (a cup of coffee and bagel). I get a shared IP, 5gb disk, and 20gb/mo bandwidth to use as my sandbox. Signup was instant. I created a subdomain in my registrar for this host, e.g. rails.myserver.com and pointed it to my new IP.
Establish SSH Key
ref: http://www.hostingrails.com/forums/wiki_thread/27
Connect to server, and accept the fingerprint:
local$ ssh 'user@rails.myserver.com'
server$ exit
If file doesn't exist ~/.ssh/id_dsa.pub , create one now: (accept defaults, just press enter when prompted)
local$ ssh-keygen -t dsa
Copy key to ther server
local$ ssh-copy-id -i ~/.ssh/id_dsa.pub user@rails.myserver.com
Oops, I dont have this command on my mac. Here's the manual steps. If ~/.ssh doesn't exist on the server, create it and copy over the id_dsa.pub file:
server$ mkdir ~/.ssh
local$ scp ~/.ssh/id_dsa.pub user@rails.myserver.com:.ssh/authorized_keys
However, if ~/.ssh/authorized_keys exists on the server then you need to append it instead:
local$ scp ~/.ssh/id_dsa.pub user@rails.myserver.com:homebox_dsa.pub
server$ cat homebox_dsa.pub >> .ssh/authorized_keys
server$ rm homebox_dsa.pub
Manual Deploy
First, I'll deploy my first Rails app manually (later I'll setup Capistrano). I'm setting up the host for a single app, for now (especially since that's how most of the wiki doc's on hostingrails are written), and then redo it for multiple domains and/or subdirectories.
ref: http://www.hostingrails.com/forums/wiki_thread/1
1. Copy over the database
I won't go into explanations here. I'm using mySQL5 on both systems. You can do this through GUI tools (phpMyAdmin, CocoaSql) or via command line. On the server, create the database, create a user, assign user to database with permissions, then load the schema (and data, if you want). For example,
local$ mysqldump -p --opt myproject_development >myproject.sql
local$ scp myproject.sql user@rails.myserver.com
server$ mysql -u hruser_dbuser -p hruser_dbname <myproject.sql
2. Create online versions of config files
Here's how hostingrails recommends keeping track of server versions of these files: create copies named environment.rb.online and database.yml.online.
database.yml.online only needs to contain:
production:
adapter: mysql
database: [your_hostingrails_username]_[your_database_name]
username: [your_hostingrails_username]_[your_database_username]
password: your_password
and in environment.rb.online uncomment the line:
ENV['RAILS_ENV'] ||= 'production'
Also, change .htaccess to use dispatch.fcgi (from dispatch.cgi)
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
and comment out this line at the top:
# AddHandler fastcgi-script .fcgi
3. Generate empty Rails app on server
server$ rails myproject
4. Upload only the folders and files needed by the app
- whole app folder
- stuff in public/ incuding images/, javascripts/, stylesheets/, .htaccess
- config/database.yml.online, config/environment.rb.online
5. Make public folder public_html
server$ mv ~/public_html ~/public_html_backup
server$ ln -s ~/apps/myproject/public ~/public_html
6. Restart your server process and go
server$ killall -usrl dispatch.fcgi
link browser to the server (e.g. http://rails.myserver.com) TaaDa! (fingers crossed). If problems, check the log/production.log file
Setting Up Capistrano
Capistrano lets you control all your server side setup and processes from your local machine's command line. First I do a generic Capistrano install.
ref: http://www.hostingrails.com/forums/wiki_thread/5
local$ sudo gem install termios
local$ sudo gem install -y capistrano
Capistrano-ize my app
local$ cd myproject
local$ cap --apply-to .
That creates the files config/deploy.rb and lib/tasks/capistrano.rake
Configure for a single project on a single server. In deploy.rb, set vars as needed:
set :domain, "rails.myserver.com"
set :user, "myaccount"
set :application, "myproject"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :rails_env, :production
set :chmod755, %w(app config db lib public vendor script tmp public/dispatch.cgi public/dispatch.fcgi public/dispatch.rb)
set :use_sudo, false
roles:
role :web, domain
role :app, domain
role :db, domain, :primary => true
Capistrano + local svn
OK. Now, my objective is to deploy from my local Subversion repository. (Although it's more common to deploy from a shared svn repos over the Internet, I'm keeping my repos local because a) i dont need to share them right now, and b) i have a relatively slow internet connection).
ref: http://blog.wolfman.com/articles/2006/12/06/a-capistrano-scm-module-for-local-svn-access
Download this, de-tar, and put the 2 files into lib/tasks/:
http://blog.wolfman.com/files/local_subversion_with_rsync_3.tar.gz
Add these lines to deploy.rb (change your paths as needed):
require 'lib/tasks/local_subversion_rsync.rb'
set :scm, Capistrano::SCM::LocalSubversionRsync
set :repository_is_not_reachable_from_remote, true
set :tmpdir_local, "/Users/me/temp"
set :tmpdir_remote, "/home/user/tmp"
set :repository, "file:///Users/jonathan/svn/myproject/trunk"
[ssh option]
Add fcgi tasks (for Mongrel see the wolfman post).
# FCGI TASKS
desc "FCGI is already running, so we don't really need the spinner script, but we have it create the initial symlink for us"
task :spinner, :roles => :app do
run "rm -rf /home/#{user}/public_html;ln -s #{current_path}/public /home/#{user}/public_html"
end
desc "Restart the FCGI Process"
task :restart, :roles => :app do
run "cd #{current_path}; killall dispatch.fcgi"
cleanup
end
# GENERAL TASKS
desc "Set the proper permissions for directories and files on HostingRails accounts"
task :after_deploy do
chmod755.each do |item|
run "chmod 755 #{current_path}/#{item}"
end
end
Cap Deploy
Do this once, the first time.
local$ cap setup
local$ cap cold_deploy
Then anytime you have a new release to deploy
local$ cap deploy
or
local$ cap deploy_with_migrations
There's some final fixup that I have to do manually. These should be integrated into this process, but I'll leave that for another day.
server$ cd ~/apps/myproject/config
server$ mv database.yml.online database.yml
server$ mv environment.rb.online environment.rb
Lastly, the public/dispatch.fcgi got overwritten and has the ruby path on my mac rather than the server, so change the first line to:
#!/usr/local/bin/ruby
OK. Let's restart the server and go
local$ cap restart
Browse to your site.
(Again, crossing your fingers might help, although it can get in the way of typing on the keyboard).
Comments
>>
This is great! I'll look in to the capistrano-part and get it fixed with my rails app. Right now i edit a file, find it in the finder and upload it manualy... :)
Any issues with hostingrails.com?
I've started looking for rails hosts. Now that things are running, have you noticed any problems with Hostingrails.com?
Re: Any issues with hostingrails.com?
Hi Joe, no problems, but I'm not really in a position to answer because its still a sandbox for me and I havent yet deployed a live site.
New Comment