Here we will create flask python app on our cPanel with a sub-domain.
How to run Flask python app on shared hosting cPanel using SSH
We can use main domain or sub-domain for flask web-application.
Goto cPanel login and login your cPanel. Open File-Manger and browse your desire domain directory. In my case, I use a sub-domain. cPanel automatic create a folder name “cgi-bin” if not then create a folder name “cgi-bin”
Create two files
It’s not required with the same name. We can use any name but the extension must be same as like I was written, .cgi and .py
Open filename.py on your cPanel file editor. Let’s create a simple flask application. Here is the sample code of flask app.
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello HomePage" if __name__ == '__main__': app.run(debug=True) # Debug true for development purpose.
save the file and close this window. Now open the .cgi file on your cPanel editor. Write code written below.
#!/home/creative/public_html/sub.example.com/venv/bin/python from wsgiref.handlers import CGIHandler from app import app CGIHandler().run(app)
Here some important things to know and change. Here I move my virtualenv of python to my subdomain directory. It’s not a tough task to do. Just copy the “venv” folder from “virtualenv-12.0.7” and paste that folder into your domain directory.
Then paste the url top of the cgi file like example.
The first line of the cgi file should same some characters.
After that second line same as in the example. In the third line
“from app import app” here is the connection of .py file.
(from “name-of-py-file” import “flask-app“)
In my case my .py file name app.py and my flask app is app. That’s why it was written app twice. The last line will same as written but replace “app” with your “flask-app” if you create flask-app with different. Last line “app” is not the filename of .py file.
One most important is the file permissions. .cgi and .py file permission change to 0755. We know folders permission are 0755 but python cgi project need file permission 0755 for .py and .cgi file.
Now browse your http://domainname/cgi-bin/filename.cgi/
Be sure browse your domain like I written not http://domainname/cgi-bin/filename.cgi because flask python route end with ‘/’ So ‘/’ is important end of the link.