Python Deploy Your Flask App – Heroku

Till now in our previous blog posts we have developed our flask application, lets now see how we can deploy it to a server.

We can first try to deploy our app on heroku. Heroku provides free cloud hosting which can be used for small app’s for free. To start with heroku create an account and install heroku on your system. Follow this steps to install heroku

After heroku is install do

$ heroku login

in your terminal and login with your heroku account. Next go to your project root folder and do

$ heroku create 

this should create a new project on heroku

open heroku dashboard and confirm you should see the project there

Ok, so far so good.

Next, first confirm if your project is working locally using “flask run”

Now to deploy a flask app on production, we cannot use “flask run”

We need a production grade server which can handle lot of users, has logging etc. We will use for the same.

Install it using

$ pip install gunicorn   //or pip3

Next, run it via

$ gunicorn "app:create_app()"

Now the app run’s on port 8000

This is the most tricky part, how to exactly run “gunicorn” for a flask app. Since custom flask app can follow different app structure

You would need to google or follow this link to understand better how exactly gunicorn run’s an app.

Basically “gunicorn” is a production server which can server python apps to the web.

Next, for heroku we need to create a “Procfile”

basically what “Procfile” does it tell heroku which process should be exposed to web i.e port 80/443 (https) so that it will open on browser.

So in project root create a file name “Procfile” and put in

web gunicorn “app:create_app()”

Next commit your code to git

$ git add -A && git commit -m "heroku"
$ git push heroku master

This should ideally push all code to heroku and start the build process.

One this very important is that, there should be a requirements.txt file already present. This way heroku will know this is a python project and which all modules to install automatically. So if you don’t have a requirements file create using

pip freeze > requirements.txt

and again push to heroku. You should see an output like this

So the build was successful, and also to verify open the project in heroku dashboard and you should be able to see the latest activity

Click on open app, and your app is ready.

In my case, when i opened the app i get an error

On your terminal you can view the logs as

The error which i see is

gunicorn not found!

The reason for above is my requirements.txt didn’t have “gunicorn” so i added it. Did git commit and git push heroku master to fix the issue.

So basically we can use heroku logs -tail to see all errors coming in terminal.

Next, in my case i am using mongodb in my flask app. Since there is no mongodb installed on heroku it will give a connection error and not be able to connect to it.

We can use a cloud mongodb like mongo atlas to solve our problem

So create an account in mongo atlas, and get the connection parameters. The problem with mongo atlas, is that it requires a fixed static ip address for incoming connection requests. Fixed IP is not possible without paid modules via heroku so this option won’t work out.

Now cloud atlas has option to enable access from anywhere which is much easier now

Here is screenshot for the same

There are many other free cloud mongo setups which don’t require a fixed IP. We can use any of those to setup our application.

So in conclusion we saw above how to deploy python on heroku, but need a paid account to setup mongodb connection.

If we have our server (vps or dedicated server) with nginx setup we can simple do the following

 server {
    listen 80;
    access_log  /var/log/nginx/example.log;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;