Deployment of Flask web app on cPanel
It took me over a day to figure out how to properly deploy a website, so I feel like it’s a good idea to share how I did it in case some else might need it.
PS: totally my own experience, from a beginner perspective
What I had
I had a web app built using Python3 and the Flask framework on a mac device. It contains multiple HTML and CSS files and is fully functional on local host.
Things to have before starting
- adomain
- a server hosting account
I got both from Godaddy and I’m using the Economy Linux Hosting with cPanel plan
The acutal process (2021 version)
A lot of tutorials says you need to change DNA setting and etc. but I later found out that Godaddy automatically does it for you when you set up your server hosting account so don’t worry about it.
- Account > My product > Web Hosting > Manage
- Change Password in settings on the right
- click cPanel admin
- scroll down and go to “setup Python App”
- create application
- fill the fields:
Python version: latest
Application root: name for folder that you will put all your files in later
Application URL link to your website (can leave it blank)
Application startup file: leave blank for now
Application Entry point: leave blank for now
Passenger log file: the log file (I prefer doing “your app root”/log.txt) - create
- now you can leave cpanel and go to the root folder where you save all your files
- add a requirements.txt file and put down all the packages you’ve used in making your web app
since I used only Flask, I wrote down only ‘Flask’ in the txt file
- compress the root folder that contains all you files including the requirements.txt you just created
- now go back to cPanel and go to file manager
- you should be able to see a folder with the name you entered for “application root” in the previous step. If not, reload the page
- go into this folder and click upload to upload the zip file
- once you’ve uploaded it and can see it in your root folder, you can extract it
- you will notice a new folder in your root folder.
- go into this folder > select all the files > move them up one level so that they are all directly accessible from your root folder
- in the root folder there is a file called “passenger_wsgi.py”, click edit it
- replace everything inside with
from your-app import app-name as app-name
whereyour-app
is the name of your_app.py fileapp-name
is something ofsomething = Flask(__name__)
in your_app.pyapp-name
can be anything you want to call it (or keep it the same as the app name)
- save changes and close it
- go back to setup Python App and edit your web app
- replace the field of Application startup file with “your_app.py”
- replace the “passenger_wsgi.py” in Application Entry point with the
app-name
from step 17 - save your web app
- scroll down to configuration files and type in “requirements.txt”, add
- stop your app and click run pip install in the configuration section
if your don’t stop your app before running pip install you might get an error
- scroll up to application URL and click open
- you should now be able to access your website!
if not, try clicking the restart button to refresh the page and then open the url
Ways of debugging that I’ve tried
1. test with a simple app
there’s a bunch of youtube videos that teach how to do this
- Account > My product > Web Hosting > Account Action > Reset everything
- create a mini flask app that only returns hello world
- zip it with a requirement txt file and deploy it
- if you see a hello world page when you open the url, you are good to move on
2. check your file names
- if your mini app works fine but no longer works after you add your html files, the the page you see is
future home for something quite cool
then the problem is likely to be your file names. Check if you have files named:
- home.html
change it to something else (as well as all the redirecting stuff in your app) and restart the application. The problem should be fixed now when you open the url.
3. testing if it’s really the problem of your app or html files
in your_app.py, comment out all the return render_template('something.html')
and replace it with return 'hello world'
. Restart your application and open the url, if it workds, your app is fine.
4. reset
If nothing looks wrong but the app just doesn’t work, try reset your server hosting account and try everything again.
- Account > Manage product > Web Hosting > Account Actions > Reset