Deploying flask app to Apache shared hosting

时光毁灭记忆、已成空白 提交于 2019-12-12 02:25:50

问题


I am trying to deploy a simple flask application in the Apache shared hosting server.

I am not sure what is wrong here.

I am stuck at the .cgi file for now.

The flask app - hello.py:

#!/usr/bin/python

from flask import Flask
app = Flask(__name__)

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

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

The myapp.cgi file:

#!/usr/bin/python  

import os
from wsgiref.handlers import CGIHandler
from hello import app


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)

Both the files are placed in the /home/username/public_html/cgi-bin directory

The same cgi-bin has the directory named myenv - it's a virtualenv I have created. The virtualenv is active.

Now,

I navigate to the cgi-bin directory and run -

 python hello.py

I get this :

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

So this is fine. Now I am running the myapp.cgi file:

python myapp.cgi

I get this :

Status: 301 MOVED PERMANENTLY
Content-Type: text/html; charset=utf-8
Content-Length: 251
Location: http://127.0.0.1:5000/

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="http://127.0.0.1:5000/">http://127.0.0.1:5000/</a>.  If not click the link.

How can I make this status as 200 OK, Please suggest.

Thanks!


回答1:


I think you have 2 big misunderstandings about how apache works with flask with the help of cgi.

  1. Apache uses the system directorys for the python interpreter. You can in fact change the sys.path Like here descriped. But that is far from ideal.

  2. you don't have to call python for your cgi file. The Server will do that when you did your config correctly

in the cgi doc of flask are some ways how you get the server to work with the cgi file.

Since you say you want it to upload at shared hosting writing a .htaccess file for your needs would be the most promesing way, since most of those services only allow you to work from your public dircectory. In this case you also have to use a shared hoster where python is on the server or be willed to install python with all the packages you need for you, since you can't install any packages by yourself.

You could try the changing of the interpreter path, but i have no experience if that would work on shared hosting.




回答2:


I had to make few changes in the .cgi file. Below is the final file.

import os
from wsgiref.handlers import CGIHandler
from hello import app


CGIHandler().run(app)

and added these lines in my hello.py file:

import os
import sys
sys.path.insert(0, '/home/username/public_html/cgi-bin/myenv/lib/python2.6/site-packages') 

Refer this - https://medium.com/@mohdejazsiddiqui/deploy-flask-app-in-apache-shared-hosting-5b3c82c8fd5e



来源:https://stackoverflow.com/questions/33902167/deploying-flask-app-to-apache-shared-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!