2013-08-25 100 views
7

我想在apache2上安裝mod_wsgi,在Ubuntu上。 所以我安裝了libapache2-mod-wsgi包,我用a2enmod激活了他。apache2和mod wsgi:目標WSGI腳本不能作爲Python模塊加載

我有一個網站(languageAnalyz),我正在嘗試使用mod_wsgi。 當我打電話本地主機/ languageAnalyz,我得到一個錯誤500

在Apache2的日誌,我可以看到這一點:

[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] mod_wsgi (pid=4993): Target WSGI script '/var/www/languageAnalyz/test-wsgi.py' cannot be loaded as Python module. 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] mod_wsgi (pid=4993): SystemExit exception raised by WSGI script '/var/www/languageAnalyz/test-wsgi.py' ignored. 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] Traceback (most recent call last): 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/test-wsgi.py", line 10, in <module> 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1]  WSGIServer(app).run() 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi.py", line 112, in run 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1]  sock = self._setupSocket() 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 997, in _setupSocket 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1]  req.run() 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 572, in run 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1]  self._end(appStatus, protocolStatus) 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 601, in _end 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1]  sys.exit(appStatus) 
[Sun Aug 25 11:28:21 2013] [error] [client 127.0.0.1] SystemExit: 0 

我GOOGLE了這個錯誤,我發現很多解決方案(大多數時候爲Django項目)。 我無意中發現,我需要創建一個__init__.py文件。 我做了什麼,這是我__init__.py文件:

__all__ = ['app','get_size_dir','get_nbrf_dir','getStats'] #name of my functions 
import index # my three python files 
import analyz 
import test-wsgi 

__all__名單,這是在這三個文件的功能名稱。 我只是嘗試啓動test-wsgi.py,並得到了同樣的錯誤500

這裏是我的test-wsgi.py文件:

import os,sys 
sys.path.append(os.path.dirname(__file__)) 
from cgi import escape,parse_qs 
from flup.server.fcgi import WSGIServer 
def app(environ, start_response): 
    start_response('200 OK',[('Content-Type','text/plain; charset=utf-8')]) 
    yield "hello world!" 
WSGIServer(app).run() 

那麼,什麼是錯的? 謝謝

編輯: 這裏是我的Apache2的conf:

WSGIPythonPath /var/www/languageAnalyz 
<VirtualHost *:80> 
    ... 
    <Directory /var/www/languageAnalyz/> 
    Options +Indexes ExecCGI FollowSymLinks MultiViews 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
    DirectoryIndex index.py 
    SetHandler wsgi-script 
    </Directory> 
    ... 
</VirtualHost> 

Edit_bis: 所以我嘗試這樣做,因爲我的Django的文檔閱讀。 我改變我的Apache2的conf到:

WSGIPythonPath /var/www/languageAnalyz 
<VirtualHost *:80> 
    ... 
    WSGIScriptAlias /IPA /var/www/languageAnalyz/testwsgi.py 
    <Directory /var/www/languageAnalyz/> 
      Options +Indexes FollowSymLinks MultiViews 
      AllowOverride None 
      Order allow,deny 
      Allow from all 
      DirectoryIndex testwsgi.py 
    </Directory> 

    ... 
</VirtualHost> 

我重新啓動的Apache2,我得到了同樣的錯誤,還有一個前:

