Deployment on Heroku
- Heroku is a Platform as a service(
PaaS
) - Which abstracts us from dealing with servers, all we have to do is sign up,
- Download a few tools and then upload our code to the platform effortlessly
Deployment way
- We need to format/create some files according to Heroku for deployment
- Two ways of Deployment
Using Github
Using CLI
Using github
- Connect your GitHub with Heroku app
- Select the branch
- Deploy
- After successful build, website will be live
Using CLI
- Install and Connect Heroku CLI
sudo snap install --classic heroku
heroku --version
heroku login
- Connect repo with Heroku app
git init
# heroku git:remote -a app_name
heroku git:remote -a blogpost-dj
git add .
git commit -am "make it better"
git push heroku master
- After successful build website will be live
Get App info using CLI
- Get all the env vars
# heroku config --app=heroku_app_name
heroku config --app=blogpost-dj
Deploy static app
- We need to use a trick and tell Heroku that our website is a PHP application.
- Steps to deploy static page on Heroku
- Create
index.php
file- Heroku will search form main/index page in index.php
Refer your main/index in index.php
# index.php
<?php include_once("home.html"); ?>
- Create
composer.json
file- Empty json file
echo '{}' > composer.json
Django app deployment
- Create
Procfile
- It defines process types and explicitly declare what command should be executed to start your app.
# Procfile
# web: gunicorn django_app_name.wsgi --log-file -
web: gunicorn blogpost.wsgi --log-file -
- Add
runtime.txt
- Specify the correct Python version.
- Check available Py version in Heroku stack
python-3.6.1
- Add a
requirements.txt
file- pip freeze > requirements.txt
- Includes all the packages required to run the app
Database
- Resource > Add ons > Heroku Postgres
- Get db_url from Setting > Reveal Config Vars
Additional Packages
required- pip install gunicorn dj-database-url whitenoise psycopg2
- All them in requirements.txt file
- Deploy on Heroku using GitHub or CLI
- Migrate
heroku login
# heroku run puthon manage.py migrate --app=heroku_app_name
heroku run puthon manage.py migrate --app=blogpost-dj
Heroku Postgres
- Get DB URL
heroku pg:credentials:url
Heroku db access using Web
Heroku db access using CLI
- Access your app Database using CLI
heroku pg:psql --app=blogpost-dj
select * from auth_user;
\q
Heroku db access GUI Client
- DATABASE_URL: postgres://{username}:{password}@{hostname}:{port}/{dbname}
- Now add those details to your GUI app
Heroku Dynos
- heroku ps:scale web=1
- heroku ps:scale worker=1
- heroku ps:stop run.9296
Custom Heroku Domain Names