apache2, mod_wsgi, python web app (bottle framework)

后端 未结 3 1648
一向
一向 2021-01-16 12:07

Note: I guess the bottle framework is not relevant here. Wsgi is.

I\'ve managed to configure my apache to work with wsgi and one-file web applicatio

相关标签:
3条回答
  • 2021-01-16 12:41

    I tried Graham's suggestion but it didn't work for me.

    Here is what worked for me:
    [BTW, I am working on OSX. Please adjust the paths, user, group according to your operating system]

    /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>
    

    Don't forget to restart your apache server.

    Check the app in the web browser

    0 讨论(0)
  • 2021-01-16 12:46

    The following approach worked for me.
    step 1: Configure the virtual Host:
    Linux Path : /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>
    

    Step 2: configure app.wsgi file.
    path: /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()
    

    Note: Import a file without using .py extension. (import hello_world)

    Step 3: Create hello_world.py
    Path : /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)
    

    step 4: Restart your apache server & test your api with Curl:
    $ curl -i GET "http://[hostname]/hello"

    Output:

    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
    
    0 讨论(0)
  • 2021-01-16 12:49

    Using Apache/mod_wsgi, it does not look in the current working directory of the process for modules when imported. This is by virtue of how things when work Python is used in embedded systems. You thus need to tell mod_wsgi which additional directories should be added into the Python module search path. Assuming the directory where the additional modules is is the same as your WSGI script, use:

    WSGIPythonPath /home/tducin/Development/Python/bottle-test/src
    

    BTW, it is bad practice in WSGI applications to rely on the current working directory being a specific value and use relative path names in config file/template access. IOW, it is not a good idea to use:

    os.chdir(os.path.dirname(__file__))
    

    You should always calculate file system paths dynamically based on a configured root directory value or from os.path.dirname(__file__). That is, like you are doing, but just use it as input to os.chdir(), use it to construct proper absolute path names for files instead.

    0 讨论(0)
提交回复
热议问题