WebFaction
Community site: login faq
1
1

Does anyone have a guide for installing Apache 2.4.2 with the latest version of mod_wsgi 3.3?

It would also be convenient if the instructions included an easy way to set the installation prefix and python version.

asked 04 May '12, 22:09

ryans ♦♦
5.0k93159
accept rate: 43%


Introduction

The WebFaction Control Panel has a one-click installer for mod_wsgi. Under most circumstances, this will do what you need.

However, in some cases you do need to build one from source - for example, when you want to use the Prefork MPM. So, here is an installation guide for Apache 2.4.2 with mod_wsgi 3.3.1. Newer versions should be similar.

You can run these commands individually, or you can paste them into a shell script and run it as

bash -e build_modwsgi-3.3.sh | tee build_modwsgi-3.3.log

Either way, you will want to run it in a screen session.

You will want to build this within a Custom Application (listening on port). In this example, I am using app_name to represent the name of this application, which is assumed to have been created via the Control Panel. PORT should be set to the assigned port in the Control Panel applications list.

You can edit the installation prefix, port, and python versions by modifying the first three lines of the script.

Installation Procedure

cd ~/webapps/app_name
export PORT="77777"
export PYTHON="python2.7"


# Internal path variables
APP_PREFIX="$PWD"
APP_NAME="$(basename $APP_PREFIX)"
APACHE_INSTALL_PREFIX="$APP_PREFIX/apache2"
PYTHON_INSTALL_PREFIX="$($PYTHON -c 'import sys; print(sys.prefix)')"

# set up standard build environment
mkdir -p $APACHE_INSTALL_PREFIX/{src,tmp}
export TMPDIR="$APACHE_INSTALL_PREFIX/tmp"
export PATH="$APACHE_INSTALL_PREFIX/bin:$PATH"
export C_INCLUDE_PATH="$APACHE_INSTALL_PREFIX/include:$C_INCLUDE_PATH"
export LIBRARY_PATH="$APACHE_INSTALL_PREFIX/lib:$LIBRARY_PATH"
export LD_LIBRARY_PATH="$APACHE_INSTALL_PREFIX/lib:$LD_LIBRARY_PATH"
export CPPFLAGS="-I$APACHE_INSTALL_PREFIX/include $CPPFLAGS"
export LDFLAGS="-L$APACHE_INSTALL_PREFIX/lib $LDFLAGS"
export PKG_CONFIG_PATH="$APACHE_INSTALL_PREFIX/lib/pkgconfig"
mkdir -p $APACHE_INSTALL_PREFIX/lib/${PYTHON}
mkdir -p $APACHE_INSTALL_PREFIX/lib/perl5
export PERL5LIB="$APACHE_INSTALL_PREFIX/lib/perl5"
export CFLAGS="-O2 -pipe"

# fix lib64
if [ ! -L "$APACHE_INSTALL_PREFIX/lib64" ]; then ln -s $APACHE_INSTALL_PREFIX/lib $APACHE_INSTALL_PREFIX/lib64; fi

###########################################################
# APR 1.4.6
# original: http://newverhost.com/pub//apr/apr-1.4.6.tar.bz2
###########################################################
cd $APACHE_INSTALL_PREFIX/src
wget 'http://mirror.ryansanden.com/modwsgi-5a98762c/apr-1.4.6.tar.bz2'
tar -xf apr-1.4.6.tar.bz2
cd apr-1.4.6
./configure --prefix=$APACHE_INSTALL_PREFIX
make
make install

###########################################################
# APR-Util 1.4.1
# original: http://newverhost.com/pub//apr/apr-util-1.4.1.tar.bz2
###########################################################
cd $APACHE_INSTALL_PREFIX/src
wget 'http://mirror.ryansanden.com/modwsgi-5a98762c/apr-util-1.4.1.tar.bz2'
tar -xf apr-util-1.4.1.tar.bz2
cd apr-util-1.4.1
./configure --prefix=$APACHE_INSTALL_PREFIX --with-apr=$APACHE_INSTALL_PREFIX
make
make install

###########################################################
# Apache 2.4.2
# original: http://www.apache.org/dist/httpd/httpd-2.4.2.tar.bz2
###########################################################
cd $APACHE_INSTALL_PREFIX/src
wget 'http://mirror.ryansanden.com/modwsgi-5a98762c/httpd-2.4.2.tar.bz2'
tar -xf httpd-2.4.2.tar.bz2
cd httpd-2.4.2
./configure --prefix=$APACHE_INSTALL_PREFIX \
            --enable-mods-shared=all \
            --enable-mpms-shared=all \
            --with-mpm=worker \
            --with-apr=$APACHE_INSTALL_PREFIX \
            --with-apr-util=$APACHE_INSTALL_PREFIX
make
make install

