Installing Web2py with Nginx and Uwsgi on Centos 5 is really tricky. There 
are lots of subtleties of ownership, and one has to take care when 
installing python 2.6 not to stop the systems python2.4 from working. I 
have finally developed a script that does all the installation from a clean 
start machine. The only thing that should need changing for another 
installation is the $basearch (base architecture) of the machine. This can 
be determined by doing 'uname -i'. This is needed for the nginx 
installation.

 

 

--------------------------------------------------------------------------------------------

 

# install development tools

yum install gcc gdbm-devel readline-devel ncurses-devel zlib-devel  
bzip2-devel sqlite-devel db4-devel openssl-devel tk-devel bluez-libs-devel

 

#Install python 2.6 without overwriting python 2.4

#=================================

VERSION=2.6.8

mkdir ~/src

chmod 777 ~/src

cd ~/src

wget http://www.python.org/ftp/python/$VERSION/Python-$VERSION.tgz

tar xvfz Python-2.6.8.tgz

cd Python-2.6.8

./configure --prefix=/opt/python2.6 --with-threads --enable-shared

make

 

#The altinstall ensures that python2.4 is left okay

make altinstall

echo "/opt/python2.6/lib">/etc/ld.so.conf.d/opt-python2.6.conf

ldconfig

 

#create alias so that python 2.6 can be run with 'python2.6'

alias -p python2.6="/opt/python2.6/bin/python2.6"

ln -s /opt/python2.6/bin/python2.6 /usr/bin/python2.6

 

# Install uwsgi

#=========

version=uwsgi-1.2.3

cd /opt/

wget http://projects.unbit.it/downloads/$version.tar.gz

tar -zxvf $version.tar.gz

mv $version/ uwsgi/

cd uwsgi/

 

# build using python 2.6

python2.6 setup.py build

python2.6 uwsgiconfig.py --build

useradd uwsgi

 

#create and own uwsgi log

# Note this log will need emptying from time to time

echo " ">/var/log/uwsgi.log

chown uwsgi /var/log/uwsgi.log

 

#Install web2py

#==========

cd /opt

mkdir web-apps

cd web-apps

wget http://www.web2py.com/examples/static/web2py_src.zip

unzip web2py_src.zip

 

#set the ownership for web2py application to uwsgi

cd web2py 

 chown -R uwsgi applications

chmod -R u+wx applications

 

#Now install nginx

#============

cd /etc/yum.repos.d

echo "[nginx]">nginx.repo

 

# in the line below replace 'i386' with the $basearch for your machine.

echo "baseurl=http://nginx.org/packages/centos/5/i386/";>>nginx.repo

echo "gpgcheck=0">>nginx.repo

echo "enabled=1">>nginx.repo

yum install nginx

 

#We don't want the defaults, so remove them

cd /etc/nginx/conf.d

mv default.conf default.conf.o

mv example_ssl.conf example_ssl.conf.o

----------------------------------------------------------------------------------------------------------------

 

The following configuration files are also needed

 

The options for uwsgi are in the following file. It should be placed in 
/etc/uwsgi. Other options could be included.

 

uwsgi_for_nginx.conf

 

[uwsgi]

uuid=uwsgi

pythonpath = /opt/web-apps/web2py

module = wsgihandler

socket=127.0.0.1:9001

harakiri 60

harakiri-verbose

enable-threads

daemonize = /var/log/uwsgi.log

------------------------------------------------------------------------ 

The next configuration file is for nginx, and goes in /etc/nginx/conf.d It 
serves the static diretory of applications directly. I have not set up ssl 
because I access web2py admin by using ssh tunneling and the web2py rocket 
server. It should be straightforward to set up the ssl server however.

 

web2py.conf

 

server {

listen 80;

server_name $hostname;

location ~* /(\w+)/static/ {

root /opt/web-apps/web2py/applications/;

}

location / {

uwsgi_pass 127.0.0.1:9001;

include uwsgi_params;

}

}

#server {

#listen 443;

#server_name $hostname;

#ssl on;

