Locked lesson.
About this lesson
Exercise files
Download this lesson’s related exercise files.
Hosting Your App On Heroku.docx59.7 KB Hosting Your App On Heroku - Solution.docx
59.5 KB
Quick reference
Hosting Your App On Heroku
We'll host our app on Heroku; which is a production quality web hosting service that has a free tier.
When to use
C9 allows us to run a simple web server that will give us a public URL and website, but it doesn't have the horsepower to allow many people to view that web site at once. So we need real webhosting to host our app professionally. Heroku is the answer.
Instructions
Sign up for Heroku for free at Heroku.com
At your C9 terminal, issue this command to make sure the Heroku toolbelt is installed:
- heroku --version
To Connect your C9 account with Your Heroku account, issue this command from your terminal:
- heroku login
Enter your heroku email and password when prompted.
Next, add your SSH keys to heroku by issuing this command from the C9 terminal:
- heroku keys:add
Next, create an app on Heroku by issuing this command from the C9 terminal:
- heroku create
Be sure to write down the URL that the terminal spits out at you.
Next, we need to make some changes to our Gemfile to move the sqlite3 database into development only, and add Postgres database to production.
Open the Gemfile and remove the line:
- gem 'sqlite3'
and add it to the Gemfile Section:
group :development, :test do
gem 'sqlite3'
gem 'byebug'
end
Next add this code to the Gemfile to add the Postgres database to our production environment (heroku):
group :production do
gem 'pg'
end
Finally, from the terminal, type in this command:
- bundle install --without production
Finally, we need to push all our code to heroku. From the terminal, type these commands:
- git add .
- git commit -am 'added PG'
- git push
- git push heroku master
Hints & tips
- Heroku is a production quality web host
- Heroku offers a free tier that we'll use in this course
- We push code to Heroku in much the same way that we push code to github, from the terminal
- Heroku requires the Postgres database, so we need to make a small change to our Gemfile
- 00:04 We're almost ready to dive in and start building stuff, but before we do,
- 00:08 we need to talk about one more thing, and that's web hosting.
- 00:11 I mentioned earlier that right off the bat
- 00:14 C9 gives us an actual website with an actual live URL.
- 00:18 The thing about this is, it's not a production level quality website, and
- 00:22 that means basically a couple of people could come to this URL, and
- 00:26 everything will be fine, but if more than one, or two, or
- 00:29 three people hit this thing at the same time, the whole thing's gonna crash.
- 00:33 The purpose of this is just to give us a development website that we can make
- 00:38 changes and see in real-time what we're doing.
- 00:40 When we get our app finished, we're gonna wanna push to an actual professional
- 00:45 webhosting service of some sort.
- 00:47 Now, there's lots of options out there, but one of the most popular, and
- 00:50 definitely one of the easiest,
- 00:52 is something called Heroku, and you may have heard of this before.
- 00:55 Heroku is set up to host all kinds of things, but
- 00:58 primarily it's a Rails hosting service, and this is built on top of Amazon AWS.
- 01:03 The thing about this is it's completely free at a basic level.
- 01:06 The basic setup is totally free.
- 01:09 So if we go to heroku.com and click on Pricing, you can see what we want is this
- 01:14 free account here, and it's sort of like the C9 development environment.
- 01:19 It'll give you a hosting account that maybe a dozen or
- 01:22 so people can hit your website before it crashes.
- 01:24 Well, maybe a little bit more than that, but it will start to be very slow.
- 01:28 If you wanna actually ramp up your website, and make it faster,
- 01:31 and allow more people hit it at the same time, you just increase your dynos, and
- 01:36 think of dynos as horsepower, you're just giving it more horsepower.
- 01:40 So at one dyno is $25 a month,
- 01:42 and that's probably gonna be enough to get you going on for quite a bit.
- 01:45 Two dynos, $50 a month, $100 a month.
- 01:48 Unless you're getting thousands of people hitting your website all at once
- 01:52 you're not gonna need to increase this probably ever past one dyno or
- 01:56 so, but just for our purposes zero.
- 01:58 It's completely free.
- 01:59 It's gonna be more than enough, and so
- 02:00 that's what we're gonna be using in this course.
- 02:03 So go to Heroku, sign up, fill out the form, click the email they send you to
- 02:07 verify your account, and when you're ready, login, and
- 02:12 you'll see I already have a whole bunch of apps running here, and
- 02:16 we don't actually need to do anything here right now.
- 02:18 What we need to do is come back here, and we're going to go through a similar
- 02:21 process like we went through with GitHub, and you'll see just a minute here, but
- 02:25 before we do that, we need to setup a few things.
- 02:28 Come down here to terminal and type in haroku --version, and that's just
- 02:34 to make sure that this, we have the Heroku toolbelt running on our system, and we do.
- 02:40 So we are all good to go there.
- 02:42 So what we need to do is login to heroku.
- 02:46 Type in heroku login and it's gonna ask you for
- 02:51 the same email address that you just signed up for
- 02:55 Heroku with, and your password.
- 02:59 Notice that it doesn't actually type it in, it'll be hidden.
- 03:02 So see now we're logged in, and we're good to go.
- 03:04 Now we also need to add our SSH keys, just like we did with GitHub, but
- 03:09 this time we don't have to actually copy and paste them, all we need to do
- 03:14 is type in, heroic keys:add, and that'll do it for us automatically.
- 03:19 Would you like to do this?
- 03:20 Yes, boom, done.
- 03:22 Now we need to create an app on Heroku, and we do that by typing in heroku create,
- 03:28 and here is our URL, and later on I'll show you how to change this.
- 03:33 But, for now, if we copy this, and
- 03:36 open it up another web browser, it says here we are.
- 03:40 We're good to go.
- 03:41 So, we're almost done.
- 03:43 We've got everything set up.
- 03:45 We just need to make a quick change to our gem file, and
- 03:48 we talked briefly earlier about databases,
- 03:50 and our development environment database is something called SQL3.
- 03:54 But, Heroku doesn't like that, Heroku wants us to use Postgres, so
- 03:58 we need to delete this, actually, I'm gonna copy this and
- 04:02 I'm gonna come down here to development, and paste that in.
- 04:05 That's saying go ahead and keep using SQL Lite3 in the development environment,
- 04:11 but for our production environment, we're gonna use something else, and
- 04:16 we do that by typing in group :production do.
- 04:20 End, and inside of here, we wanna put in the gem 'pg',
- 04:26 and that stands for Postgres.
- 04:29 So save that, come down here, and type in the command bundle,
- 04:34 B-U-N-D-L-E, install --without production,
- 04:40 and anytime we add a new gem, we have to type this bundle install command.
- 04:46 Usally, we'll just type bundle install without the -- without production,
- 04:51 but the first time we need to do it without production, so
- 04:55 it doesn't add this pg file.
- 04:57 And there we go.
- 04:58 All of this stuff has been added.
- 05:00 The Postgres database has been added to our production, so
- 05:02 we can clear this screen.
- 05:03 Now the last thing to do is push this code to Heroku, and
- 05:07 first let's save this to Git.
- 05:09 So we type git add ., git commit -am 'added pg',
- 05:15 git push, to push it to GitHub, and
- 05:19 now we type in git push heroku master, and
- 05:24 that's the command to push our stuff to Heroku.
- 05:30 We'll use that command everytime we want to push our stuff up to Heroku.
- 05:33 Now this can take a couple of minutes up to several minutes, or
- 05:37 it can be 20 seconds or less, it just sort of depends.
- 05:40 The first time you do it, it might take a little bit longer, usually after that,
- 05:43 it goes a little quicker.
- 05:44 Okay, and now we're done.
- 05:46 So we can come back to our Heroku site, click reload, and the page doesn't exist,
- 05:50 'cause we haven't actually created any page as yet, but now we're all set up.
- 05:54 We've got Heroku going, we've got a production level quality website,
- 05:57 we've got this URL that we can use.
- 05:59 Now we can change this URL later, I'll show you how to do that,
- 06:02 but right now we've got everything set up, and we're ready to go, and
- 06:05 in the next module we'll start building out our app.
Lesson notes are only available for subscribers.