![](/img/trans.png)
[英]On vestacp, apache2, mod_wsgi, python web app (bottle framework)
[英]apache2, mod_wsgi, python web app (bottle framework)
注意:我猜瓶子框架在这里不相关。 wsgi是。
我已经设法将我的 apache 配置为与 wsgi 和基于 python 瓶子框架的单文件 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 包含瓶子应用程序的东西。 app.wsgi 文件具有完整的可执行权限:
-rwxrwxr-x 1 tducin tducin 377 Feb 5 09:22 app.wsgi
以下方法对我有用。
第一步:配置虚拟主机:
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://[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
使用 Apache/mod_wsgi,导入时不会在进程的当前工作目录中查找模块。 这是由于在嵌入式系统中使用 Python 工作时的事情。 因此,您需要告诉 mod_wsgi 应将哪些附加目录添加到 Python 模块搜索路径中。 假设附加模块所在的目录与您的 WSGI 脚本相同,请使用:
WSGIPythonPath /home/tducin/Development/Python/bottle-test/src
顺便说一句,在 WSGI 应用程序中依赖当前工作目录作为特定值并在配置文件/模板访问中使用相对路径名是不好的做法。 IOW,使用以下方法不是一个好主意:
os.chdir(os.path.dirname(__file__))
您应该始终根据配置的根目录值或从 os.path.dirname(__file__) 动态计算文件系统路径。 也就是说,就像您正在做的那样,但只需将其用作 os.chdir() 的输入,而是使用它来构造文件的正确绝对路径名。
我尝试了格雷厄姆的建议,但对我不起作用。
这是对我有用的:
[顺便说一句,我正在研究 OSX。 请根据您的操作系统调整路径、用户、组]
/Library/WebServer/Documents/hello_app/app.wsgi:
import sys
sys.path.insert(0, "/Library/WebServer/Documents/hello_app")
import bottle
import hello
application = bottle.default_app()
/Library/WebServer/Documents/hello_app/hello.py:
from bottle import route
@route('/hello')
def hello():
return "Hello World!"
/etc/apache2/extra/httpd-vhosts.conf:
<VirtualHost *:80>
ServerName xyz.com
WSGIDaemonProcess hello_app user=_www group=_www processes=1 threads=5
WSGIScriptAlias /v1 /Library/WebServer/Documents/hello_app/app.wsgi
<Directory /Library/WebServer/Documents/hello_app>
WSGIProcessGroup hello_app
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
不要忘记重新启动您的 apache 服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.