我已经配置了Apache、MySql和Python。安装了mod_wsgi并在我的系统上运行了一个示例.wsgi“Hello World”应用程序。我的Flask应用程序还使用Flask的webserver在virtualenv内部和外部执行python myServer.py,virtualenv也配置良好(安装了Flask和mysql python)。
现在我正在尝试部署flask应用程序,从页面中得到以下错误:
Server error!
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
If you think this is a server error, please contact the webmaster.
Error 500
我去了Apache的错误日志,得到了这个:
[Mon Jul 14 19:27:50.692271 2014] [wsgi:error] [pid 56887] [remote ::1:0] mod_wsgi (pid=56887): Target WSGI script '/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi' cannot be loaded as Python module.
[Mon Jul 14 19:27:50.692387 2014] [wsgi:error] [pid 56887] [remote ::1:0] mod_wsgi (pid=56887): Exception occurred processing WSGI script '/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi'.
[Mon Jul 14 19:27:50.692411 2014] [wsgi:error] [pid 56887] [remote ::1:0] Traceback (most recent call last):
[Mon Jul 14 19:27:50.692438 2014] [wsgi:error] [pid 56887] [remote ::1:0] File "/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi", line 4, in <module>
[Mon Jul 14 19:27:50.692535 2014] [wsgi:error] [pid 56887] [remote ::1:0] from myServer import app as application
[Mon Jul 14 19:27:50.692553 2014] [wsgi:error] [pid 56887] [remote ::1:0] File "/Applications/xampp/xamppfiles/htdocs/nudgeAlong/myServer.py", line 2, in <module>
[Mon Jul 14 19:27:50.692633 2014] [wsgi:error] [pid 56887] [remote ::1:0] import MySQLdb
[Mon Jul 14 19:27:50.692658 2014] [wsgi:error] [pid 56887] [remote ::1:0] ImportError: No module named MySQLdb
我的Wsgi应用程序:
import sys
activate_this = '/Applications/XAMPP/htdocs/nudgeAlong/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
sys.path.append('/Applications/xampp/xamppfiles/htdocs/nudgeAlong/')
from myServer import app as application
我的httpd.conf虚拟主机部分是:
<VirtualHost *>
ServerName MeuServer
WSGIDaemonProcess myServer threads=5
WSGIScriptAlias /myapp /Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi
<Directory /Applications/XAMPP/htdocs/nudgeAlong/wsgi>
WSGIProcessGroup myServer
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
我的文件夹结构如下:
__主文件夹
____myServer.py服务器
____wsgi公司/
________myServer.wsgi
____静脉/
希望有人能帮忙。
奇怪的是,在我的程序的第2行,即导入MySQLdb行中发生了错误,但在此之前,我导入了Flask,没有发生错误,所以我可能正在从我的系统中获取库。。。
最后,如果需要,我的烧瓶应用程序:
from flask import Flask, jsonify, render_template, request, make_response, request, current_app
import MySQLdb
from datetime import timedelta
app = Flask(__name__)
db_owen = MySQLdb.connect("localhost","root","","climote_trial")
@app.route('/get_json')
@crossdomain(origin='*')
def get_json():
results = []
c = db_owen.cursor()
c.execute("select date,message_log from trialresults")
jsonresult = c.fetchall()
for x in jsonresult:
dic = {'col1':str(x[0]),'col2':x[1] }
results.append(dic)
return jsonify({ 'results':results });
@app.route('/')
def index():
return render_template('number_1.html')
if __name__ == '__main__':
app.run(debug= True, port=53000)
最佳答案
解决了的:
如果我错了,任何人都会纠正我。我相信问题在于,当你安装mysql python时,你会用python解释器“连接”你的内部mysql文件。所以,当我安装它时,它将我的MySql连接到Python。之后我安装了XAMPP,它有自己的MySql。一切都发生了冲突,我试图用很多方法来解决,但没有一个奏效。我从中了解到的两件事是,你必须决定是否要使用MAMP、XAMPP之类的工具,或者自己安装web服务器、数据库和驱动程序的基本方法。另一件重要的事情是,这三个Web服务器、数据库和编程语言解释器必须具有相同的体系结构(32位或64位)。
最后我决定重新安装所有东西,下面是我使用MacOSX Mavericks的步骤:
清理混乱:我删除了XAMPP(在删除它之前请注意移动应用程序文件,我几乎丢失了所有代码)
在此之后卸载了MySQLlink
卸载mysql python:sudo pip卸载mysql python
安装MySql修复我的符号链接
sudo rm/usr/lib/libmysqlclient.18.dylib
sudo ln-s/usr/local/mysql/lib/libmysqlclient.18.dylib/usr/lib/libmysqlclient.18.dylib
已安装mysql python(注意设置正确的路径)
导出路径=$PATH:/usr/local/mysql/bin
sudo pip安装mysql python
按照网站教程安装了Apache
已安装mod_wsgi,设置到Apache和Python解释器的正确路径
./configure——使用apxs=/path/to/apache/bin/apxs\——使用python=/path/to/bin/python
在那之后,我让我的应用程序在烧瓶的网络服务器上运行。使用了我问题代码中的相同配置,一切正常。