2015-04-28 138 views
1

所以我試圖部署我的python燒瓶應用程序到一個Ubuntu的AWS EC2實例。我已經設置mod_wsgi,配置虛擬主機設置一個virtualenv和創建別名服務器我的靜態文件。出於某種原因,我不能讓我的API自定義路徑返回正確的信息。我試過了我到處搜索的所有東西,這是我的最後一個選擇。燒瓶路線返回500錯誤

#!/usr/bin/env python 
import threading 
import subprocess 
import uuid 
import json 
from celery import Celery 
from celery.task import Task 
from celery.decorators import task 
from celery.result import AsyncResult 
from scripts.runTable import runTable 
from scripts.getCities import getCities 
from scripts.pullScript import createOperation 
from flask import Flask, render_template, make_response, url_for, abort, jsonify, request, send_from_directory, Response 
app = Flask(__name__) 
app.config['CELERY_BROKER_URL'] = 'amqp://guest:[email protected]:5672//' 
app.config['CELERY_RESULT_BACKEND'] = 'amqp' 
app.config['CELERY_TASK_RESULT_EXPIRES'] = 18000 
app.config['CELERY_ACCEPT_CONTENT'] = ['json'] 
app.config['CELERY_TASK_SERIALIZER'] ='json' 
app.config['CELERY_RESULT_SERIALIZER'] = 'json' 

operation = createOperation() 
cities = getCities() 
table = runTable() 
value = '' 
state = '' 

