How to deploy your website to a custom domain using Heroku | by Sanket Gupta | Towards Data Science

This blog documents the steps needed to deploy a website written in Python with Flask framework to a custom domain using Heroku and NameCheap. Flask is a micro-framework that allows us to use Python in the back-end to interact with our front-end code in HTML/CSS or Javascript to build web sites. People also use other frameworks like Django for this purpose, but my favorite is Flask as it is easy to get started with.
A small Flask web-site can be created by creating a new repo, by creating a Python file such as this:
# app.py from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello World!"if __name__ == "__main__":    app.run(port=5000)
A typical Flask app has folders static for all the CSS and JS files as well as images etc. and a templates folder for HTML files. I will not go into much detail here, but fantastic information on this can be found in tutorials by Miguel Grinberg.
When the above file is run python app.py it opens the website locally with “Hello World!” written in HTML.
How do we deploy this website to a custom domain for everyone to see?
Go to Heroku website and sign up for an account. Heroku will allow us to deploy the website to a domain ending with herokuapp.com. We will use this website and deploy to custom domain. We could have used Amazon Web Services to deploy the website, but I find it to be much more complex than using a solution like Heroku. AWS is infrastructure-as-a-service (IaaS) and Heroku is platform-as-a-service (PaaS). Because of this we do not need to worry about exact infrastructure details with Heroku! That’s what makes it simpler to use.
Now, go to this link and download heroku for the command line. If you have homebrew on your machine, you can use brew install heroku/brew/heroku to install it on your machine. Next run heroku login to enter your Heroku credentials.
a) Make a Procfile:To deploy your app to Heroku, first create a Procfile and save it in the same folder as app.py . Note that the Procfile does not have any extension like txt etc. This Procfile will have the content web: gunicorn app:app This statement will allow Heroku to run gunicorn as the web server for this app. Make sure that you have gunicorn installed in your virtual environment.
b) Make requirements.txt:Also, use pip freeze > requirements.txt to write all the requirements of this project into a file. Heroku will use this to install all the necessary packages in the cloud to run your app.
c) [Optional] Make runtime.txt:If you want to specify which Python version should be used in the cloud to run your app by Heroku, just mention it in this file as python-3.6.6 .
d) Deploy to Heroku:Run heroku create <app-name>where <app-name> is the name of your liking. If your app name is calm-river then Heroku will deploy your website to calm-river.herokuapp.com. Make sure git is installed on your command line. Run git init to initialize your current folder as a git repository. Next, run git add . and thengit commit -m "first commit" to commit all your files to git . Finally run git push heroku master to deploy your app to Heroku~ If all things go well, you can access your website at <app-name>.herokuapp.com
Next, we’ll need to buy a custom domain from NameCheap or GoDaddy. Say you bought the domain, example.comNext, run heroku domains:add www.example.com to add this custom domain to your Heroku app. It will give you a DNS target type and DNS target name. This target type should be CNAME . We need to link this CNAME record to the settings console of your domain on NameCheap or GoDaddy.
Go to Namecheap settings console, and go to Advanced DNS . Add CNAME record to Host targets with Type being CNAME and Host being www and Value being the DNS target name that you received when you added the domains to Heroku.
Having added the CNAME record, add URL Redirect record with Host being @ and Value being http://www.example.com with Unmasked setting.
Allow upto 30 minutes for these settings to propagate, and your website should be live on your custom domain! Hope these steps were useful in your quest to share your work with the world. More details can be found on Heroku website on these steps. Hope to see your website around!
P.S. I am creating a NEW COURSE titled “Overview of Query Understanding Techniques”. This course will cover information retrieval techniques, text mining, query understanding tips and tricks, ways to increase precision, recall and user clicks. Please sign up here if you are interested! This show of interest will allow me to prioritize and build the course.
If you have any questions, drop me a note at my LinkedIn profile or send me an email at sanket@omnilence.com. Thanks for reading!