Capistrano Deployment in Dream Host

Posted on   09-10-2011 11:19 by admin

After wasting an entire Saturday leafing through old, unhelpful forum posts and trying to get a fresh Rails site deployed and running DreamHost without success I finally figured out my problem: Gem.cl

Really a good source to deploy rails 3 in Dreamhost, copied here so that incase if that site is down or takes out, still we have that valuable information here. All kudos to Christopher H Laco

Rails3, DreamHost and You
Sunday, December 19, 2010 11:53 PM
Christopher H. Laco

After wasting an entire Saturday leafing through old, unhelpful forum posts and trying to get a fresh Rails site deployed and running DreamHost without success I finally figured out my problem: Gem.clear_paths. But that isn’t the only problem. For the sake of helping others and myself on the next project, this is what you have to do to start a new Rails 3.x project on DreamHost.

Assumptions

For the sake of not covering too many details, I going to assume that you have a working ruby install, the rvm and bundler gems installed, ruby 1.8.7 installed using rvm, git installed and already have a GitHub and DreamHost account.
Create a new Rails 3.x site

First let’s create a fresh new Rails 3 application:

$ rails new example.com

A heap of output will fly but leaving you with a fresh new example.com directory full of Rails 3 goodness.
Create an RVM Gemset

Next, we’ll create an rvm gemset just for this project.

$ vim example.com/.rvmrc

Add this to your new rvmrc file:

rvm_gemset_create_on_use_flag=1
rvm use 1.8.7@examplecom

DreamHost currently has Ruby 1.8.7 installed so lets develop under that. We’re also auto creating a gemset just for this site.

Now chenge directory into your new project:

$ cd example.com

When rvm sees your rvmrc, it will switch to your 1.8.7 install, then create and switch to a new gemset for examplecom.

Using /Users/claco/.rvm/gems/ruby-1.8.7 with gemset examplecom

Bundle Your Gems

Now that we have a fresh new empty gemset for this project, we need to bundle our gems. First, open your Gemfile and uncomment the following line:

gem 'capistrano'

Since we’re going to use capistrano to deploy the site, we need to have bundler include it. While we’re in there, let’s add a specific version of sqlite-ruby. DreamHost currently has sqlite3-ruby 1.2.1 while the latest gem is 1.2.2. Since I have problems compiling it on DreamHost, let’s just use what they have:

gem 'sqlite3-ruby', '1.2.1', :require => 'sqlite3'

Save your Gemfile, then run bundler to install the required gems into your currently empty examplecom gemset:

bundle install
bundle package

This will install all of the gems needed to run this rails app and puts a copy into vandor/cache. When we deploy to DreamHost later, bundle install will pull from vendor/cache.
Make It Deployable

Now let’s make our project deployable to DreamHost. First, capify your site:

$ capify .

Next, let’s add bundler support to our deployment tasks. Open config/deploy.rb and add this to the top:

require 'bundler/capistrano'

Next, we need to tell capistrano about our deployment details while still in deploy.rb. Let’s start with some connection options:

default_run_options[:pty] = false
ssh_options[:forward_agent] = true
set :use_sudo, false
set :user, "dhusername"

First, we’re telling capistrano to not use a pseudo tty, or to use the normal login tty. Next, we tell ssh to foward our gent to the remote server, so it can use our local key to get to GitHub. Finally, DreamHost doesn’t allow sudo, so turn that off.

Now, let’s edit the file with our application/repository/git settings:

set :application, "example.com"
set :repository, "git@github.com:claco/example.com.git"
set :scm, :git
set :branch, 'master'
set :git_shallow_clone, 1
set :deploy_via, :remote_cache
set :copy_compression, :bz2
set :rails_env, 'production'
set :deploy_to, "/home/dhusername/#{application}"

The application name is used in later settings and is usually the site/domain name. We point it to our git repository for the site and tell it what branch to checkout, how to clone and to compress the transfer. We set the rails env to production and tell cap what folder on DreamHost to copy the site to.

