I recently built a rails app that needed more than the free level provided at Heroku so I decided to provision my own VPS and I thought I’d give the steps I followed to help someone else doing the same thing.
I’m making the following assumptions: You have a github account and know how to generate SSH keys. You can work with a command line editor like vim/emacs/pico Your using a debian based linux distro on your VPS, I’m using Ubuntu 12.04 LTS If you don’t have a VPS, most people recommend Linode since it beats both EC2 and Digital Ocean when compared head-to-head but since I was being thrifty and I didn’t need that level of performance, I went with Digital Ocean’s cheapest plan.
Assuming you have your server provisioned, you need to ssh into it, add a user (you don’t need to create a user with the name Sean, but you can) and give the user sudo access.
Setting up the server
Edit the file so that it looks something like this:
You should now exit your server and copy your public key to your server, you can copy and paste or just use the scp command. For me the command was:
Now ssh back into your server using your public key. If this doesn’t work, ssh back in using your password you set when you created the account and do the following:
Set the following entries to yes in the sshd_config file:
After making the change, reload the the ssh configuration file:
Note: if the service command failed run this command:
Exit the server and you should now be able to log in without using a password. Next disable password logins by again editing the sshd_config file and changing the following values to no:
Reload the ssh config as before. You should also at this point set up ssh keys on your server and add those keys to your github profile, assuming you are going to be deploying from github. At the top of this article I posted a link to github with their directions on creating ssh keys.
Installing Ruby We should make sure our system is up to date before installing anything:
Now we can start with the actual ruby and rails installation. Before we can install rvm we need to install all the rvm dependencies.
Then we install RVM
Then reload rvm.
Then install ruby.
Now install passenger.
If you want you can create a gemset for your application. If you are not planning on using your VPS for anything other than hosting this app this is probably not needed. Here is how you do it (my app is called pompeii.)
Webserver - Nginx Now to install Nginx we first need to install the curl development headers with SSL support:
Then install Nginx:
Now we need to configure Nginx for our application vim /opt/nginx/conf/nginx.conf and in the http section add the following:
You don’t strictly need to add the line client_max_body_size 20M; unless you plan on transferring large files to/from your app. Next we need to get the linode init script and make nginx start when the server starts with the following:
Then just restart Nginx and you are good to go:
Database - Postgres Now on to installing our database. I used Postgresql that you install with the following command:
We need to give our application a role and give it the ability to create databases/tables and login.
Now just create your database:
Now we just need to set up capistrano and we are good to go. Just add the following gems to your gemfile:
Then enter the following command from your command line on your development machine: capify . Your Capfile should look like this:
Your deploy.rb should look something like this:
Assuming you want capistrano to setup and create the directories for you:
Now test to make sure there are no errors with:
Use cap deploy:cold the first time you deploy, after that `cap deploy’ is going to be all you need.
Finally migrate your database and you should be good to go.
This is only a fraction of my deploy script, I have tasks in there to restart solr for search and to precompile my assets to speed up my deployment but this should be enough to get you going. Hopefully your site is now live and on the web!