How to configure apache server to host bottle python app via mod_wsgi

1,029 views
Skip to first unread message

Jo Grey

unread,
Jun 9, 2016, 10:16:21 PM6/9/16
to bottlepy

For background, I am really new to setting up servers via ssh and the command line so please excuse my inexperience.

I am trying to build a basic app using the bottle (bottlepy) micro framework, I want to host the app using httpd apache server running on centOS 6.6

So far I have been able to get httpd to serve a basic index.html file from the /var/www/html directory. So the apache server is confirmed up and running.

Where I am stuck is how to serve the bottle app with httpd. I read the deployment documentation on the bottlepy site but they skip over specific details. I have tried a few permutations based off other docs I have found, but nothing has worked so I am looking for some really explicit suggestions on what to do here.

At a high level I understand that what has to happen is:

  1. alter the httpd configuration so that it loads the app.wsgi file
  2. write an app.wsgi file that loads the python application file app.py

First off, is that correct? Or does the app.wsgi file "replace" the app.py file? As in do I just change the file extension and drop into the /var/www directory? That doesn't seem right, but what do I know.

So my three questions are as follows:

  1. httpd config:
    • Where *exactly do I put the virtualhost configuration for httpd at? 
    • Does it go inside the /etc/httpd/conf/httpd.conf file, and if so, where at in the file does it go? 
    • Or does it go in its own file, and if so, where do I put that file, what should I name it, and how will httpd know to read it?

This is the config info I have, based off the bottle docs (server is apache 2.2):

<VirtualHost *>
   ServerName test_server

   WSGIDaemonProcess app.wsgi user=www-data group=www-data processes=1 threads=5
   WSGIScriptAlias / /var/www/my_app/app.wsgi

   <Directory /var/www/my_app>
       WSGIProcessGroup app.wsgi
       WSGIApplicationGroup %{GLOBAL}
       Order deny,allow
       Allow from all
   </Directory>
</VirtualHost>
  1. app.wsgi file:

    • What should this file contain to get it to load my python app.py file?
  2. app.py file:

    • Where do I put my python app.py file? Or is this explicitly defined in the app.wsgi file so it can be put anywhere?
    • How do I make sure the python code is actually executed and not just read as a text file?

Thanks in advance.

Tim Arnold

unread,
Jun 10, 2016, 4:27:32 PM6/10/16
to bottlepy
This is what works for me. I don't use VirtualHost, but these lines are the end of my httpd.conf
-------------------------------------------
WSGIPassAuthorization On
WSGIScriptAlias /service /path/to/bottle_adapter.wsgi
WSGIDaemonProcess myhost user=www group=www processes=5 threads=25 home=/path/to/logs
#
<Directory />
Order deny,allow
Allow from all
</Directory>
-------------------------------------------
so requests with urls starting with 'service' will be shunted to the bottle_adapter.wsgi file, which is plain python:
import bottle
application = bottle.default_app()
etc, etc, set up routes, views, per the bottle documentation.

so apache actually calls that wsgi file which is executed. And if you have your routes set up in there, it will serve whatever those routes return. 

See the setup section of this article:


good luck,
--Tim

John Stile

unread,
Jun 13, 2016, 12:57:28 PM6/13/16
to bott...@googlegroups.com
This works for me, but I would like to know if I am following best practices.

I am using
  apache-2.4.18 (slightly different allow rule than 2.2.x) w/ virtual hosts
  python 2.7
  virtualenv
  Gentoo distribution.

Virtual host file includes my include file
/etc/apache2/vhosts.d/00_default_vhost.conf

<IfDefine DEFAULT_VHOST>
<VirtualHost *:80>
        ServerName localhost
        Include /etc/apache2/confs/project_bottle.include
        <IfModule mpm_peruser_module>
                ServerEnvironment apache apache
        </IfModule>
</VirtualHost>
</IfDefine>

The content of /etc/apache2/confs/project_bottle.include

<IfDefine WSGI>
        WSGIScriptAlias  /bottle /var/www/localhost/wsgi/project_bottle/myproject.wsgi
        <Directory "/var/www/localhost/wsgi/project_bottle">
            AllowOverride None
            Options None
            Require all granted
        </Directory>
</IfDefine>


This file loads the module:   /etc/apache2/modules.d/70_mod_wsgi.conf

<IfDefine WSGI>
LoadModule wsgi_module modules/mod_wsgi.so
</IfDefine>

In gentoo, I add APACHE2_OPTS to support the virtual hosts and wsgi

Content of: /etc/conf.d/apache2

APACHE2_OPTS="-D DEFAULT_VHOST -D INFO -D SSL -D SSL_DEFAULT_VHOST -D LANGUAGE -D PROXY -D WSGI"

In gentoo, since WSGI is defined, the module is loaded and my config is read.

Setting up the virtualenv, I have a text file (pip_requirements.txt) containing:

Beaker==1.8.0
bottle==0.12.9
docutils==0.12
funcsigs==1.0.2
lockfile==0.12.2
pid==2.0.1
python-daemon==2.1.1
python-pid==2.2.0
wheel==0.24.0

To create the virtual environment, I run:

mkdir -p /var/www/localhost/wsgi/project_bottle
pushd !$
virtualenv --python=python2.7 venv
. venv/bin/activate
pip install -r pip_requirements.txt


Content of  /var/www/localhost/wsgi/project_bottle/myproject.wsgi

import os
import sys
# Apache base for the app
here = os.path.dirname(__file__)
# Activate virtualenv
activate_this = os.path.join( here, 'venv', 'bin','activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
# Add to python path
sys.path.insert(0,here)
# Not sure about this one
sys.stdout = sys.stderr
# Base framework
import bottle
# My app
import hello
# Instantiate the application for mod_wsgi
application = bottle.default_app()


Content of hello.py

from bottle import route, run, template
@route('/hello')
def hello():
    return "Hello World!"
@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)


When I go here:
http://localhost/bottle/hello

I see:

Hello World!

When I go here:
http://localhost/bottle/hello/john

I see:

Hello john!
--
--
You are member of the "bottlepy" group at google groups.
See http://groups.google.de/group/bottlepy for mailing list options.
See http://bottlepy.org/ for news and documentation.

---
You received this message because you are subscribed to the Google Groups "bottlepy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottlepy+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages