Parashar
Parashar

Reputation: 153

Deploying Flask as CGI on shared web hosting

I have small Flask app that I wish to deploy. I have bought domain name and hosting service on bigrock.com. I wrote a small mock app to test whether my Flask app would work as CGi. I have used following configuration:

File :/home/USERNAME/public_html/cgi-bin/cgi.cgi

#!/usr/bin/env python  

from wsgiref.handlers import CGIHandler
from myapp import app
import os
import cgitb; cgitb.enable()

os.environ['SERVER_NAME'] = '127.0.0.1'
os.environ['SERVER_PORT'] = '5000'
os.environ['REQUEST_METHOD'] = 'GET'
os.environ['PATH_INFO'] = ""
CGIHandler().run(app)

File :/home/USERNAME/public_html/cgi-bin/myapp.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "This is Hello World!\n"

if __name__ == "__main__":
    app.run()

File : /home/USERNAME/public_html/.htaccess

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

Running cgi.cgi using ssh session as ./cgi.cgi

gives following output:

Status: 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 21

This is Hello World!

Also running myapp.py as

python myapp.py

activates the development server:

Running on http://127.0.0.1:5000/

And now, no matter when I access my site anyway:

www.mysite.com

www.mysite.com/cgi.cgi

www.mysite.com/cgi-bin/cgi.cgi

I get a 500 internal server error.

When I used a Python hello world cgi script it worked perfectly:

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""

I know similar questions have been brought up again and again but none has been properly answered or the answers don't seem to work.

Upvotes: 4

Views: 3538

Answers (1)

Parashar
Parashar

Reputation: 153

After tweaking multiple things I finally got it to work. Thanks guys.

I think this did the trick: I uploaded all the files to /scgi-bin and edited .htaccess to reflect the same.

Upvotes: -1

Related Questions