In this article, you’ll learn about Heroku and how to deploy your web application to it.
If you’ve never heard of it, Heroku is a managed server platform for quickly deploying web applications. It automatically provisions server resources for you, and deployment is as easy as a git push to your app’s repository on Heroku. Best of all, you can deploy your app for free (provided it doesn’t get too much traffic), which makes getting started free and easy.
If you have a lot of traffic, Heroku can get a bit pricy; each node (or dyno
, as they call them) will cost you $25 or more per month, and adding features like databases will increase that a bit. That said, it’s a heck of a lot cheaper than hiring a devops team to deliver the stability and ease-of-use that Heroku offers.
Before We Start
If you want to follow along from here, you’ll need to make sure you have a few things handy.
- Go download and install the Heroku Toolbelt. This is the command-line utility that we’ll be using to configure the project.
- Make sure your project is using Git. You should know what git is, but if you don’t, here’s some light reading. If you don’t have a project, just make sure
gitis installed.
If you already have something ready to go, skip the next section and go straight to Creating a Heroku Project.
Our Example Project
To assemble this example project, you’re going to need pip (which is good to have handy for any Python development anyhow). We’ll be using a Python project written using Flask, a web microframework for Python, but you can mostly follow along with any project (it should be obvious which parts are language-specific and how to adjust them).
If you have something ready, skip ahead to the next section. If you need a project, set up a project folder as follows (you don’t have to call it myproject
):
/myproject
/templates
index.html
app.py
requirements.txt
And fill them out like so:
app.py:
import os
import flask
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run(port=os.environ.get('PORT', '5000'))
templates/index.html:
<!doctype HTML>
<html>
<head>
<title>My example project</title>
</head>
<body>
<h1>This is my project.</h1>
<!-- feel free to get a bit more creative here -->
<body>
</html>
requirements.txt
Flask==0.10.1
then run:
pip install -r requirements.txt
After that, make sure everything is working by running python app.py and navigating to http://localhost:5000/. If all went well, you should see index.html rendered there.
Creating a Heroku project
Here comes the easiest thing in the world. Open a terminal, cd to your project directory, and run the following commands (skip the git one if you already have git in your project):
$ git init
$ heroku create
Creating app... done, stack is cedar-14
http://ift.tt/1XJlgq4 | http://ift.tt/1rlV7Ui
With this command, Heroku:
- generated a name for us (we could have chosen one by running
heroku create myprojectinstead) - assigned us a url and a git repository
- initialized the
herokuremote repository in git for us.
We’re just about ready to deploy, but it’s good to know what’s about to happen first. Let’s learn a bit more about how Heroku can know what to do with your code — and how to make sure it’s satisfied.
Continue reading %Deploying to Heroku: An introduction%
by Adam Bard via SitePoint
No comments:
Post a Comment