###########################################################
# Mod_WSGI 3.3
# original: hg repository: https://code.google.com/p/modwsgi/ branch mod_wsgi-3.X revision 1209:5a98762c71db
###########################################################
cd $APACHE_INSTALL_PREFIX/src
wget 'http://mirror.ryansanden.com/modwsgi-5a98762c/modwsgi-5a98762c.tar.gz'
tar -xf modwsgi-5a98762c.tar.gz
cd modwsgi-5a98762c
./configure --with-apxs=$APACHE_INSTALL_PREFIX/bin/apxs \
            --with-python=$(which $PYTHON)
make
make install

###########################################################
# Create apache "start", "stop", and "restart" scripts
###########################################################
cd $APACHE_INSTALL_PREFIX/bin

cat << EOF > start
#!/bin/bash
MYDIR="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
export LD_LIBRARY_PATH="$PYTHON_INSTALL_PREFIX/lib:$APACHE_INSTALL_PREFIX/lib:$APP_PREFIX/lib"
exec "\$MYDIR/apachectl" start
EOF

cat << EOF > stop
#!/bin/bash
MYDIR="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
export LD_LIBRARY_PATH="$PYTHON_INSTALL_PREFIX/lib:$APACHE_INSTALL_PREFIX/lib:$APP_PREFIX/lib"
exec "\$MYDIR/apachectl" stop
EOF

cat << EOF > restart
#!/bin/bash
MYDIR="\$( cd "\$( dirname "\${BASH_SOURCE[0]}" )" && pwd )"
export LD_LIBRARY_PATH="$PYTHON_INSTALL_PREFIX/lib:$APACHE_INSTALL_PREFIX/lib:$APP_PREFIX/lib"
exec "\$MYDIR/apachectl" stop
sleep 3
exec "\$MYDIR/apachectl" start
EOF

chmod 755 start stop restart

###########################################################
# Create httpd.conf
###########################################################
cd $APACHE_INSTALL_PREFIX/conf
mv httpd.conf httpd.conf.original

cat << "EOF" > httpd.conf
ServerRoot "PLACEHOLDER_APACHE_INSTALL_PREFIX"

LoadModule mpm_event_module  modules/mod_mpm_event.so

LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dir_module        modules/mod_dir.so
LoadModule env_module        modules/mod_env.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule mime_module       modules/mod_mime.so
LoadModule rewrite_module    modules/mod_rewrite.so
LoadModule setenvif_module   modules/mod_setenvif.so
LoadModule wsgi_module       modules/mod_wsgi.so
LoadModule unixd_module      modules/mod_unixd.so

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog PLACEHOLDER_HOME/logs/user/access_PLACEHOLDER_APP_NAME.log combined
ErrorLog /home/testweb500/logs/user/error_PLACEHOLDER_APP_NAME.log

DirectoryIndex index.py
DocumentRoot PLACEHOLDER_APP_PREFIX/htdocs

Listen PLACEHOLDER_PORT
KeepAlive Off
SetEnvIf X-Forwarded-SSL on HTTPS=1
ServerLimit 1
StartServers 1
MaxRequestWorkers 5
MinSpareThreads 1
MaxSpareThreads 3
ThreadsPerChild 5

WSGIDaemonProcess PLACEHOLDER_APP_NAME processes=2 threads=12 python-path=PLACEHOLDER_APP_PREFIX/lib/python2.7 
WSGIProcessGroup PLACEHOLDER_APP_NAME
WSGIRestrictEmbedded On
WSGILazyInitialization On

<Directory PLACEHOLDER_APP_PREFIX/htdocs>
    Options +ExecCGI
    AddHandler wsgi-script .py
</Directory>
EOF

sed -i "s^PLACEHOLDER_APACHE_INSTALL_PREFIX^${APACHE_INSTALL_PREFIX}^g" httpd.conf
sed -i "s^PLACEHOLDER_APP_PREFIX^${APP_PREFIX}^g" httpd.conf
sed -i "s^PLACEHOLDER_APP_NAME^${APP_NAME}^g" httpd.conf
sed -i "s^PLACEHOLDER_HOME^${HOME}^g" httpd.conf
sed -i "s^PLACEHOLDER_PORT^${PORT}^g" httpd.conf

###########################################################
# Create index.py
###########################################################
mkdir -p $APP_PREFIX/htdocs
cd $APP_PREFIX/htdocs

cat << "EOF" > index.py
import sys

def application(environ, start_response):
    output = 'Welcome to your mod_wsgi website! It uses:\n\nPython %s' % sys.version
    output += '\nWSGI version: %s' % str(environ['mod_wsgi.version'])

    response_headers = [
        ('Content-Length', str(len(output))),
        ('Content-Type', 'text/plain'),
    ]

    start_response('200 OK', response_headers)

    return [output]
EOF

# Start the app
$APACHE_INSTALL_PREFIX/bin/start
permanent link
This answer is marked "community wiki".

answered 04 May '12, 22:20

ryans ♦♦
5.0k93159
accept rate: 43%

edited 10 May '16, 05:33

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×224
×55

question asked: 04 May '12, 22:09

question was seen: 7,487 times

last updated: 10 May '16, 05:33

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM