Deploy Flask on Godaddy Linux share hosting with cPanel

1testyourpythonenvironment,beattentionthatputyourpythoncodeondocumentrootfoldorsubfoldercgi-bin,otherwiseyourcodeisnotabletoaccessedthroughcgi.

hereissampleoftestingpythoncode:

#!/usr/bin/python
#hello.py
print "Content-Type: text/html"     # HTML is following
print
print "<html><header><title>Test CGI Python</title></header><body>Hello CGI!</body></html>"

setprivilegeto755!!!!!!!(dontknowwhy)

Meanwhileaddahandlerforextension.pythroughcPanelorchangehello.pytohello.cgi

runitthrough:http://yourdomain.com/hello.pyorhello.cgi

2installvirtualenvpipandcreateyourownpythonenvironmentandinstallFlaskthroughpip.

3createcgiapplicationunderfolderofcgi-bin:

#!/yourpythonpath/python
#myapp.py
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)

4createmyappfolderunderdocumentroot,create.htaccessinsideofmyapp:

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

Becarefulthatalthoughthereisalinkwwwpointtopublic_htmlfolder,butitdoesnotworkifyouputherewww.

testlink:http://yourdomain.com/myapp/

reference:http://flask.pocoo.org/docs/deploying/cgi/

RegardingFastCGIonGodaddysharedhost:

IhavesuccessfullyconfigFlashwithFastCGIthroughflup,butitdoesnotmakesense.SincethesocketwillbeclosedafteraHTTPaccess,apachecreatesanewprocesstodealwithanewrequest.

Letslookintoflupsourcecode:

sock = socket.fromfd(FCGI_LISTENSOCK_FILENO, socket.AF_INET,
                                 socket.SOCK_STREAM)
            try:
                sock.getpeername()
            except socket.error, e:
                if e[0] == errno.ENOTSOCK:
                    # Not a socket, assume CGI context.
                    isFCGI = False
                elif e[0] != errno.ENOTCONN:
                    raise

sock.getpeername()willthrowaexception,whichindicatethatsocketdoesnotexist,thereisnodifferentbetweenCGIandFastCGIoninitialisingofpythonapp.

IalsogotproofonGodaddysupportwebsite:

https://support.godaddy.com/help/article/20/does-your-cgi-service-support-socket-connections-or-socket-modules

相关推荐