apache2,mod_wsgi,python web app(瓶子框架)

duc*_*cin 1 python apache mod-wsgi

注意:我想瓶子框架在这里不相关.Wsgi是.

我已经设法将我的apache配置为使用基于python bottle框架的wsgi和单文件Web应用程序.下面的文件是我现在所拥有的 - apache使用virtualenv并运行包含所有内容的单个wsgi/py文件.

虚拟主机:

<VirtualHost *:80>
    ServerName bottle-test

    WSGIDaemonProcess bottle-test user=www-data group=www-data processes=1 threads=5
    WSGIScriptAlias / /home/tducin/Development/Python/bottle-test/src/app.wsgi

    <Directory /home/tducin/Development/Python/bottle-test/src>
        WSGIProcessGroup bottle-test
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
    ErrorLog /var/log/apache2/wsgi/error-bottle.log
    CustomLog /var/log/apache2/wsgi/access-bottle.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

httpd.conf(这是我的virtualenv所在的地方):

WSGIPythonHome /home/tducin/Development/Python/bottle-test
Run Code Online (Sandbox Code Playgroud)

最后,这是app.wsgi:

import os

# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))

import bottle

app = bottle.Bottle()

@app.route('/')
def siema():
    return bottle.template('<h1>SIEMA {{arg}}!</h1>', arg='Janie')

@app.route('/hello/<name>')
def hello(name):
    return bottle.template('<b>Hello {{name}}</b>!', name=name)

application = app
Run Code Online (Sandbox Code Playgroud)

我想要做的是将wsgi层与应用程序的其余部分分开.我已经尝试过几次从另一个文件导入应用程序,但我Failed to import module每次都收到错误.有谁知道如何将wsgi与应用程序分开?


编辑:我将httpd.conf移动到wsgi.conf(它已加载),现在我有以下内容:

WSGIPythonHome /home/tducin/Development/Python/bottle-test
WSGIPythonPath /home/tducin/Development/Python/bottle-test/src
Run Code Online (Sandbox Code Playgroud)

问题是,现在我有错误500,apache错误日志是:

[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1] mod_wsgi (pid=4260): Target WSGI script '/home/tducin/Development/Python/bottle-test/src/app.wsgi' cannot be loaded as Python module.
[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1] mod_wsgi (pid=4260): Exception occurred processing WSGI script '/home/tducin/Development/Python/bottle-test/src/app.wsgi'.
[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1] Traceback (most recent call last):
[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1]   File "/home/tducin/Development/Python/bottle-test/src/app.wsgi", line 7, in <module>
[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1]     import server
[Wed Feb 05 09:24:32 2014] [error] [client 127.0.0.1] ImportError: No module named server
Run Code Online (Sandbox Code Playgroud)

src/server.py包含瓶子应用程序的东西.app.wsgi文件具有完整的可执行权限:

-rwxrwxr-x 1 tducin tducin  377 Feb  5 09:22 app.wsgi
Run Code Online (Sandbox Code Playgroud)

H. *_* U. 7

以下方法对我有用.
步骤1:配置虚拟主机:
Linux路径:/ etc/apache2/sites-available/default

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    WSGIDaemonProcess my_working_dir user=www-data group=www-data
    WSGIScriptAlias /my_working_dir /var/www/my_working_dir/app.wsgi

    <Directory /var/www/my_working_dir>
               WSGIProcessGroup my_working_dir
               WSGIApplicationGroup %{GLOBAL}
               Order deny,allow
               Allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

第2步:配置app.wsgi文件.
路径:/var/www/my_working_dir/app.wsgi

import sys, os

# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))

# ... build or import your bottle application here ...
# Do NOT use bottle.run() with mod_wsgi

import bottle
from rs import app as application
from bottle import route
import hello_world
application=bottle.default_app()
Run Code Online (Sandbox Code Playgroud)

注意:导入文件时不使用.py扩展名.(import hello_world)

第3步:创建hello_world.py
路径:/var/www/my_working_dir/hello_world.py

from bottle import route, run

@route('/hello')
def hello():
    return "Hello World!"

#Comment out the localhost part, to test Apache configuration. 
#run(host='localhost', port=8080, debug=True)
Run Code Online (Sandbox Code Playgroud)

第4步:重新启动Apache服务器并使用Curl测试您的api:
$ curl -i GET "http://[hostname]/hello"

输出:

HTTP/1.1 200 OK
Date: Thu, 06 Aug 2015 21:51:54 GMT
Server: Apache/2.2.22 (Ubuntu)
Content-Length: 12
Content-Type: text/html; charset=UTF-8
Run Code Online (Sandbox Code Playgroud)