开发者社区> 问答> 正文

apache2,mod_wsgi,python Web应用程序(瓶框架)

注意:我猜这里的bottle框架不相关。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包含Bottle应用程序的东西。app.wsgi文件具有完整的可执行权限:

-rwxrwxr-x 1 tducin tducin  377 Feb  5 09:22 app.wsgi

展开
收起
祖安文状元 2020-02-22 17:45:43 958 0
1 条回答
写回答
取消 提交回答
  • 以下方法对我有用。 步骤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>
    
    

    步骤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()
    
    

    注意:导入文件时不使用.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!"
    
    ```js
    #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"
    
    输出:
    
    ```js
    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
    
    2020-02-22 17:46:22
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载