Now all that’s left is the app settings telling it what server to deploy to:

role :web, "#{application}" # Your HTTP server, Apache/etc
role :app, "#{application}" # This may be the same as your `Web` server
role :db, "#{application}", :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"

Since we’re deploying to the same place for everything, we’ll just reuse the application name.

namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end

While you’re in there, we need to uncomment the task to restart the app after deployment since DreamHost uses Passenger:

namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end

Add Test Url

The default rails page is public/index.html, which doesn’t really tell you if the site actually runs. Let’s add a quick test action to our rails app to test later:

$ rails g controller test index

ENV for Passenger

This is the part that cost me a day. DreamHost Passenger knows nothing about your users GEM_PATH or GEM_PATH. If you were to push this app to DreamHost as is, you would get “unknown constant: Bundler” since DreamHost does not have bundler installed in the system gems.

To fix this, add this to the top of your config.ru:

ENV['GEM_HOME']="#{ENV['HOME']}/.gems"
ENV['GEM_PATH']="#{ENV['GEM_HOME']}:/usr/lib/ruby/gems/1.8"
require 'rubygems'
Gem.clear_paths

This will tell passenger where to look for gems when rails’ boot.rb fires. The Gem.clear_paths is critical.
Push to Git

Let’s push everything to GitHub before we move on to DreamHost setup.

git init
git add .
git commit -m "Initial Import"
git remote add origin git@github.com:claco/example.com.git
git push origin master

Configure DreamHost

Before we push the site to DreamHost, we need to configure a few things. First, we need to add GEM_PATH/GEM_HOME to your .bashrc so capistranos’ ssh session can find your local gems and gem binaries. SSH into your DreamHost account and edit .bashrc and add:

export GEM_HOME="$HOME/.gems/"
export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8"
export PATH="$HOME/bin:$HOME/.gems/bin:$PATH"

Now, install bundler:

$ gem install bundler

Now, go to the domains management page in the DreamHost Web Panel, enable the “Passenger (Ruby/Python apps only)” checkbox and change your Web directory to “example.com/current/public”.
Deploy!

Time for the moment of truth. Let’s deploy our app!

cap deploy:setup
cap deploy

If all goes as planned, http://example.com/ should show the rails index.html page and /test/index should show “Test#index Find me in app/views/test/index.html.erb”

    Source: http://chrislaco.com/blog/rails3-dreamhost-and-you/
    Link: http://chrislaco.com/blog/rails3-dreamhost-and-you/
