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.

  1. Account > My product > Web Hosting > Manage
  2. Change Password in settings on the right
  3. click cPanel admin
  4. scroll down and go to “setup Python App”
  5. create application
  6. 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)
  7. create
  8. now you can leave cpanel and go to the root folder where you save all your files
  9. 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

  10. compress the root folder that contains all you files including the requirements.txt you just created
  11. now go back to cPanel and go to file manager
  12. 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
  13. go into this folder and click upload to upload the zip file
  14. once you’ve uploaded it and can see it in your root folder, you can extract it
  15. you will notice a new folder in your root folder.
  16. go into this folder > select all the files > move them up one level so that they are all directly accessible from your root folder
  17. in the root folder there is a file called “passenger_wsgi.py”, click edit it
  18. replace everything inside with
    from your-app import app-name as app-name
    where
    • your-app is the name of your_app.py file
    • app-name is something of something = Flask(__name__) in your_app.py
    • app-name can be anything you want to call it (or keep it the same as the app name)
  19. save changes and close it
  20. go back to setup Python App and edit your web app
  21. replace the field of Application startup file with “your_app.py”
  22. replace the “passenger_wsgi.py” in Application Entry point with the app-name from step 17
  23. save your web app
  24. scroll down to configuration files and type in “requirements.txt”, add
  25. 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

  26. scroll up to application URL and click open
  27. 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

  1. Account > My product > Web Hosting > Account Action > Reset everything
  2. create a mini flask app that only returns hello world
  3. zip it with a requirement txt file and deploy it
  4. if you see a hello world page when you open the url, you are good to move on

2. check your file names

  1. 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