Sign In
Start Page | Recent Changes | All Pages | Orphan Pages | Junebug Help

How To Deploy on Dreamhost

Using Phusion Passenger (recommended)

Dreamhost now supports running Rails applications through Phusion Passenger, a.k.a. mod_rails, which is both easier and faster than using FastCGI on a shared host. This guide assumes that you want to use the “unpacked” version of Radiant with any extensions you need. It is possible to use the Radiant gem at Dreamhost, but it will require you to follow the steps below — under Using FastCGI — although your installation can still run under Passenger when Radiant is installed in this manner. However, installing as a gem holds no real advantage over the “unpacked” version.

You can either deploy using Capistrano, which is highly recommended if you plan to add extensions and make changes to the default Radiant code, or you can deploy using SFTP, if you just want to upload and use Radiant without touching any Ruby code. For Capistrano deployment, you should check Radiant into a subversion repository. For SFTP, you can just export Radiant into a local directory using this command, substituting “rel_0-6-7” with the newest version of Radiant:

svn export http://svn.radiantcms.org/radiant/tags/rel_0-6-7/radiant/

You can also InstallDreamhostUsingSSH

Creating the website

Add a new fully hosted domain in the Dreamhost Web Panel (Domains > Manage Domains > Add New Domain). Check the box next to “Ruby on Rails Passenger (mod_rails)?”.

In the textbox “Specify your web directory”, add ”/current/public” to the path. “current” is a directory that is always symlinked to the current release of your Rails applicaiton by Capistrano. “public” is where Passenger finds the dispatch ruby file and your image, stylesheets, javascripts and other public assets.

If you do not want to deploy using Capistrano, but rather just use SFTP (alternative 2), you should just add ”/public” to the web directory instead of ”/current/public”, and you can ignore the last text paragraph in this section.

When you have created the website, you must ssh or sftp into your new domain (ssh username@domain.tld, on Windows; use Putty or Filezilla) and delete the “current” directory in the directory of your domain (/home/username/domain.tld/current) that Dreamhost has automatically created. This is important, since Capistrano needs to create a symlink called “current”, and cannot if a directory of the same name already exists.

Creating the database

Add a new MySQL database in the Dreamhost Web Panel (Goodies > Manage MySQL). If you have not yet done so, rename “database.mysql.yml” to “database.yml” and delete the other “database.xxx.yml” files in the Radiant config directory. Enter “host: mysql.domain.tld” and your selected database name, username and password into database.yml in the production part of the file. Delete the “socket: ” line in the production part, as it does not apply to Dreamhost.

Deploying using Capistrano (alternative 1)

Open up your Radiant application in a shell and run the command “capify .”. This will add a Capfile and deploy.rb into the config directory of the application.

Copy this Capistrano recipe into deploy.rb, overriding the existing code in the file:


set :user, 'my_dreamhost_user'
set :application, "my-passenger-domain.com" 
set :repository, "my_svn_repository_url" 
set :svn_username, "my_svn_username" 

# =============================================================================
# You shouldn't have to modify the rest of these
# =============================================================================

role :web, application
role :app, application
role :db,  application, :primary => true

set :deploy_to, "/home/#{user}/#{application}" 
# set :svn, "/path/to/svn"       # defaults to searching the PATH
set :use_sudo, false
# set :restart_via, :run
set(:svn_password) { Capistrano::CLI.password_prompt }

# saves space by only keeping last 3 when running cleanup
set :keep_releases, 3 

# issues svn export instead of checkout
set :checkout, "export" 

# keeps a local checkout of the repository on the server to get faster deployments
set :deploy_via, :remote_cache

ssh_options[:paranoid] = false

# =============================================================================
# OVERRIDE TASKS
# =============================================================================
namespace :deploy do

  desc "Restart Passenger" 
  task :restart, :roles => :app do
    run "touch #{current_path}/tmp/restart.txt" 
  end

  desc <<-DESC
    Deploy and run pending migrations. This will work similarly to the \
    `deploy' task, but will also run any pending migrations (via the \
    `deploy:migrate' task) prior to updating the symlink. Note that the \
    update in this case it is not atomic, and transactions are not used, \
    because migrations are not guaranteed to be reversible.
  DESC
  task :migrations do
    set :migrate_target, :latest
    update_code
    migrate
    migrate_extensions # Added to support Radiant Extensions
    symlink
    restart
  end

  desc "Migrates Radiant Extensions" 
  task :migrate_extensions do
    rake = fetch(:rake, "rake")
    rails_env = fetch(:rails_env, "production")
    migrate_env = fetch(:migrate_env, "")
    migrate_target = fetch(:migrate_target, :latest)

    directory = case migrate_target.to_sym
      when :current then current_path
      when :latest  then current_release
      else raise ArgumentError, "unknown migration target #{migrate_target.inspect}" 
      end

    run "cd #{directory}; #{rake} RAILS_ENV=#{rails_env} #{migrate_env} db:migrate:extensions" 
  end

end

Enter the correct values into the user, application, repository and svn_username. “application” is the domain of your Radiant application. “repository” is the full url to the Subversion repository you have stored Radiant into, and “svn_username” is your username for accessing that repository. Check your changes into Subversion.

Setup the server for Capistrano and make your first deployment running these two commands from your local shell in the root of the Radiant application:

cap deploy:setup
cap deploy
If you get stuck here, because Capistrano can’t access your Subversion repository, ssh into the server and make a checkout of the repository to a temporary directory:

ssh my_dreamhost_user@my-passenger-domain.com
mkdir temp
cd temp
svn co my_svn_repository_url

Deploying using SFTP (alternative 2)

SFTP is not the same as FTP, and ordinary FTP programs such as CuteFTP does not support it. For SFTP on Windows I recommend Filezilla, for Mac I recommend Cyberduck.

Setup a new SFTP site in your SFTP application with your Dreamhost domain as host, and your Dreamhost SSH user. Upload the Radiant code to the my-passenger-domain.com directory in your home directory. If you for any need to restart you site later on, you can delete and recreate the file “restart.txt” in the “tmp” directory of Radiant.

Bootstrapping the database

Once you have made your first succesful deployment, you must ssh into the server and run these commands to initialize the Radiant database:

ssh my_dreamhost_user@my-passenger-domain.com
cd my-passenger-domain.com/current
rake production db:bootstrap

That’s it. You should now have your very own Radiant instance running on your Dreamhost domain using Phusion Passenger.

Using local gems

If your application uses any local gems, including Radiant, you must specify the gem path, typically in config/environment.rb:
ENV['GEM_PATH'] = 'path-to-your-gem-repository'

See Dreamhost Passenger for more details.

Using FastCGI (not recommended)

I have just installed radiant in my dreamhost setup, and I also saw that this page was empty. So this is the best moment to write all the steps down while my memory is still fresh! This document is mostly a compendium of several sources, I will try to cite them when possible.

Having Radiant installed in Dreamhost requires to follow several steps, covering activation of FastCGI support, custom installation of gems, creating the MySQL databases, setting up the environment, and certain customizations. I now will describe them in detail.

Enabling FastCGI

Go to your Dreamhost Control Panel, in Domains → Manage Domains, select the domain that is going to host the Radiant installation and click on “Edit” in the WebHosting column. Choose it, and then make sure ‘FastCGI Support’ is enabled.

If you want a step by step with screenshots, visit the entry made by William in RailsHosting, Deploying a Rails App with DreamHost.

Custom Installation of gems

Radiant is not included in the standard installation on Dreamhost, so we need to add the required gems. For this, you have to setup a custom gems repository in your local directory. The instructions are listed in the Dreamhost Wiki, in this link. I will summarize them here to make it more comfortable for all.

1. Log in into a shell, for the user who is hosting the domain where you want to set Radiant.

2. Create the ’.gems’ directory, setup the environment variables, and load the new setup. To do this, add the following lines to ‘bash_profile’, and then update the environment running source .bash_profile

export GEM_HOME="$HOME/.gems" 
export GEM_PATH="$GEM_HOME:/usr/lib/ruby/gems/1.8" 
export PATH="$HOME/.gems/bin:$PATH" 
3. Edit or create the ’.gemrc’ file, and add the following (replace username appropriately)

gemhome: /home/yourname/.gems
gempath:
- /home/yourname/.gems
- /usr/lib/ruby/gems/1.8

Creating the Database

Radiant will need one database, and I will show here how to create a MySQL database to use with Radiant. Go to the Dreamhost Control Panel, under Goodies → Manage MySQL. Follow the on-screen instructions to create a new database. Note that you need a separate hostname (you can create one in the same page). Take note of the hostname, database name, username and password, as they will be needed in the Radiant configuration.

Install and Setup Radiant

So we are finally here! Go to the Shell window, and follow the standard Radiant installation instructions. Of course, always refer to the latest installation instructions in Installation. Note that path/to/project is not necessarily the actual web path (actually, that is not recommended). I suggest you create something like ‘radiantcms’ in you home directory.

% gem install --include-dependencies radiant
% radiant --database mysql path/to/project

Then, configure the database. Modify the config/database.yml to point to your MySQL database, using the parameters you copied earlier. Set it as the production database.

Finally, initialize Radiant:

% rake production db:bootstrap

Modifications needed for Dreamhost

In order for everything to work properly, you need to change some directory permissions, modify some scripts, and finally make a link to make your Radiant installation available to the public.

Change directories to the ‘radiantcms’ and ensure the public directory is publicly accessible. Type:


chmod -R 755 public
Check the public/.htaccess file, if it is set up properly. Modify the appropiate RewriteRule line to point to dispatch.fcgi instead of dispatch.cgi. It should look like this:

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Apparently, the environment variables we just setup get overwritten somewhere in the processing flow, so we have to force them. Modify the public/dispatch.fcgi file, and add the following before the ‘require’ lines (don’t forget to adjust the username):

ENV['RAILS_ENV'] = 'production'
ENV["GEM_HOME"]="/home/username/.gems" 
ENV["GEM_PATH"]="/home/username/.gems:/usr/lib/ruby/gems/1.8" 

Ensure the selected environment is production in the config/environment.rb file. Uncomment the line:


ENV['RAILS_ENV'] ||= 'production'

Finally, create a link in your host to point to your radiant installation (modify according to your setup):


% ln -s ~/radiantcms/public ~/hostname/radiant

or if you prefer to have it as a subdomain:


% ln -s ~/radiantcms/public ~/subdomain.hostname

That’s it, Enjoy!

Check the References for more information and for special cases.

References

Deploying a Rails App with DreamHost

Dreamhost Wiki, RubyGems

Dreamhost Wiki, RubyOnRails


Version 33 (current) | Other versions: « older versions
Page last edited by adsmart on October 09, 2008 09:42 PM (diff)