Comments:
    Audel on   29-11-2011 09:56
    AFAIC that's the best aneswr so far!
    epbwnnocjh on   07-11-2016 22:45
    9OnHc1 <a href="http://pbczgdkjqsgb.com/">pbczgdkjqsgb</a>, [url=http://jchvgaauwftg.com/]jchvgaauwftg[/url], [link=http://jfhgqebeuadl.com/]jfhgqebeuadl[/link], http://icewumchjgkl.com/
    ztvogifci on   09-11-2016 07:56
    gExMOK <a href="http://cucjbwztuayd.com/">cucjbwztuayd</a>, [url=http://rjvgseofhyke.com/]rjvgseofhyke[/url], [link=http://lxadrxdapzyz.com/]lxadrxdapzyz[/link], http://lhmoolgorohs.com/
    dytsyjibd on   10-11-2016 03:42
    QqeITj <a href="http://leelsegjjlur.com/">leelsegjjlur</a>, [url=http://avoateompuxh.com/]avoateompuxh[/url], [link=http://hwszciqxfgsj.com/]hwszciqxfgsj[/link], http://zsjzwuexdvzo.com/
    yvrippq on   13-11-2016 11:18
    7ECSg3 <a href="http://lbswcbqufnag.com/">lbswcbqufnag</a>, [url=http://tvziebxpizik.com/]tvziebxpizik[/url], [link=http://kiarhqruktsp.com/]kiarhqruktsp[/link], http://iuzamykafaap.com/
    rpjordjqq on   16-11-2016 00:49
    daKr1U <a href="http://rkiqssmbhkfe.com/">rkiqssmbhkfe</a>, [url=http://ftncbsducpmb.com/]ftncbsducpmb[/url], [link=http://xiplejmqmbvg.com/]xiplejmqmbvg[/link], http://sekpjiqluolo.com/
    yofyndnfjc on   16-11-2016 16:24
    rsFDGj <a href="http://uqamcwqqwetc.com/">uqamcwqqwetc</a>, [url=http://mlxzegbsjtvi.com/]mlxzegbsjtvi[/url], [link=http://ngazkbgwmhal.com/]ngazkbgwmhal[/link], http://nlvmuiiwadfy.com/
    vziuuavakbl on   18-11-2016 18:47
    l7dtZF <a href="http://zhjogarbmiyq.com/">zhjogarbmiyq</a>, [url=http://umfijrxbptvk.com/]umfijrxbptvk[/url], [link=http://iquflsivdvuw.com/]iquflsivdvuw[/link], http://neohvxojozdl.com/
    autrvc on   19-11-2016 08:29
    KsI2aS <a href="http://fqvgelwsmyha.com/">fqvgelwsmyha</a>, [url=http://ffmlurvkdkly.com/]ffmlurvkdkly[/url], [link=http://odbtkelkdwdi.com/]odbtkelkdwdi[/link], http://kxczmszxzhwb.com/
    tedonx on   01-12-2016 00:37
    EwlzS0 <a href="http://buknnucojqxh.com/">buknnucojqxh</a>, [url=http://dpzzghjkmgyw.com/]dpzzghjkmgyw[/url], [link=http://kydkklgluzcw.com/]kydkklgluzcw[/link], http://pjocgrkaqgrs.com/
    ehtqssrikzd on   01-12-2016 11:25
    xbhs3N <a href="http://hepyzkeejtvc.com/">hepyzkeejtvc</a>, [url=http://embjeokpknio.com/]embjeokpknio[/url], [link=http://utxsrgmvnqso.com/]utxsrgmvnqso[/link], http://whlvtludnxig.com/
    olysppvy on   01-01-2017 17:46
    Xr7f2C <a href="http://bfbugxubvsrb.com/">bfbugxubvsrb</a>, [url=http://gqnffjbrtruf.com/]gqnffjbrtruf[/url], [link=http://jeavbctlsiyp.com/]jeavbctlsiyp[/link], http://kuqoztgfyvlz.com/
    hpgadjoic on   02-01-2017 19:48
    zpaWVG <a href="http://dvszesywjkku.com/">dvszesywjkku</a>, [url=http://ejgmdiavjxal.com/]ejgmdiavjxal[/url], [link=http://mamhtgljdkqx.com/]mamhtgljdkqx[/link], http://qssiycisfdfx.com/
    yjkccceaks on   03-01-2017 10:14
    nvoV0T <a href="http://cogscbpvxdxv.com/">cogscbpvxdxv</a>, [url=http://mqveruqfrfba.com/]mqveruqfrfba[/url], [link=http://jfxiddkegkqx.com/]jfxiddkegkqx[/link], http://yvdxtiymbvre.com/
    pbzrwqycxlw on   04-01-2017 13:41
    M9mPfW <a href="http://iviclsiyjuol.com/">iviclsiyjuol</a>, [url=http://luvmkhryhqhm.com/]luvmkhryhqhm[/url], [link=http://cstuqsgwekrk.com/]cstuqsgwekrk[/link], http://itopjmbgzerz.com/
    xteugov on   04-01-2017 15:19
    IKXRrn <a href="http://yietkdpoohvf.com/">yietkdpoohvf</a>, [url=http://liimfnbvdubo.com/]liimfnbvdubo[/url], [link=http://gxpuxwbeyigt.com/]gxpuxwbeyigt[/link], http://yvjgnhnklzgv.com/
    iijsxnzsqui on   05-01-2017 01:47
    yIW0ku <a href="http://elvfhswodrjs.com/">elvfhswodrjs</a>, [url=http://rrxmitldiezl.com/]rrxmitldiezl[/url], [link=http://gnfpdrgpgyqv.com/]gnfpdrgpgyqv[/link], http://uaovwqzaojhx.com/
    nlqqwczc on   14-01-2017 10:50
    ugJ7EY <a href="http://jamaxeruiikq.com/">jamaxeruiikq</a>, [url=http://wsjbqvsyzbfv.com/]wsjbqvsyzbfv[/url], [link=http://zhngvzbrigdz.com/]zhngvzbrigdz[/link], http://uffmvgztjwjg.com/
    pzptmprdg on   14-01-2017 14:09
    ybKRK6 <a href="http://qhoymgpodnwh.com/">qhoymgpodnwh</a>, [url=http://ubnzfojapjdz.com/]ubnzfojapjdz[/url], [link=http://vkhzgbqyjkdi.com/]vkhzgbqyjkdi[/link], http://uzoxtqfixovz.com/
    cvexovqu on   15-01-2017 23:11
    20IgOA <a href="http://ybbwsiqapeea.com/">ybbwsiqapeea</a>, [url=http://ipfjpsdysixn.com/]ipfjpsdysixn[/url], [link=http://fhkjcktzocha.com/]fhkjcktzocha[/link], http://pqrdmqvpnuqg.com/
    letagpnjoj on   16-01-2017 23:18
    d0Fv20 <a href="http://htipbirnabfj.com/">htipbirnabfj</a>, [url=http://ftrwwwxfwiqw.com/]ftrwwwxfwiqw[/url], [link=http://qwkttngxmibv.com/]qwkttngxmibv[/link], http://jrmfpjpcfpxn.com/
    kkaouiflvg on   17-01-2017 14:38
    xiHLJ7 <a href="http://vcvaiglhjsvx.com/">vcvaiglhjsvx</a>, [url=http://ikjhzoelrapo.com/]ikjhzoelrapo[/url], [link=http://fxqhvmaxhohw.com/]fxqhvmaxhohw[/link], http://rqzmnwvluzad.com/
    ocbrfhevuoj on   18-01-2017 10:27
    wqGa7h <a href="http://skvoofsgnpbe.com/">skvoofsgnpbe</a>, [url=http://mezdmsdqbusj.com/]mezdmsdqbusj[/url], [link=http://eydmhztiojim.com/]eydmhztiojim[/link], http://vojdavifcwrr.com/
    qskyvtdz on   19-01-2017 23:57
    V6eiVl <a href="http://moltvccdifef.com/">moltvccdifef</a>, [url=http://rgwjrdytillm.com/]rgwjrdytillm[/url], [link=http://szeghwuvrhff.com/]szeghwuvrhff[/link], http://qkjmfoyjethk.com/
    xuuqrekx on   20-01-2017 01:20
    ZZ2PzZ <a href="http://aydokjcngwnu.com/">aydokjcngwnu</a>, [url=http://vpyapbanhuyq.com/]vpyapbanhuyq[/url], [link=http://qdeyidzcncms.com/]qdeyidzcncms[/link], http://gyqxmdvlhwwo.com/
    knsgcmteiro on   20-01-2017 06:00
    r8SU5P <a href="http://vvivypperrwp.com/">vvivypperrwp</a>, [url=http://lizxkzwghbrn.com/]lizxkzwghbrn[/url], [link=http://cghtojzlngnj.com/]cghtojzlngnj[/link], http://glqaoexdpxvg.com/
    dojotxlvpw on   04-02-2017 22:35
    g1LcN1 <a href="http://pnehbalayluo.com/">pnehbalayluo</a>, [url=http://vgjqvlhqhtyq.com/]vgjqvlhqhtyq[/url], [link=http://ppqxqenzsemn.com/]ppqxqenzsemn[/link], http://dmqksxucpztl.com/
    jmgqyqif on   05-02-2017 02:28
    uVMlw2 <a href="http://fumxrjlswswk.com/">fumxrjlswswk</a>, [url=http://gexaurwtpozn.com/]gexaurwtpozn[/url], [link=http://xuhqaoqndran.com/]xuhqaoqndran[/link], http://npsbfbjdaasu.com/
    rffpunkrhj on   05-02-2017 08:11
    pPcXH9 <a href="http://rarhxfvhxrfw.com/">rarhxfvhxrfw</a>, [url=http://gtzmvhuecruu.com/]gtzmvhuecruu[/url], [link=http://dlzjtlykkddj.com/]dlzjtlykkddj[/link], http://nauqtehtujsq.com/
    kvzyqkja on   07-02-2017 15:12
    HOmCH5 <a href="http://dgvkhgasvvpk.com/">dgvkhgasvvpk</a>, [url=http://cqqwqtkqzlfp.com/]cqqwqtkqzlfp[/url], [link=http://ifpaksxuedtu.com/]ifpaksxuedtu[/link], http://watihptvufph.com/
    iiejwc on   07-02-2017 19:54
    uxuUfl <a href="http://egqkjgzudrnf.com/">egqkjgzudrnf</a>, [url=http://ozdoddbymyxq.com/]ozdoddbymyxq[/url], [link=http://hkxqyizeuqfc.com/]hkxqyizeuqfc[/link], http://aawspaeetlys.com/
    clbeattde on   08-02-2017 02:59
    xwl0xR <a href="http://abpvgzamodgw.com/">abpvgzamodgw</a>, [url=http://zmkavxtuzhdz.com/]zmkavxtuzhdz[/url], [link=http://kpzhgeedovlu.com/]kpzhgeedovlu[/link], http://cmdqoqvecdcf.com/
    efdieymdj on   09-02-2017 02:54
    d95uo2 <a href="http://xpwwmuuferkn.com/">xpwwmuuferkn</a>, [url=http://iycmfpfzggds.com/]iycmfpfzggds[/url], [link=http://ptysoptmojqc.com/]ptysoptmojqc[/link], http://mcgcgtwyayub.com/
    jxgbsy on   09-02-2017 07:50
    OBmA2s <a href="http://ovmfqvijonpv.com/">ovmfqvijonpv</a>, [url=http://qejvyosvziam.com/]qejvyosvziam[/url], [link=http://tflbkuytndoe.com/]tflbkuytndoe[/link], http://zatfgkaneeds.com/
    mmczbheygs on   10-02-2017 09:42
    EFXaYr <a href="http://mfxisvkfgpog.com/">mfxisvkfgpog</a>, [url=http://dbmspmrulbmv.com/]dbmspmrulbmv[/url], [link=http://ndsatotbinqz.com/]ndsatotbinqz[/link], http://zmveqnoicaev.com/
    vjdnvhyp on   10-02-2017 14:22
    fybfit <a href="http://zywugwvzcxuy.com/">zywugwvzcxuy</a>, [url=http://vwkiajhtrdrd.com/]vwkiajhtrdrd[/url], [link=http://dgxkjedtilzd.com/]dgxkjedtilzd[/link], http://ijoqcdqozpbk.com/
    nteemd on   11-02-2017 05:39
    Ulz7bz <a href="http://oiyglmxpjktb.com/">oiyglmxpjktb</a>, [url=http://lprvkvxtsuwq.com/]lprvkvxtsuwq[/url], [link=http://erniwrqlorsv.com/]erniwrqlorsv[/link], http://xiceswggdyvc.com/
    home page on   12-07-2017 05:03
    I have been surfing online more than three hours today, yet I never found any interesting article like yours. It's pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the web will be much more useful than
 

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.