Deploy flask application on 1&1 shared hosting (with CGI)

前端 未结 3 450
暗喜
暗喜 2020-12-03 01:59

I\'ve written a web application for my sports club with the flask web framework. I did everything on my local machine with the build-in test server.

Know they told m

3条回答
  •  孤街浪徒
    2020-12-03 02:48

    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['PATH_INFO'] = "/"
           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 and all python packages.

    For debugging I recommend visiting via ssh terminal your site and just run the CGI alone. If it works, but in the web don't, it means probably you need to give more permissions or you wrote wrong the .htaccess file.

    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.

提交回复
热议问题