Hi
Some of my servers are hosted on Planethoster (shared environment).
So far it has been a nice experience, except that their customer support’s technical competencies can vary a lot depending on who gets to help you there.
Regarding their Ruby on Rails setup, it uses Mongrel over Apache, and provide cPanel as an admin interface. I found out that many of their servers have different versions of Mongrel, Ruby on Rails, and Rubygems. This can be quite annoying all the more so as some of them have incompatible versions, hence the impossibility to sometimes create new rails projects from the cPanel, or even start servers. The problem becomes even more complicated when you try to install your own gems in your user space.
So here are my advices on how to deal with a correct Ruby on Rails setup there.
The example is taken from my own Redmine installation (which you can check here).
- Get an SSH access to your server. This is obvious, as otherwise you will have to deal with their crontab and scripts sent by FTP to launch your database migrations and other rails maintenance tasks.
- If you want to create your Rails application, follow those steps:
- First try to create the Rails application using the cPanel. If it fails because of non matching Rails version or another unknown error, don’t worry, we’ll create it manually.
- To create the Rails application manually, just use your SSH access, and try running your
rails
commands in the~/rails_apps
directory.There, you can see the real errors preventing the creation, and take corrective actions if needed: install the gems you want (even a complete new Rails version if needed), modify yourGEM_PATH
environment variable if needed (in this case you’ll have to remember the working value to set it later in further points).Although I did not try it, I think you can also install and use other Mongrel versions in your user space.
- If you want to set up an existing Rails application in this environment (the
~/rails_apps
directory and the cPanel) for the first time, follow these steps:- First try to create a new empty Rails application using the cPanel. If it fails because of non matching Rails version or another unknown error, don’t worry, set it up manually by following step 2.B.
- Then use your SSH access to copy all your Rails application files in the created directory.
- Create a config file setting the environment for your own gems. This is necessary if you want your own gems (saved in your user space) to be used. For example here is the file I created:
require 'rubygems' ENV['GEM_PATH'] = "/home/username/ruby/gems:/home/username/.gem/ruby/1.8:/usr/lib/ruby/gems/1.8" Gem.clear_paths
This file will be used to enforce the setup of your gems in any Ruby environment, as it is not always done by default (crontab environments or cPanel starting Mongrel may not set it); this way you will be on the safe side.
- Require your environment file in your Rails’ Rakefile. This is needed for rake tasks to execute with your gems.
# Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/switchtower.rake, and they will automatically be available to Rake. require '~/rtenv.rb' require(File.join(File.dirname(__FILE__), 'config', 'boot')) require 'rake' require 'rake/testtask' require 'rake/rdoctask' require 'tasks/rails'
- Require your environment file in your Rails’ environment. This will make sure that your gems will be used when starting and Rails command (including the server itself).If you use
~
while requiring your environment file, you will want to also manually set theHOME
environment variable before, as sometimes it is not set when the Mongrel service is started from the cPanel.This is also the place to set the relative path of your Rails’ root URL (only if your Rails’ root is in a sub path). For example, my server’s root URL ishttp://mysite.com/redmine
.# Be sure to restart your web server when you modify this file. # Set the relative path to the root URL ENV['RAILS_RELATIVE_URL_ROOT'] = '/redmine' # Force the HOME environment variable (used for ~ requirements) ENV['HOME'] = '/home/username' # Require our gem environment require '~/rtenv.rb' # Uncomment below to force Rails into production mode when # you don't control web/app server and can't set it the proper way # ENV['RAILS_ENV'] ||= 'production' ...
- If the cPanel creation of your Rails application succeeded on steps 2 or 3, and if you want to use the default Mongrel version, you should be ready to go. After creating your database, migrating it, etc… you should be able to run your server using cPanel.Don’t forget to create a Rewrite rule using your cPanel if you want it to be accessible without specifying the port.
- If you want to use your custom Mongrel version, or if the cPanel creation failed, no worries, everything can be done by hand:
- Create a script that will launch your Mongrel server. Don’t forget to add execution permissions to it. This is very easy, for example this is mine:
#!/bin/sh cd ~/rails_apps/redmine /usr/bin/ruby /usr/bin/mongrel_rails start -p 12006 -d -e production -P log/mongrel.pid
If you want to use your own Mongrel version, you should first setup the
GEM_PATH
environment variable, and then change themongrel_rails
path to use your own one. - Create a script that will stop your Mongrel server. Don’t forget to add execution permissions to it.
#!/bin/sh cd ~/rails_apps/redmine /usr/bin/ruby /usr/bin/mongrel_rails stop -p log/mongrel.pid
- Now you can start and stop your server from your terminal. It will be daemonized, so you can kill your terminal afterwards, your server will still be up.It will be listening on the port you will have specified in the start script (12006 in my example). So you can access it through
http://mysite.com:12006/
.You can then use those scripts in other scripts, crontabs or any other way you want to start and stop your server. - If you want to use normal URL without specifying the port, and eventually a relative path for your root URL, you will have to create manually the Apache Rewrite rules.This is done in your root
.htaccess
file:RewriteEngine on RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ RewriteRule ^redmine/(.*) "http\:\/\/127\.0\.0\.1\:12006\/$1" [P,L]
If you don’t want the relative root URL redirection, just remove the
redmine/
part from my example:RewriteEngine on RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:12006\/$1" [P,L]
- Create a script that will launch your Mongrel server. Don’t forget to add execution permissions to it. This is very easy, for example this is mine:
And now you should be set up, with your Rails server running the way you want:
- with your own Rails version,
- with your own gems,
- with your own Mongrel version (if you don’t use cPanel),
- with the root URL you want for it,
- with the complete control on when to start and stop it, even without cPanel.
Cheers!
Muriel