Setting up Rails applications with cPanel

Having worked only with Apache/FCGI deployment of Rails during my day job (which in itself is a whole different bucket of pain), setting it up with cPanel looked like it should have been a breeze. Unfortunately there was a few catches which caused me no end of pain. I’ve got the excellent book, Deploying Rails Applications, but its information on shared hosting was rather skimpy, and did not seem to be directed towards hosts with cPanel installed. (I’d still recommend this book as a must for any serious Rails developers. It is incredibly useful.)

So here, documented, is the whole process in cPanel (although it will require ssh access to the server). I did find a good reference, but it didn’t address a specific problem I was having. (More on that later…)

Setting up the Rails application

From the Ruby on Rails menu option, generate a new application. cPanel will automatically generate a location for the application at ~/public_html/appname. Unfortunately, using this location makes the whole application’s code available to the public, so a better idea is to deploy to a subdirectory off your home directory, e.g. ~/rails_apps/appname. This can just be changed manually in the textbox. Select ‘Load on boot’ and production mode and hit create.

What you get

This will generate a rails application skeleton in ~/rails_apps/appname, as well as setting up the necessary values in ~/.cpanel/ruby-on-rails.db. In spite of its name, this is actually a YAML file which stores the deployment information for the app.

It should look something like this:

# ~/.cpanel/ruby-on-rails.db
---
-
  env: production
  loadonboot: 1
  name: appname
  path: /rails_apps/appname
  port: 12040

Pretty self-explanatory, really. Each of the entries lines up with particular values in cPanel, with the port automatically assigned during the creation process.

