- Create an app on Heroku
- Create a free Postgres Heroku instance.
- Set a new git remote to your Heroku git link
- Copy your Heroku git link from Heroku's website
- Go back to the root of your project directory and type
git remote add production <your heroku git link>
Now your app has 2 git remotes! origin -> GitHub and production -> Heroku
-
Set environment variables on Heroku! Our app relies on a few env variables. Locally on our computer, we used
.env
file to manage all these variables but in production, we don't do that. On heroku, here's how you can add env variables to the session.ensure you're in your project directory when running heroku commands. (add all the environment variables you need)
heroku config:set FLASK_APP='start' FLASK_ENV='production' S3_KEY="yourawss3key" S3_SECRET="yourawss3secret" S3_BUCKET="your-bucket" # and more
protip: For security, use a different AWS S3 bucket and key/secret for production vs staging vs development environments!
DATABASE_URL is already set for you by Heroku when you created a Postgres DB at step 1.
Pro tip: see all env variables set
heroku config
-
Temporarily remove
peewee-db-evolve
fromrequirements.txt
(copy first; you'll need it later) -
Make a commit and deploy
git push production master
-
Add
peewee-db-evolve
back intorequirements.txt
(Please make sure peewee-db-evolve version is at least 3.7.3) -
Add a Procfile at the root of your project
Procfile
release: python migrate.py web: gunicorn start:app --preload
The setting above tells Heroku to run
python migrate.py
each time you deploy new code, update env configs or modify heroku's add-on resources. And to start the flask server, usegunicorn start:app --preload
command.gunicorn start:app
here tells Gunicorn to refer tostart.py
for starting the app. -
pip install gunicorn
Gunicorn is a production-ready web server software (usually for Python apps). Every time you start Flask usingflask run
, it uses a web server software that is not great for production. Hence the use of gunicorn! Remember to runpip freeze > requirements.txt
-
Make a new commit (be sure to run
git add Procfile
first) -
Deploy again
git push production master
That's all! You should see a link to your application at the end of GIT Push process.
All these steps are a one time affair. In future when you make changes to your app, it's as simple as committing to git and then pushing it with git push production master
Want your own domain?
- Buy a domain
- Go to heroku.com, add your domain to the app
- Go to your domain manager (usually where you buy from) and change the nameserver/dns settings!