This post will help you with setting up of any python-flask (or any cgi enabled python) application on your GoDaddy hosting. I, myself was struggling to get this done, so I know the pain. There was information all over the internet, but everything was too scrambled. That was when I decided to provide my learning to anyone out there still struggling to do the same.

Prerequisites:-
  1. GoDaddy shared hosting account (Deluxe OR Ultimate)
  2. Python application (Don’t worry if you need a test application. Clone this repo – Basic-Python-Flask-App)

That’s it. We are ready to go!

Directory Structure
    /home
     |/<USERNAME>
     |/public_html
     |/site_home
     |/.htaccess
     |/cgi-bin
     |/test-script.cgi
     |/personal
     |/code_home
     |/app.py
Steps :-
  1. Log in to your cPanel, and map the domain to a directory in ~/public_html/<DIRECTORY_NAME>(Assuming the directory as “/home/<USERNAME>/public_html/site_home”).
  2. Log in to your shared hosting server using SSH.
  3. Create directory structure as mentioned above.
  4. Install a new python environment under /home/<USERNAME>/.local/python
  5. Create a virtual environment under /home//personal/code_home/venv
  6. Copy your application in a directory outside of ~/public_html. This is a important step and please don’t put your application files in ~/public_html/. Assuming this code directory as /home/<USERNAME>/personal/code_home
  7. Create a new cgi-script file
#!/home/<USERNAME>/.local/bin/python2.7
from wsgiref.handlers import CGIHandler

from sys import path
path.insert(0, '/home/<USERNAME>/personal/code_home')

activate_this = '/home/<USERNAME>/personal/code_home/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from app import app as application

class ProxyFix(object):
def __init__(self, application):
self.application = application

def __call__(self, environ, start_response):
environ['SERVER_NAME'] = "test.something.com"
environ['SERVER_PORT'] = "80"
environ['REQUEST_METHOD'] = "GET"
environ['SCRIPT_NAME'] = ""
environ['PATH_INFO'] = "/"
environ['QUERY_STRING'] = ""
environ['SERVER_PROTOCOL'] = "HTTP/1.1"
return self.application(environ, start_response)

if __name__ == '__main__':
# This is to test if the cgi script can be executed without any errors. Please remember to test only a URL which is valid.
# application.wsgi_app = ProxyFix(application.wsgi_app)
CGIHandler().run(application) # Run the application using CGI Handler

Create a file .htaccess in the site_home directory. And add the following content in it.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/<USERNAME>/public_html/site_home/cgi-bin/test-script.cgi/$1 [L]

This is the most important step of all. Change permissions for .htaccess, test-script.cgi & app.py

chmod 755 /home/<USERNAME>/public_html/site_home/.htaccess
chmod 755 /home/<USERNAME>/public_html/site_home/cgi-bin/test-script.cgi
chmod 755 /home/<USERNAME>/personal/code_home/app.py # Triggering python script