celery = Celery(app.name, backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL']) 
celery.conf.update(app.config) 

@task(bind=True) 
def pull_async_data(self, data): 
    global state 
    state = pull_async_data.request.id 
    operation.runSequence(data) 


@app.route('/api/v1/getMapInfo', methods=['GET']) 
def map(): 
    mp = operation.getMapData() 
    resp = Response(mp, status=200, mimetype="application/json") 
    return resp 


@app.route('/api/v1/getTable', methods=['GET']) 
def tables(): 
    tb = table.getTableInfo() 
    resp = Response(tb, status=200, mimetype="application/json") 
    return resp 


##Get states from the DB 
@app.route('/api/v1/getStates', methods=['GET']) 
def states(): 
    st = cities.getStatesFromDB() 
    resp = Response(st, status=200, mimetype="application/json") 
    return resp 


@app.route('/api/v1/getCities', methods=['POST']) 
def city(): 
    data = request.get_json() 
    # print data 
    ct = cities.getCitiesFromDB(data) 
    resp = Response(ct, status=200, mimetype="application/json") 
    return resp 


@app.route('/api/v1/getQueue', methods=['GET']) 
def queue(): 
    queue = operation.getCurrentQueue() 
    resp = Response(queue, status=200, mimetype="application/json") 
    return resp 



##Checking the status of api progress 
@app.route('/api/v1/checkStatus', methods=['GET']) 
def status(): 
    res = pull_async_data.AsyncResult(state).state 
    js = json.dumps({'State': res}) 
    resp = Response(js, status=200, mimetype="application/json") 
    return resp 


##Perform the pull and start the script 
@app.route('/api/v1/pull', methods=['POST']) 
def generate(): 
    global value 
    value = json.dumps(request.get_json()) 
    count = operation.getCurrentQueue(value) 
    pull_async_data.apply_async(args=(value,)) 
    js = json.dumps({"Operation": "Started", "totalQueue": count}) 
    resp = Response(js, status=200, mimetype="application/json") 
    return resp 


##Check main app 
if __name__ == "__main__": 
    app.run(debug=True) 

這裏是WSGI文件oakapp.wsgi

#!/usr/bin/python 
import sys 

activate_this = '/var/www/oakapp/venv/bin/activate_this.py' 
execfile(activate_this, dict(__file__=activate_this)) 

sys.path.append('/var/www/oakapp') 

print sys.path.insert(0, '/var/www/oakapp/scripts') 

from app import app as application 

這裏是虛擬主機環境

<VirtualHost *:80> 
      ServerName oakapp.com 

      DocumentRoot /var/www/oakapp 

      Alias /js /var/www/oakapp/js 
      Alias /css /var/www/oakapp/css 

      WSGIDaemonProcess oakapp user=apps group=ubuntu threads=5 
      WSGIScriptAlias//var/www/oakapp/oakapp.wsgi 


      <Directory /var/www/oakapp/> 
        WSGIProcessGroup oakapp 
        WSGIApplicationGroup %{GLOBAL} 
        WSGIScriptReloading On 
        Order allow,deny 
        Allow from all 
      </Directory> 

      ErrorLog /var/www/oakapp/logs/oakapp_error.log 
      LogLevel info 
      CustomLog /var/www/oakapp/logs/oakapp_access.log combined 
    </VirtualHost> 

這裏是我的訪問日誌,它創建WSGI實例,所以我必須做正確的事情。

[Tue Apr 28 04:30:03.705360 2015] [:info] [pid 2611:tid 140512828155776] mod_wsgi (pid=2611): Attach interpreter ''. 
[Tue Apr 28 04:30:11.704865 2015] [:info] [pid 2611:tid 140512695293696] [remote 72.219.180.235:10929] mod_wsgi (pid=2611, process='oakapp', application=''): Loading WSGI script '/var/www/oakapp/oakapp.wsgi'. 
[Tue Apr 28 04:30:11.705804 2015] [:error] [pid 2611:tid 140512695293696] None 

這裏是一個netstate -plunt輸出

Active Internet connections (only servers) 
    Proto Recv-Q Send-Q Local Address   Foreign Address   State  PID/Program name 
    tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN  1062/sshd  
    tcp  0  0 127.0.0.1:3031   0.0.0.0:*    LISTEN  29277/uwsgi  
    tcp  0  0 0.0.0.0:46035   0.0.0.0:*    LISTEN  24222/beam  
    tcp6  0  0 :::22     :::*     LISTEN  1062/sshd  
    tcp6  0  0 :::5672     :::*     LISTEN  24222/beam  
    tcp6  0  0 :::80     :::*     LISTEN  2608/apache2  
    tcp6  0  0 :::4369     :::*     LISTEN  24197/epmd  
    udp  0  0 0.0.0.0:17372   0.0.0.0:*       568/dhclient  
    udp  0  0 0.0.0.0:68    0.0.0.0:*       568/dhclient  
    udp6  0  0 :::28264    :::*        568/dhclient 

這裏是目錄結構

├── app.py 
    ├── app.pyc 
    ├── css 
    │   ├── fonts 
    │   │   ├── untitled-font-1.eot 
    │   │   ├── untitled-font-1.svg 
    │   │   ├── untitled-font-1.ttf 
    │   │   └── untitled-font-1.woff 
    │   ├── leaflet.css 
    │   └── master.css 
    ├── js 
    │   ├── images 
    │   │   ├── layers-2x.png 
    │   │   ├── layers.png 
    │   │   ├── marker-icon-2x.png 
    │   │   ├── marker-icon.png 
    │   │   └── marker-shadow.png 
    │   ├── leaflet.js 
    │   └── main.js 
    ├── json 
    │   └── states.json 
    ├── logs 
    │   ├── oakapp_access.log 
    │   └── oakapp_error.log 
    ├── oakapp.wsgi 
    ├── sass 
    │   └── master.scss 
    ├── scripts 
    │   ├── database 
    │   │   ├── cities_extended.sql 
    │   │   ├── oak.db 
    │   │   └── states.sql 
    │   ├── getCities.py 
    │   ├── getCities.pyc 
    │   ├── __init__.py 
    │   ├── __init__.pyc 
    │   ├── pullScript.py 
    │   ├── pullScript.pyc 
    │   ├── runTable.py 
    │   └── runTable.pyc 
    ├── templates 
        └── index.html 

任何幫助理解。

這裏是一個捲曲的請求從我個人的機器跑到主機

djove:.ssh djowinz$ curl http://**.*.**.139/api/v1/getTable 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
    <title>500 Internal Server Error</title> 
    <h1>Internal Server Error</h1> 
    <p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p> 

這裏是捲曲要求使用本地主機的請求從主機跑到主機。

[email protected]:/var/www/oakapp# curl http://localhost/api/v1/getTable 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> 
    <title>500 Internal Server Error</title> 
    <h1>Internal Server Error</h1> 
    <p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p> 
+0

可以使用捲曲輸出顯示請求和響應?本地或遠程使用@ kracekumar – Kracekumar

+0

,例如從服務器或從我的個人機器? – djowinz

+0

服務器輸出。 – Kracekumar

回答

0

原來,這是我如何實例化uwsgi的問題。我從零開始刪除了所有內容,並使用uwsgi遍歷了安裝說明,並能夠讓所有的東西都能正常工作。