Tag:

Deploying Ruby on Rails 3.1 on Ubuntu

Posted: December 27th, 2011     Reminders to myself   Web Development

This serves as a reminder for me on getting Ruby on Rails 3.1.3 running on Ubuntu (tested on 10.01 LTS).

Install git using apt-get:

sudo apt-get install git-svn

Install build dependencies:

sudo apt-get install gcc g++ build-essential libssl-dev libreadline5-dev zlib1g-dev linux-headers-generic libsqlite3-dev

Install rvm (Ruby Version Manager)

bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Put RVM into .bash_rc file so that you can run it easily:

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_rc

Reload bash settings:

source ~/.bash_rc

That should install rvm. Now you can use it to install Ruby:

rvm install 1.9.2

Make sure to switch into the correct version of Ruby:

rvm use 1.9.2

Install Rails:

gem install rails

This takes a while….

Install Apache:

sudo apt-get install apache2

Install Phusion Passenger:

gem install passenger
passenger-install-apache2-module

Follow the instructions on screen.

Setup Apache virtual hosting the way you normally do. Make the DocumentRoot the rails public directory.

<VirtualHost *:80>
        ServerName ec2-107-21-154-173.compute-1.amazonaws.com
        RailsEnv production
        DocumentRoot /home/ubuntu/depot/public
        <Directory /home/ubuntu/depot/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
 
</VirtualHost>

When deploying an app make sure to run bundle install to make sure all the dependencies are installed. This includes mysql, etc.

Rails 3.1 needs a Javascript runtime.

Add this to your gemfile:

gem 'execjs'
gem 'therubyracer'

Run:

bundle install

Make sure to setup your DB using:

rake db:setup RAILS_ENV="production"

Restart apache:

sudo service apache2 restart

Load your browser and visit the site. Hopefully it works, if it doesn’t, check logs/production.log and trace from there.

Asset Pipeline Gotchas

The new asset pipeline in Rails may cause your app to fail.

When you deploy an app, make sure that you precompile the assets using:

bundle exec rake assets:precompile

If you are including css files that are not in your application.css manifest, you need to manually include them in your config/environments/production.rb:

config.assets.precompile += ['960.css']

Note: After recompiling assets you will need to restart your rails app. Do this either by restarting apache or creating/modifying “tmp/restart.txt” in your rails app. Passenger will look at the timestamp for restart.txt and restart rails accordingly.

Rcconf – Tool to edit Linux boot services

Posted: November 15th, 2011     Reminders to myself   Web Development

I know I’ll forget this because I only have to edit runlevel services once in a blue moon. Use a tool called rcconf. You may have to install it using apt-get.

Video Textures SIGGRAPH Paper Implementation

Posted: November 14th, 2011     Computer Graphics

I’m doing another course at Concordia to keep my brain active. This term, I was asked to implement the Video Textures SIGGRAPH paper by Schodl et al (2000). Schodl’s paper introduces a new medium called a “video texture” (not to be mistaken with a texture map that uses an video file as its source). Basically the paper involves taking arbitrary input video footage and turning it into infinitely looping videos. I implemented the video texture synthesis in C++ on Mac OS X, utilizing the OpenCV framework. I also integrated the video textures in an OpenGL application.

Read my paper here

YouTube Preview Image

VBulletin single sign on

Posted: November 1st, 2011     Reminders to myself   Web Development

I figure I would write this because I searched around for a suitable solution and it’s quite frustrating to find. VBulletin (the company) runs a very tight community and generally don’t help people who want to hack or extend the software. In my case, I am integrating VBulletin into a client’s custom application and trying to get single sign on to work. Do a search and you’ll find VBulletin telling people who have asked the same question “we won’t help you. Go to vbulletin.org.” When you do get to vbulletin.org, you can’t see any code because you’re not a licensed user. Very frustrating.

So…here it is.

VBulletin stores two cookie vars that can be used for single sign on:

  • bb_userid – look in the ‘user’ table, it is the field ‘userid’
  • bb_password – md5(user.password . COOKIE_SALT)

When a user visits the forum, it can login based on the above.

Once you authenticate that the user can login in your app, read the user row from the vbulletin database. Take the password (which is already hashed) and concatenate the COOKIE_SALT constant to it. You can find COOKIE_SALT defined in /includes/functions.php. You then md5 hash this string and set the cookie as bb_password.

When setting your cookie, remember to set the domain to one that vbulletin can read. If you’re using different subdomains (e.g.   www.leonardteo.com for the main app and forums.leonardteo.com for the forums) simply set the domain to “.leonardteo.com”. In VBulletin, you’ll want to go into the admincp and change the cookie domain to the same.

By setting these two cookies, you should be able to login with your app and it will automatically login to VBulletin as well.

Logout
Make sure you set the logout routine as well. Your logout routine needs to clear three cookies:

  • bb_userid
  • bb_password
  • bb_sessionhash

Enjoy.

My rant about storing crucial web app settings in a database

Posted: November 1st, 2011     Rambling   Web Development

