欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Deploy Flask on Godaddy Linux share hosting with cPanel

程序员文章站 2024-02-21 15:14:10
...
1 test your python environment, be attention that put your python code on document root fold or sub folder cgi-bin, otherwise your code is not able to accessed through cgi.

here is sample of testing python code:

#!/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>"

set privilege to 755 !!!!!!!(dont know why)
Meanwhile add a handler for extension .py through cPanel or change hello.py to hello.cgi

run it through : http://yourdomain.com/hello.py or hello.cgi

2 install virtualenv pip and create your own python environment and install Flask through pip.

3 create cgi application under folder of cgi-bin:
#!/yourpythonpath/python
#myapp.py
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)


4 create myapp folder under document root, create .htaccess inside of myapp:

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


Be careful that although there is a link [color=red]www[/color] point to [color=red]public_html[/color] folder, but it does not work if you put here [color=red]www[/color].

test link: http://yourdomain.com/myapp/


reference: [url]http://flask.pocoo.org/docs/deploying/cgi/[/url]

Regarding FastCGI on Godaddy shared host:

I have successfully config Flash with FastCGI through flup, but it does not make sense. Since the socket will be closed after a HTTP access, apache creates a new process to deal with a new request.

Lets look into flup source code:

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() will throw a exception, which indicate that socket does not exist, there is no different between CGI and FastCGI on initialising of python app.

I also got proof on Godaddy support website:
[url]https://support.godaddy.com/help/article/20/does-your-cgi-service-support-socket-connections-or-socket-modules[/url]