AARON VAN BOKHOVEN
I just recently fired up a new Linode VPS, with Ubuntu 10.04 LTS, to stage a Ruby on Rails 3.1 app running Ruby 1.9.2 and I thought I would share my process. Total time took about 1 hour. I’m also going to share my deploy method using Github and Capistrano.
Just for reference, this is what’s going to be installed:
Git RVM Ruby 1.9.2 Ruby on Rails 3.1 sqlite3 Apache 2 Passenger
So the first thing to do is make sure you have your Ubuntu server setup properly with SSH and and all Ubuntu updates. I followed these steps: http://articles.slicehost.com/2010/4/30/ubuntu-lucid-setup-part-1. Also make sure you know your root password as we’ll need to su back into root later on.
Once you have that done, the second article should end after you update. Next install Git.
You can verify the installation by typing:
After git is installed, we need to get the packages for Ruby 1.9.2 to be compatible with Rails 3.1.
Next we need to log into root to install RVM.
Once RVM is installed, time to install Ruby 1.9.2. Make sure you’re still logged in as root(through su). You’ll need to source the RVM script every time you log into root. Normally you wont be doing this often enough to have it auto load, but you can if you want to.
Now you should have access to the command: rvm
Install packages for Ruby 1.9.2 that Rails 3.1 will require.
Now you can install Ruby 1.9.2
Once that’s finished, set Ruby 1.9.2 as the system default.
Then exit root su.
You should be back to your normal user. Check to make sure rvm is loaded by typing ‘rvm’. If nothing happens exit SSH and log back in. At this point, Ruby should be installed:
Next install Bundler.
Before continuing, make sure your project is on a github repository.
To be able to deploy the code from your github repo to this server, you need to have an rsa key that will be added to your github repository deploy key setting.
Once that’s done, copy the contents of the id_rsa.pub key.
The output should look like: ssh-rsa AASasdnl31nADLKAnrqppq…oeqnAE1== firstname.lastname@example.org
Copy that to your github project deploy key:
You can verify that you did it correctly:
Next, Ubuntu dependencies for sqlite3:
The next steps will be on your LOCAL computer. If you already have your own way of deployment you can skip this. Install capistrano, or update to the latest version, and update to the newest bundler. The reason why you need the latest bundler is so that capistrano can work with the new asset pipeline in Rails 3.1. Let me explain how this can work.
You have two options when deploying your app with the new asset pipeline. Compile your assets locally and include the files in your push to your repository, then deploy normally. Second option, push your code as you normally would to your repository, deploy, and have your assets compile on your server instead of locally. The later is the default way capistrano will handle the deploy, and in my opinion is the easiest way.
On your LOCAL computer:
Setup capistrano in your app:
Next, change capistrano settings to allow asset pipeline and RVM support. In your app directory, open up the file: Capfile and change it to look like this:
Next configure the capistrano deploy file by opening: your/app/config/deploy.rb. I’ve provided my deploy.rb as a reference:
Now to setup the directories for capistrano on your server. Still on your LOCAL computer:
Now to deploy JUST the code from your github repository:
Once that finishes, go back to your SERVER. Your app should now be residing in the folder you setup within the capistrano deploy.rb file. Go to that directory. If you used my deploy.rb then it will be in ~/sites/yourappname/.
When you find it, go to /releases/ and go to the only folder in there. It should be a folder named with numbers(datetime stamp of the latest deploy). In that folder you should see your app code from your github repository. The way capistrano works is that every time you deploy your code, it uploads it to a new folder in /releases/ which will be named by a datetime stamp, and then linked to /yourappname/current.
So at this point you should be sitting in your app root directory on your server. The next step is to get your apps gems installed, easy:
After that finishes, time to install node.js. This is required to compile the assets for the asset pipeline in Rails 3.1. If you decided to compile your assets on your local computer, you can skip this. I followed the instruction from: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager for this part.
If you’re using sqlite as the database, you might run into a little problem when deploying. Every time you deploy, your production.db will get removed. This is because your production.db isn’t in your github repository. You have a few options to handle this; have your deploy script copy the production.db from your old release to your latest release, or place your production.db outside of your releases. I use the later option. To do this I created the folder: /sites/yourappname/db/ and in my database.yml config file I changed the path for the production database to: database: /home/yourserverusername/sites/yourappname/db/production.sqlite3.
Time to set the database up in your app. Change directories back to your project.
You can test your app now by trying the console: $ rails c production.
Installing Apache 2 and Passenger:
Follow the instructions that Passenger presents after the installation. It should ask you to place a few lines of code at the bottom of /etc/apache2/apache2.conf.
The last part is to create the virtual host for your app:
Inside that file add. Note: I’m basing this off of my deploy script I provided. If you have your app in a different directory I’m sure you can figure this out. The DocumentRoot and Directory MUST point to the public folder in the /current/ folder of your capistrano deploy directory:
Save the file, disable and enable a few things, then restart apache2.
Hope this helps. It’s quite long and forgive me if I’ve misspelled something or skipped a step. Please let me know if you have any troubles.