[Sun Aug 25 12:47:18 2013] [notice] caught SIGTERM, shutting down 
[Sun Aug 25 12:47:19 2013] [notice] FastCGI: wrapper mechanism enabled (wrapper: /usr/lib/apache2/suexec) 
[Sun Aug 25 12:47:19 2013] [notice] FastCGI: process manager initialized (pid 7879) 
[Sun Aug 25 12:47:19 2013] [notice] mod_python: Creating 8 session mutexes based on 150 max processes and 0 max threads. 
[Sun Aug 25 12:47:19 2013] [notice] mod_python: using mutex_directory /tmp 
[Sun Aug 25 12:47:19 2013] [warn] mod_wsgi: Compiled for Python/2.7.3. 
[Sun Aug 25 12:47:19 2013] [warn] mod_wsgi: Runtime using Python/2.7.4. 
[Sun Aug 25 12:47:19 2013] [notice] Apache/2.2.22 (Ubuntu) mod_fastcgi/mod_fastcgi-SNAP-0910052141 PHP/5.4.9-4ubuntu2.2 mod_python/3.3.1 Python/2.7.4 mod_ruby/1.2.6 Ruby/1.8.7(2012-02-08) mod_wsgi/3.4 configured$ 
[Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param REQUEST_METHOD required by WSGI! 
[Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_NAME required by WSGI! 
[Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_PORT required by WSGI! 
[Sun Aug 25 12:47:19 2013] [error] WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI! 
[Sun Aug 25 12:47:19 2013] [error] Status: 200 OK\r 
[Sun Aug 25 12:47:19 2013] [error] Content-Type: text/plain; charset=utf-8\r 
[Sun Aug 25 12:47:19 2013] [error] \r 
[Sun Aug 25 12:47:19 2013] [error] hello world! 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7884): Target WSGI script '/var/www/languageAnalyz/testwsgi.py' cannot be loaded as Python module. 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] mod_wsgi (pid=7884): SystemExit exception raised by WSGI script '/var/www/languageAnalyz/testwsgi.py' ignored. 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] Traceback (most recent call last): 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/testwsgi.py", line 10, in <module> 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1]  WSGIServer(app).run() 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi.py", line 112, in run 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1]  sock = self._setupSocket() 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 997, in _setupSocket 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1]  req.run() 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 572, in run 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1]  self._end(appStatus, protocolStatus) 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] File "/usr/local/lib/python2.7/dist-packages/flup/server/fcgi_base.py", line 601, in _end 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1]  sys.exit(appStatus) 
[Sun Aug 25 12:47:19 2013] [error] [client 127.0.0.1] SystemExit: 0 

Edit_ter:

好吧,我剛剛成功發射testwsgi.py。我改變我的應用程序功能到應用程序,並在結尾添加一些行:

if __name__ == '__main__': 
    from wsgiref.simple_server import make_server 
    server = make_server('localhost', 8080, application) 
    server.serve_forever() 

事件我不知道,爲什麼它的工作,爲什麼它不工作之前。 ......現在,我得到了一些問題,打開文件,與他們的路徑(配置文件或模板文件...)

[Sun Aug 25 13:10:51 2013] [error] [client 127.0.0.1] File "/var/www/languageAnalyz/analyz.py", line 22, in getStats 
[Sun Aug 25 13:10:51 2013] [error] [client 127.0.0.1]  flangs=open('config/languages.yml') 

我嘗試用絕對路徑,它沒有工作太...

+0

您可能也想看看這些問題:http://stackoverflow.com/questions/17766595/403-forbidden-error-with-django-and-mod- wsgi/28394998#28394998和http://stackoverflow.com/questions/3749331/django-with-mod-wsgi-returns-403-error – jknappen

回答

0

你似乎會跟着錯誤的文檔爲初學者。對於mod_wsgi的使用:

你似乎是使用文檔FASTCGI部署。

而且,你不能做:

import test-wsgi 

一個Python模塊名稱不能以 ' - ' 它。

總之,重新閱讀(或閱讀)有關部署的Django文檔。

+0

我甚至比以前有更多的錯誤。我得到了同樣的錯誤,並在此之前有另一個錯誤。 我編輯我的帖子。 – vekah

+0

好吧,我剛剛成功啓動了testwsgi.py。 我將我的應用程序函數改爲應用程序,並在末尾添加一些行:'if __name__ =='__main__': from wsgiref.simple_server import make_server server = make_server('localhost',8080,application) server。 serve_forever()' 我事件不知道,爲什麼它的工作,爲什麼它不工作之前。 ... 現在我得到了一些問題打開文件,其路徑(配置文件,或模板文件...) – vekah

+0

你不能使用相對路徑名,你必須使用絕對路徑名。當前的工作目錄不會在你期望的位置。請參閱http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory –

0

對於不同的文件我有同樣的問題,並使文件世界可執行文件修復它爲我。你當然會希望將其鎖定到所需的用戶,你已經縮小下來作爲您的問題後:

chmod a+x test-wsgi.py 
0

對我來說,問題是WSGI Python版本不匹配。我使用python 3,因此:

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi 
$ sudo apt-get install libapache2-mod-wsgi-py3 
相關問題