How to deploy application to online server using Heroku
By: Lukasz Muzyka, On:
This tutorial assumes you have already completed:
- Install Ruby on Rails
- Create Ruby on Rails application
- Create Static Pages - without this deploy will not work
- Install Git
- Create Remote Git Repository - optional but recommended
Our simple application is now working. It's now time to make it work on the internet so that other people can access it. Back in the day, this was a difficult process. Today, thanks to companies like Heroku it has become very, very simple. It takes just a couple simple steps to deploy the app to the Heroku platform.
Our application is prepared to work in 3 different configurations: Test, Development and Production. The test environment is a special configuration that runs special scripts. These scripts checks to ensure the application is working in the right order. Development is what we can access when we run
$rails server and open a browser with the address http://localhost:3000. Production is a special configuration for the server. All environments are using different databases and we can make them use different email addresses, etc.
For our production environment, we will be using the heroku.com platform. Not only can we host our application for free there (until we generate reasonable traffic) but also everything is ready to go. After the initial setup, deploying an application will be as easy as typing a simple command in the Terminal. Let's get going:
Step 1: Create an Account on Heroku
Head over to www.heroku.com and create an account there.
After you confirm your account head back to https://devcenter.heroku.com/articles/quickstart to download the Heroku toolbelt.
Step 2: Heroku Login
After you install it, open terminal and type:
Enter your credentials:
Enter your Heroku credentials. Email: firstname.lastname@example.org Password: Could not find an existing public key. Would you like to generate one? [Yn] Generating new SSH public key. Uploading ssh public key /Users/adam/.ssh/id_rsa.pub
Step 3: Configure the Database
First, we need to make sure we use the proper type of database for our production environment. Rails, by default, gave us an SQLite3 database, which is okay for our development environment. Heroku however wants us to use PostgreSQL database. Let's make sure our
Gemfile has the necessary gems.
# Use sqlite3 as the database for Active Record gem 'sqlite3'
group :development, :test do gem 'sqlite3' end group :production do gem 'pg' end
Because the way heroku uses Gemfile.lock file to build our application we will need to delete that file now, and rebuild it with command "bundle install". Otherwise, heroku will reject our push. So go ahead and delete
Gemfile.lock. It's next to the Gemfile.
Step 4: Install dependencies
Now, we can install all the new dependencies. In your terminal, type:
$ bundle install
Step 5: Change Configuration File
One more change we need to make is to change the configuration file for our database. Head over to
# SQLite version 3.x # gem install sqlite3 # # Ensure the SQLite 3 gem is defined in your Gemfile # gem 'sqlite3' # default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default database: db/test.sqlite3 production: adapter: postgresql host: localhost pool: 5 timeout: 5000 database: demo_production
Note that we have changed the settings for the production database.
Step 6: Changes to Git Repository
We can now commit the changes to the git repository:
$ git add . $ git commit -m "changed database type for production to postgres"
Step 7: Deploy application to Heroku
At this point, we are ready to deploy our application to Heroku. First, we have to create an application:
$ heroku create pick-unique-name-of-your-application
Creating peoplecancode-demo... done, stack is cedar http://peoplecancode-demo.herokuapp.com/ | email@example.com:peoplecancode-demo.git
If the creation is successful, you will be able to deploy the application to Heroku:
$ git push heroku master
Fetching repository, done. Counting objects: 9, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (5/5), 970 bytes | 0 bytes/s, done. Total 5 (delta 4), reused 0 (delta 0) -----> Ruby app detected -----> Compiling Ruby/Rails -----> Using Ruby version: ruby-2.0.0 -----> Installing dependencies using 1.5.2 Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment Using rake (10.3.1) Using json (1.8.1) Using i18n (0.6.9) Using thread_safe (0.3.3) Using builder (3.2.2) Using erubis (2.7.0) Using minitest (5.3.3) Using rack (1.5.2) Using polyglot (0.3.4) Using mime-types (1.25.1) Using arel (188.8.131.5240414130214) Using bcrypt (3.1.7) Using coffee-script-source (1.7.0) Using execjs (2.0.2) Using thor (0.19.1) Using orm_adapter (0.5.0) Using hike (1.2.3) Using multi_json (1.9.3) Using pg (0.17.1) Using bundler (1.5.2) Using tilt (1.4.1) Using sass (3.2.19) Using rdoc (4.1.1) Using tzinfo (1.1.0) Using treetop (1.4.15) Using rack-test (0.6.2) Using warden (1.2.3) Using uglifier (2.5.0) Using sprockets (2.11.0) Using coffee-script (2.2.0) Using sdoc (0.4.0) Using activesupport (4.1.0) Using mail (2.5.4) Using actionview (4.1.0) Using activemodel (4.1.0) Using jbuilder (2.0.6) Using activerecord (4.1.0) Using actionpack (4.1.0) Using actionmailer (4.1.0) Using railties (4.1.0) Using sprockets-rails (2.1.3) Using coffee-rails (4.0.1) Using devise (3.2.4) Using rails (4.1.0) Using jquery-rails (3.1.0) Using sass-rails (4.0.3) Using turbolinks (2.2.2) Your bundle is complete! Gems in the groups development and test were not installed. It was installed into ./vendor/bundle Bundle completed (0.58s) Cleaning up the bundler cache. -----> Preparing app for Rails asset pipeline Running: rake assets:precompile Asset precompilation completed (2.05s) Cleaning assets Running: rake assets:clean -----> WARNINGS: Include 'rails_12factor' gem to enable all platform features See https://devcenter.heroku.com/articles/rails-integration-gems for more information. You have not declared a Ruby version in your Gemfile. To set your Ruby version add this line to your Gemfile: ruby '2.0.0' # See https://devcenter.heroku.com/articles/ruby-versions for more information. No Profile detected, using the default web server (webrick) https://devcenter.heroku.com/articles/ruby-default-web-server -----> Discovering process types Procfile declares types -> (none) Default types for Ruby -> console, rake, web, worker -----> Compressing... done, 21.5MB -----> Launching... done, v9 http://sc-demo-peoplecancode.herokuapp.com/ deployed to Heroku To firstname.lastname@example.org:sc-demo-peoplecancode.git e8bbe97..6f47150 master -> master
The second to last line shows you your URL. Open it in your browser. Your app is now on the internet. Please share the link in the comments :-)
You can login to comment