It's time to deploy your app! H0Lee S#!T...
-- Khurram Virani, 2014
Localhost is great and all, but at some point you might want to deploy other app to the wild (the internets) for others to be able to use it.
When an app is live, it’s often referred to as the "production" instance/server of your app. Your your local server (on localhost
) which only you can use is referred to the "development" instance.
There are many options on how and where to deploy your web app. The simplest approach is to use a Platform as a Service (PaaS). A PaaS abstracts away many of the complexities associated with setting up, configuring and deploying to a machine in the cloud. Heroku is the most popular platform of this kind.
Heroku uses PostgreSQL as the RDBMS (Relational Database Management System) vendor of choice. So in order to have your app running on Heroku, it must use a Postgres database (instead of SQLite3 for example), which is what your app is likely using for development. And we can't (easily) use MySQL with Heroku either (thankfully)
The objective of this exercise is to get comfortable with Heroku while learning the basics of deployment with services like Heroku.
In doing so, you also get your first ruby web app up on a URL that you can send to your friends, family and even your frenemies.
Unless you've already done so (which you should have since you have previously used Heroku Postgres): https://id.heroku.com/signup
Download the Heroku Toolbelt for Ubuntu/Debian by running the given unix command in your vagrant machine.
Note: Follow only the heroku login step under the Getting Started section on this page. Say yes (Y) if prompted to generate an SSH key to use with your heroku account.
https://toolbelt.heroku.com/debian
Update the rubygems-bundler
gem to avoid a bug later on. In your vagrant box. Note: This is a rather specific thing so don’t worry about remembering this step at all.
From anywhere in your vagrant box, run the following command:
rvm all do gem update rubygems-bundler
Add the pg
gem to your Gemfile, but only for Heroku (production) not for localhost (development):
group :production do
gem 'pg'
end
Create a Procfile in the root directory of your app. This will help heroku understand how the app should be run from the command line. Don't worry about this too much for now.
Add the following (single) line to this new Procfile
:
web: bundle exec puma -p $PORT -e $RACK_ENV -t 0:5
Update the database.rb
and environment.rb
to prepare for production
Open the database.rb
file in the config
folder and change the set :database
stuff to look more like this:
if development?
set :database, {
adapter: "sqlite3",
database: "db/db.sqlite3"
}
else
set :database, ENV['DATABASE_URL']
end
Open the environment.rb
and change the require 'pry'
:
require 'pry' if development?
Probably a good point to commit and push this stuff to your github (it won't affect your project negatively)
Use the "Create a new app" link for your Heroku Dashboard http://note.io/1uJaqQf. Call it whatever you like. It will be the name used for the subdomain in the app's URL. You may need to provision a Heroku Postgres database. On your app dashboard, add a postgres Add-On.
Take a look at the settings page of your heroku app: and copy the Git URL ( shown here: http://note.io/1y2QT4w)
git remote add heroku <the git:// url from the app's settings page>
git push heroku master
If you get a Permission denied (publickey)
error, you may need to notify Heroku of your SSH keys using the command:
heroku keys:add
We need to migrate our Postgres database on heroku (it's empty at the moment). Run this from the command line (from within the app folder through vagrant of course):
heroku run rake db:migrate
Since the database has changed, we need to restart our app on heroku:
heroku restart
heroku info
is a command that will show you the web URL. Run that command and then copy/paste the "Web URL" into your browser.
Example: http://sinatra-skeleton-lhl.herokuapp.com/
Does it work? If not, don't sweat it and don't try to debug it too much. We'll solve it together if need be.
Simply push your code to heroku using the git command (Step 10) and run any new migrations (Steps 11 and 12) when you have new commits to push to heroku
- Note: Switching to a postgres db means your your local data will stay behind in the old SQLite3 database. So don’t be surprised if your data is missing.
- The
heroku open
command won’t work from the vagrant machine. You will need to manually open the URL in your browser. This is why you have to runheroku info
and copy/paste the Web URL from the output into your browser.