Tag Archive for 'development'

Deploy a rails app to bluehost without SSH

rails on bluehost

I’ve been doing a little free-lance web development again lately. It’s been a while and the landscape’s changed in the 2 or 3 years since I was last freelancing. Specifically the ‘minimum requirements’ for a public facing site and the quality of the ‘cheap’ generic hosts have both increased (for the better). In light of these two factors I’ve been having a go at making some basic ‘web-presence’ sites using ruby on rails.

Learning enough rails to make a simple site was easy enough, but the deployment turned out to be a bit of a puzzle. Particularly as bluehost, whose control panel is otherwise excellent, just doesn’t seem to have its rails story sorted. To make matters worse, every “rails bluehost” tutorial out there starts with “you need SSH access” and bluehost now requires a faxed or emailed copy of the account-holder’s drivers license to grant SSH access. Seeing as my clients are generally the hosting account holders, this wasn’t an option for me.

After a bit of trial and error I’ve discovered that you can indeed deploy a basic Ruby on Rails site to bluehost without SSH access (assuming you have ftp access). Here’s how:

  1. Create rails directory.Use your ftp client to create a directory alongside the {app} directory to which the domain/sub-domain points.I’ve called mine {app}_rails, but it can be called anything.

  2. Upload rails app.Upload your rails app into the newly created {app}_rails directory.

  3. Symlink to rails public directory using temporary PHP.We need to symlink the {app} directory (to which the domain/sub-domain points) to the {app}_rails/public directory. Ordinarily we’d use the trusty ln -s command, but we can also do it without SSH access by using PHP’s symlink function.

    Create a php script on your local machine (I’ve called mine symlink.php) with the following contents (replacing the {account} and {app} tokens with your own). Make sure the {app} directory doesn’t already exist (rename/move/delete it if it does).

    <?php symlink('/home/atfraser/public_html/maketimelapse_rails/public', '/home/atfraser/public_html/maketimelapse'); ?>

    Upload the symlink.php to an accessable directory and then browse to it in your browser. If the script succeeds you’ll be greeted with a blank page (otherwise you’ll get errors). Once run successfully, you can delete the symlink php script (it’s no longer needed).

  4. Ensure dispatch.fcgi exists.To run under apache, a rails app needs fcgi dispatchers. My understanding is that rails 2.3 no longer fcgi dispatchers by default so if you didn’t use the --with-dispatches option when you created your app (I didn’t) you’ll need to create them on your local machine using the following command:

    rake rails:update:generate_dispatchers

    Then upload the created dispatch.fcgi, dispatch.cgi and dispatch.rb files from your localpublic directory to {app}_rails/public directory on bluehost.

  5. Enable dispatch.fcgi in .htaccess.Create a .htaccess file inside your {app}_rails/public directory with the following contents:

    AddHandler fcgid-script .fcgi
    AddHandler cgi-script .cgi

    RewriteEngine On

    RewriteBase /
    RewriteRule ^$ index.html [QSA]
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

    ErrorDocument 500 <h2>Application error</h2>Rails application failed to start properly
  6. Check permissions using FTP client.We need to check/set some permissions. Again, we can’t use the standard chmod command without SSH access but luckily most modern ftp clients can also view/set permissions (select the file/directory then ⌘-i in Transmit or ‘right-click > File permissions’ in FileZilla). Set the appopriate permissions for the following files/directories (list courtesy Jack Danger Canty):

    File/Directory Permission
    {app}_rails/public 755
    {app}_rails/public/.htaccess 755
    {app}_rails/public/dispatch.fcgi 755
    {app}_rails/public/dispatch.cgi 755
    {app}_rails/log 775
    {app}_rails/tmp 775
  7. Handling rails gems version issues.If you're app still isn't working, check the error logs in cpanel. It may be that the version of the rails version you built against isn't available on the shared host. In this case,you'll need to either freeze the gems into your app locally (then upload the vendor/gems directory) or comment out the following line in environment.rb which refers to their specific version (risky).

    rake rails:freeze:gems

That should be it!

Hue City

A project that I’ve been working on forever has, among other features, a ‘search images by colour‘ feature. This is one that I’d been excited about for a long time and had tackled on several occasions but it had never quite come together. Until now.

My previous attempts had all involved reducing each image to a weighted list of ‘popular’ colours. The search UI let the user pick from the web (216) palette, with only images possessing a healthy portion of that colour being returned in the results. At one time I even allowed users to select a secondary colour along with RGB/HSV/CMYK (synchronised) sliders, but that was as complicated to use as it sounds, so I let it go. The problem was that this ‘popular colours’ approach ignored the fundamental truth that people just don’t search for drab browns, faded yellowy-greens or most of the other colours which make up these palettes. From a colour-space perspective, when people say ‘colour’, they mean ‘hue‘.

So earlier this week (on the train) I set about re-writing the whole colour search feature to actually be a hue-based search feature (the word colour remains because ‘hue search’ just doesn’t light up anyone’s eyes, not even mine). After a few train trips and most of Saturday I had the hue-parsing algorithm implemented and had written a console app which re-indexed all the existing images in the library. I also updated the relevant portions of the batch uploading tool and the admin web site to be hue-aware. Things were going well, but I had only a vague idea of how the actual search UI would work.

Initially I thought another slider would be the best approach, but decided it was overkill after looking around at existing html slider implementations (yahoo UI library as a nice slider, but I didn’t want the added dependency for what is essentially a minor feature). Eventually I set about rolling my own control for the job. I think the hue picker widget and the hue search feature as a whole turned out really well, and will continue to improve as the number of images in the library grows.

Now I’m thinking of packaging up the  Hue Picker for free distribution. You know, just for kicks.

QuintonMarais.com launched tonight

After several weeks’ work, quintonmarais.com launched this evening.

Hi-lights include

  1. .Net 1.1 to .Net 2.0 upgrade (long live the MasterPage)
  2. Amazon S3 for all image storage (dramatically reduced bandwidth and hosting costs)
  3. Google map-based location search.
  4. Creation of “Pusher”, a fire-and-forget image watermarking, batch uploading and data-entry smart client app (to create the 12+ thumbnail and preview versions of each image).
  5. Creation of “S3 Viewer”, a smart client app for viewing/maintaining the thousands of S3 items and buckets which support the site.
  6. ‘New Images’ atom feed (surprisingly easy to get going).
  7. SQL Server 2000 to SQL Server 2005 upgrade (Common Table Expressions are the goods).
  8. GeoRSS feed to (hopefully) ensure photographs appear in search results as geo-tagged objects.
  9. Tag-clouds for hierarchical image categories.
  10. Colour search (still beta, currently rewriting the search algorithm to favour colour coverage rather than colour fidelity).
  11. Cleaner UI design.
  12. Xhtml transitional doc type with vastly improved markup validity (flash still causing some issues here). Some tables remain, to be slowly phased out when I have time.
  13. Flash 9 homepage scroller (flash cs3 didn’t play nice with my old flash 6 swfs).
  14. Self installed and configured SSL.
  15. New hosting provider (discountasp.net, very impressed so far)

All told the upgrade took about 3 months’ worth of train trips, nights and weekends (around my day job). It’s meant an overall reduction in sleep, but ultimately worth it. Now I have just to force myself to stop tinkering with it.