Heroku is a cloud application platform it's a new way of building and deploying web apps, Which makes easy to host your application in the cloud with a simple git push command.

Heroku supports several programming languages like(Python, Java, PHP)

Install the Heroku Toolbelt:

The first thing you need to do is to install the Heroku toolbelt. The toolbelt is a command line software which makes it easy to interact with the Heroku service through your command line.

Here you can find out the Heroku toolbelt installation for Debian/Ubuntu, Run this from your terminal:

    wget -O- https://toolbelt.heroku.com/install-ubuntu.sh | sh

In the below link you need to select your required operating system(Download Heroku Toolbelt for..) for installing heroku toolbelt in your system. And then you can proceed to install.

Please click here to install Heroku ToolBelt

After successfully installation of toolbelt, you can use the heroku command from your terminal.

heroku login
You will prompt to provide heroku credentials(Email and password), once you have authenticated you can access both heroku and git commands.

Create your heroku app:

The following command is used to create an app

heroku create your-app-name

Here 'your-app-name' should be unique, heroku will generate default app-name if you won't specify any app-name.

For creating remote for your app use this command

heroku git:remote -a your-app-name

Define a Procfile:

A Procfile is a mechanism for declaring what commands need to run by your application dynos on the Heroku platform.

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.

Here first you need to install gunicorn using pip 

    pip install gunicorn

    web: gunicorn yoursettings.wsgi --log-file -

Create new file in the root directory of your application as "app.json"

{

    "name": "Your-App-Name",

    "description": "Self-hosted app",

    "repository": "provide your git repository url",

    "scripts": {

    "postdeploy": "python manage.py makemigrations && python manage.py migrate"

  }

}

"app.json" is a json format file for describing web apps. It declares environment variables, Description, and other information required to run an app on Heroku.

To Load your app Static files you need to use "WhiteNoise", this can be install by using pip 

pip install whitenoise

And also you need to update your "wsgi.py" file too

    from whitenoise.django import DjangoWhiteNoise

    application = DjangoWhiteNoise(application)
    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Update STATIC_ROOT in your "settings.py" file for loading your static files 

STATIC_ROOT = (os.path.join(BASE_DIR, "static"))

Database Configuration:

For configuring your database, First you need to install "dj_database_url" using pip

    pip install dj_database_url
    pip install psycopg2

    import dj_database_url

    DATABASES = {
        'default': dj_database_url.config(
        default='sqlite:////{0}'.format(os.path.join(BASE_DIR, 'db.sqlite3'))
        )
    }

Dependecies used:

    1. dj-database-url
    2. Django
    3. gunicorn
    4. psycopg2
    5. whitenoise

Update all your app dependencies in your "requirements.txt" file which is in the root directory of your app. These are the basic dependencies used.

Deploying Your app on Heroku:

    This can be done by using simple git command
        * It will read your Django appication
        * Install your requirements provided
        * Detects your Procfile
        * It will collect your static files.

    git push heroku master

After Sucessfully deploying your app, you will be provided with the url to open your app.

you can also run it by using the command.

heroku open

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties
Latest Comments
Related Articles
How to Create your own e-commerce shop using Django-Oscar. Shirisha Gaddi

Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly ...

Continue Reading...
Django Unit Test cases with Forms and Views Swetha Naretla

By writing unit test cases, you can evaluate each code component in the initial stage itself and it'll improve your app/code performance. Which is the ...

Continue Reading...
Django Conditional Expressions in Queries Anjaneyulu Batta

Reduce database queries in django with Conditional Expressions. By using Conditional Expressions we can use "If...Elif...Else" expressions while querying the database so that we can ...

Continue Reading...