I’ve been coding PHP for a long time. Since 1999 actually. That means that for most of my adult life, I’ve been working with this programming language in the web-o-sphere in general. I’ve gotten to work on many applications and continue to do so. I’ve also gotten to use many off-the-shelf apps including WordPress, Expression Engine and VBulletin. Today, I’d like to rant about two conventions that these all have in common, and why we need to be rid of them.

1. Storing paths and URLs in the database

This happens in WordPress, Expression Engine and VBulletin. Basically, the idea is that all settings must be stored in a database table because…well…it’s supposed to be easier. The user updates a setting in the admin control panel, and it stores those settings in a database. The problem with storing paths in a database like this is that it makes the app less portable. For example, say you want to move an installation of any of the above-said apps to a different domain, it’s not as simple as tarballing the entire application, running mysqldump and restoring on the other side. You’d think that changing the config file would get it to work but this is only half the battle. WordPress and Vbulletin, for example, stores the actual URL of the application in the database, so when it does any redirect (e.g. when you login to the admin control panel), it will redirect to your OLD site.

WordPress does something else that I consider evil. When you upload images, it stores the full web path in the meta data. This is also a problem when you move to another domain because all your images are now pointing to the old domain. I’ve had to write scripts that change all metadata values and wordpress settings when porting from one domain to another. It’s annoying.

Why is this an issue though?

Development. For most people, the above shouldn’t matter because you have a blog that you installed on a webserver; you update it and all’s well. You never run a local copy of the site for development. As a developer though, I have to usually get local and staging copies of the website running. This means that I’m constantly having to do all kinds of shenanigans to get these instances running on different servers.

I used to think that this was simply a bad design limited to a couple of web apps but it seems like a whole plethora of web apps suffer from this. While working for Gnomon, we bought into Expression Engine, thinking that this would be the silver bullet that kills WordPress. Nada. EE is just as BADLY designed! (not just for the reasons above, but many others)

2. Serializing Critical Settings

PHP has a neat function called serialize(). It takes data and turns it into a string. The data can be read back in easily. This is great for many things. For example, if you have user accounts and each user has their own settings, it’s probably useful to just serialize their settings data and save it as a single string in the database.

Crucial site settings though, should NEVER be serialized in a database. I would go as far to say that as a rule of thumb, if the setting is crucial enough to make or break an installation of an app, it should be in a config file. This relates to my first rant as well. Many settings can be put in the database, but some settings (like paths), should just be in flat config files that can be quickly changed when moving the app.

I was absolutely shocked when using Expression Engine that crucial app-breaking settings like upload paths were not only stored in the database but SERIALIZED. I ended up writing a script that can move an entire installation of EE from one server to another in a single command line, and it’s really long because it had to connect to the database, unserialize the data, make modifications, re-serialize the data, write back to database.

It shouldn’t be this hard.

My recommendation

My recommendation is to follow this rule of thumb: if the setting is crucial enough to make or break an installation of an app, it should be in a config file.

Absolute paths like upload paths, etc. should be stored in a config file.

If the application needs a site-wide URL, this should be in a config file.

Individual asset links in the database should link to the relative location (i.e. minus the hostname.) e.g. not http://leonardteo.com/image1.jpg, but /image1.jpg instead. Even better, just store ‘image1.jpg’ and use a configuration setting to determine what the path to that image is.

The litmus test should be: Tarball an application, dump the database. Copy everything to a different computer/hostname. Restore everything. If you can’t get the site up and running again without accessing and changing values in the database, fail.

A practical benefit – fast deployment

On a closing note, one of the benefits of running critical site settings in config files, is that you can switch configurations based on the hostname.

E.g. in your config file:

switch ($_SERVER['SERVER_NAME']){
    case 'cgacontest.local':
        define('SITE_MODE', "LOCAL");
        break;
    case 'cgacontest.test':
        define('SITE_MODE', "TEST");
        break;
    case '3dsmaxdesign.cgarchitect.com':
        define('SITE_MODE', "LIVE");
        break;
}

With the above code, we can switch between site modes and run different configurations automatically based on the hostname. This means that when it comes to deployment, I simply push any changes to the server without having to change configuration.

 

Launched Ballistiq Website

Posted: September 29th, 2011     Rambling

Very happy to say that we finally launched our Ballistiq website!

 

 

Hello WebGL!

Posted: September 29th, 2011     Computer Graphics

I’m doing another course at Concordia University in the little spare time that I have to keep my skills sharp. This time, I’m doing a course on animation. I decided to try my hand at doing everything in WebGL instead of C++ GLUT. I managed to get my OBJ reader working in PHP which writes out JSON representations of the mesh. So here’s my Hello WebGL app!

kinectfusion = WIN

Posted: August 13th, 2011     Computer Graphics

This is awesome new tech:

YouTube Preview Image

Flush DNS Cache in OS X

Posted: July 26th, 2011     Reminders to myself

Reminder to self….

sudo dscacheutil -flushcache

Deleting files in Unix in 1 command

Posted: July 15th, 2011     Reminders to myself   Web Development

I find myself constantly doing this with files that some OS’s and programs spew all over a directory structure. E.g. .DS_Store, .svn, etc.

find . -name "filename" -exec rm -rf {} \;
 
Leonard Teo
CEO, Ballistiq
Montreal, Canada