Deploying a Flask application with CGI

12,706

I will try to show what I've done and it is working in Godaddy sharing host account:

In the cgi-bin folder in MYSITE folder, I added the following cgi file:

#!/home/USERNAME/.local/bin/python3
from wsgiref.handlers import CGIHandler

from sys import path
path.insert(0, '/home/USERNAME/public_html/MYSITE/')
from __init__ import app

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

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

if __name__ == '__main__':
    app.wsgi_app = ProxyFix(app.wsgi_app)
    CGIHandler().run(app)

As you can see the init file in the MYSITE folder have the flask app.

The most important thing is to set the permissions right. I setted 755 to this folder permission AS WELL AS to "/home/USERNAME/.local/bin/python3" folder!! Remember that the system needs this permission to open flask.

To open the cgi I have the following .htaccess file in MYSITE folder:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /home/USERNAME/public_html/MYSITE/cgi-bin/application.cgi/$1 [L]

So it will render the cgi file when someone enters your page.

Share:
12,706
monostop
Author by

monostop

Updated on July 20, 2022

Comments

  • monostop
    monostop almost 2 years

    I have written a small application using the Flask framework. I try to host this using cgi. Following the documentation I created a .cgi file with the following content:

    #!/usr/bin/python
    from wsgiref.handlers import CGIHandler
    from yourapplication import app
    
    CGIHandler().run(app)
    

    Running the file results in following error:

    ...

    File "/usr/lib/pymodules/python2.7/werkzeug/routing.py", line 1075, in bind_to_environ wsgi_server_name = environ.get('HTTP_HOST', environ['SERVER_NAME'])
    KeyError: 'SERVER_NAME'
    Status: 500 Internal Server Error
    Content-Type: text/plain
    Content-Length: 59

    In my application I have set:

    app.config['SERVER_NAME'] = 'localhost:5000'
    

    When I run the application with the Flask development server it works perfectly well. As you can tell I'm very new to this stuff and I have search for others with similar errors but with no luck. All help is appreciated.

  • jlev
    jlev almost 5 years
    This was very helpful for a stupid deployment situation I got into on a shared hosting environment. Make sure to comment out or remove the PATH_INFO line though, it may interfere with any Flask routes you have defined.
  • Dinidiniz
    Dinidiniz almost 5 years
    Edited and removed the PATH_INFO
  • raven
    raven about 2 years
    Are you meant to retain USERNAME in the RewriteRule or replace that with your account name?
  • Dinidiniz
    Dinidiniz about 2 years
    Replace with your account name