#ssl_certificate /etc/nginx/ssl/web2py.crt;

#ssl_certificate_key /etc/nginx/ssl/web2py.key;

#location 

#uwsgi_pass 127.0.0.1:9001;

#include uwsgi_params;

#uwsgi_param UWSGI_SCHEME $scheme;

#}

#}

 

 ------------------------------------------------------------------------

The final configuration file is only needed if you want to run uwsgi as a 
service. It should be placed in /etc/init.d

 

uwsgi_nginx

 

#!/bin/bash

 

# uwsgi - Use uwsgi to run python and wsgi web apps.

#

# chkconfig: - 85 15

# description: Use uwsgi to run python and wsgi web apps.

# processname: uwsgi

 

PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/opt/uwsgi/uwsgi

PYTHONPATH=/opt/web-apps/web2py

MODULE=wsgihandler

OWNER=uwsgi

 

NAME=uwsgi

DESC=uwsgi

 

test -x $DAEMON || exit 0

 

# Include uwsgi defaults if available

if [ -f /etc/default/uwsgi ] ; then

              . /etc/default/uwsgi

fi

 

set -e

 

get_pid() {

    if [ -f /var/run/$NAME.pid ]; then

        echo `cat /var/run/$NAME.pid`

    fi 

}   

DAEMON_OPTS="--ini-paste /etc/uwsgi/uwsgi_for_nginx.conf"

 

case "$1" in 

  start)

              echo -n "Starting $DESC: "

        PID=$(get_pid)

              echo "$PID"

        echo "B"

        if [ -z "$PID" ]; then

            echo "C"

            [ -f /var/run/$NAME.pid ] && rm -f /var/run/$NAME.pid

            echo "D"

            echo "$PID"

 

            touch 
/var/run/$NAME.pid                                         

            chown $OWNER /var/run/$NAME.pid

                  su - $OWNER -pc "$DAEMON $DAEMON_OPTS"

                  echo "$NAME."

                  echo "$PID"

        fi

 

              ;;

  stop)

              echo -n "Stopping $DESC: "

              pkill $NAME

#        kill -9 `ps -fu uwsgi | awk 'NR != 1 {print $2}'`

              ;;

  reload)

        echo "Reloading $NAME" 

        PID=$(get_pid)

        [ ! -z "$PID" ] && kill -s 1 $PID &> /dev/null

        if [ $? -gt 0 ]; then

            echo "was not running" 

            exit 1

        else 

                  echo "$NAME."

            rm -f /var/run/$NAME.pid &> /dev/null

        fi

              ;;

  force-reload)

        echo "Reloading $NAME" 

        PID=$(get_pid)

        [ ! -z "$PID" ] && kill -s 15 $PID &> /dev/null

        if [ $? -gt 0 ]; then

            echo "was not running" 

            exit 1

        else 

                  echo "$NAME."

            rm -f /var/run/$NAME.pid &> /dev/null

        fi

        ;;

  restart)

        $0 stop

        sleep 2

        $0 start

              ;;

  status)  

              killall -10 $DAEMON

              ;;

      *)  

                  N=/etc/init.d/$NAME

                  echo "Usage: $N 
{start|stop|restart|reload|force-reload|status}" >&2

                  exit 1

                  ;;

    esac

    exit 0

 

You can test it by 

 

/etc/init.d/uwsgi_nginx start

 

and stop it similarly.

 

To add it as a service do

 

chkconfig --add uwsgi_nginx

chkconfig uwsgi_nginx o

You can test this with

 

service uwsgi_nginx start

 

nginx has automatically been set up as a service

if you now start it with

 

service nginx start

 

you should find the web2py welcome app will be displayed at your web 
address.

 

As they are both services, they should automatically start on a system 
reboot.

 

If you already had a server running, such as apache, you would need to stop 
that and turn its service off before running nginx.

 

 

 

 

 

 

 

 

 

 
 ------------------------------
 
No virus found in this message.
Checked by AVG - www.avg.com
Version: 2012.0.2177 / Virus Database: 2433/5067 - Release Date: 06/13/12

Reply via email to