As far as the application itself, I’ll echo the suggestion in the wiki article mentioned above and suggest that you delete the skeleton application (leaving the directory only) and deploy your application either using Capistrano or a subversion checkout (e.g., from ~/rails_apps/appname:

svn co http://svn.yoursite.com/svn/trunk .

I use Unfuddle for my subversion hosting, since I can have free private repos; the other popular option for Rails is GitHub. If you’re up to the challenge, you can always set up your own subversion repository on your site (as per the example).

Creating the redirect

The aforementioned wiki article suggested not using the redirect option in cPanel; I didn’t have a problem with it, however. Create a redirect for the directory in which the rails application resides from the URL. I’ll assume it’s a stand-alone app (no other rails apps on the server), so it should redirect to the root of yoursite.com (or more correctly, a request to yoursite.com will redirect to this rails application running on this port).

The redirect in cPanel will create a redirect along the lines of:

APPNAME REWRITE URL ACTIONS
appname http://yoursite.com/http://yoursite.com:12040/ [SAVE] [DELETE]

What you get

Similar to the application creation, this generates/populates the file ~/.cpanel/ruby-on-rails-rewrites.db, which looks something like this:

# ~/.cpanel/ruby-on-rails-rewrites.db
---
-
  appname: rails_app
  domain: yoursite.com
  port: 12040
  rewritebasepath: /home/username/public_html
  url: ''

where username is replaced with your username.

Here is where I ran into problems. The ruby-on-rails-rewrites.db file is not the only location this writes to; however, it is the only one which is deleted if you delete the rewrite in cPanel. In creating the rewrite, cPanel also writes to ~/public_html/.htaccess, a point which eluded me for several days. (No matter what I did, it was always trying to redirect to a path/application which no longer existed…) The following lines will be added to .htaccess:

RewriteCond %{HTTP_HOST} ^yoursite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.yoursite.com$
RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:12040%{REQUEST_URI}" [P,QSA,L]

These lines must be deleted if you wish to remove the redirect entirely (e.g. you have moved the application, written a new application to be the default, changed ports, or you simply fucked up and want to start over.)

And that’s it. Almost.

Mongrel

Although there is a start application option in cPanel, I was finding this unresponsive at times, so I simply started mongrel manually. This is simply:

mongrel_rails start -e production -d -p 12040 -P log/mongrel.pid

Now, that’s all. I don’t know if anyone else has/had the same problem I did. If so, I hope this helped.

Advertisements

~ by MattR on December 18, 2008.

11 Responses to “Setting up Rails applications with cPanel”

  1. This was of great help to me. I deleted my .htaccess in public_html when trying to password secure my website and remove it. The rewrite stopped working and my new cPanel did not contain the ROR icon(change of hosting plans caused it). This really helped me to get things back to normal 🙂 Thanks!

  2. I’ve already gotten the same problem to get my rails application up and running using mongrel and cpanel in the last 3 days.
    Before i read this article i haven’t known yet about configuration files under ~/.cpanel directory. That was a surprise for me before i tried to follow this article, i was thinking that these files that i’ve missed.

    Then i deleted my entire rails application on my shared hosting server and followed this article from scratch.
    The result is just the same with what i’ve done before i followed this article. Here is the files as it’s on my server (~/public_html/.htaccess, ~/.cpanel/ruby-on-rails-rewrites.db, ~/.cpanel/ruby-on-rails.db):


    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^dimas-priyanto.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.dimas-priyanto.com$
    RewriteRule ^(.*)$ "http\:\/\/127\.0\.0\.1\:3000%{REQUEST_URI}" [P,QSA,L]

    ---
    -
    appname: my_rails_app
    domain: dimas-priyanto.com
    port: 3000
    rewritebasepath: /home/dimaspri/public_html
    url: ''

    ---
    -
    env: production
    loadonboot: 1
    name: my_rails_app
    path: /etc/rails_apps/my_rails_app
    port: 3000

    I don’t know yet why, when i start my rails application using:
    mongrel_rails start -e production -d -p 3000 -P log/mongrel.pid
    and i called http://dimas-priyanto.com/ it returns:

    Not Found

    The requested URL / was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    then when i called using port 3000, http://dimas-priyanto.com:3000/ it works for me.

    I hope you could help me..
    Do you have any suggestions for me..??

  3. Without knowing more, I’d suggest it is probably to do with the port you are trying to run the app on. Generally the ports available for the rewrite directives will be > 12000. When you create the app through cPanel, it will automatically assign a port in this range — just use this port when starting mongrel, etc.; it should automatically be generated for the ~/.cpanel/ruby-on-rails* files. Also make sure to get rid of any old Rewrite directives in ~/public_html/.htaccess.

  4. This article helped me alot, as far as alerting me to those specific files. One thing I found out is the code in .htaccess DID get deleted for me when I removed the rewrites from cpanel. However, I think I remember one case where it didn’t while I was messing with it. Here’s my problem right now: I want to make a rewrite, and this is what it looks like on cpanel:

    http://mysite.com/project to–> http://mysite.com:12004/project

    However, I dont want it to point to http://mysite.com:12004/project, i just want it to point to http://mysite.com:12004. It gives me an error because its looking for a folder called “project” within project.

    Any idea where that information is? I tried editing some things but cant find it. Thanks and great article!

  5. Great article the missing cpanel/ruby manual. I was old school of looking at file structure to see where the cpanel dropped its goodies. The rewite was causing me problems when I started over. Port 12000 -no problem from another article Creating and Running a Ruby on Rails application in cPanel-Crucial Paradigm which is from a hoster. But the locations where not listed -Thanks

  6. Hi,
    I found a webmasters guide to cpanel, I wonder if it has Ruby setup in it
    http://www.tng-resources.com/site/content/view/6/13/
    Thanks

  7. @parkbenchbruce,

    Thanks for the link — I haven’t seen the webmaster’s guide before. I don’t know if RoR is standard in cpanel these days; certainly it wasn’t available everywhere when I first started looking into this. Hopefully there’s something there in the guide, though.

  8. yes, but how do you go on developing after creating the app skeleton? Often, when developing with rails, you need shell access to run rails scripts (script/generate) to generate controllers, views and models. How can you do this if you are only using cpanel? Thanks

  9. @ralf, you will definitely need shell access (or at least an ftp upload) to deploy the application. Don’t develop it directly on your shared host, though!

    Build your app locally and then use Capistrano (or even just check it out of your SCM of choice) to deploy it to the server. If worst comes to worst (as it did with my old shared host) you can always use FTP upload to get the app across.

    Given the plethora of new hosting options, it’s not really necessary to go for shared hosting for a cheap solution anymore either. It’s much more affordable (even since I originally wrote this article) to get a basic VPS or use cloud hosting. For basic apps, you can’t beat http://heroku.com, really.

  10. Wonder if anyone else is having this issue:
    When I added the rewrite

    RewriteCond %{HTTP_HOST} ^yoursite.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.yoursite.com$
    RewriteRule ^(.*)$ “http\:\/\/127\.0\.0\.1\:12040%{REQUEST_URI}” [P,QSA,L]

    to my htaccess file in public_html with the correct domain and port filled I get this problem.

    If I access the site with just the domain name http://www.mydoamin.com

    I can see the html on the screen, I view the source and every line of the html is escaped and it prints to the screen as if it were text.

    If I access the site http://www.mydomain.com:port then it works just fine. It renders the html and does not escape it.

    Any suggestions ?

  11. wtf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: