注意:我想瓶子框架在这里不相关。 Wsgi 是。
我已经设法配置我的 apache 以使用 wsgi 和基于 python bottle 框架的单文件 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>
httpd.conf(这是我的 virtualenv 所在的位置):
WSGIPythonHome /home/tducin/Development/Python/bottle-test
最后,这是 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
我想做的是将 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
问题是现在我得到错误 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
src/server.py 包含 bottle 应用程序的东西。 app.wsgi 文件获得了完整的可执行权限:
-rwxrwxr-x 1 tducin tducin 377 Feb 5 09:22 app.wsgi
请您参考如下方法:
以下方法对我有用。
第 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>
第二步:配置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()
注意:导入文件时不使用 .py 扩展名。 (导入 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)
第 4 步:重启您的 Apache 服务器并使用 Curl 测试您的 API:
$ curl -i GET "http://[主机名]/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
