WebFaction
Community site: login faq
0
1

After weeks of tinkering I've come up with my favorite web2py configuration so I thought I'd share the deployment script for it.

This runs Web2py through uWSGI and the shared front-end Nginx server. There is no dedicated http server using up your allotted memory.

The config is set to 6 processes which will recycle themselves when they reach 42MB in size. This is designed to keep the server running within the default memory limits while maximizing capacity and memory available to web2py scripts. You may have to tweak these settings (workers and reload-on-rss) in uwsgi.conf to meet your needs. Workers * reload-on-rss + 20MB = the max amount of memory this config will consume. The extra 20MB is the uWSGI master process.

During a load test of around 9000 requests in ten minutes this config topped out at 190MB memory usage. With 8 processes at 32MB max each it started recycling processes at about 260MB memory usage going only slightly over.

This script also generates a tool to serve web2py static content outside of uWSGI. Set up a static handler app via the Webfaction documentation. After everything is set up and you've started your new web2py app for the first time, run mvstatic.sh and follow the instructions. The script will ask you for the name of a web2py app (and some other information) then move that app's static content to its own folder under the static handler.

#!/bin/sh

echo 'Install script for uwsgi (1.4.3) and web2py (latest stable) on Webfaction'

###
### Get required information
###
echo "Desired web2py admin password:"
read  web2py_password

echo "What is the name of your Webfaction application to host web2py?"
read  webfaction_app_name

echo "What is the listen port of your custom Webfaction application?"
read  webfaction_app_port

###
### Install web2py
###
cd ~/webapps/${webfaction_app_name}
wget http://www.web2py.com/examples/static/web2py_src.zip
unzip web2py_src.zip
rm web2py_src.zip
cd web2py
python -c "from gluon.main import save_password; save_password('${web2py_password}',${webfaction_app_port})"
cd ..

###
### Install uWSGI
###
mkdir downs
mkdir uwsgi
cd downs

wget http://projects.unbit.it/downloads/uwsgi-1.4.3.tar.gz
tar xvf uwsgi-1.4.3.tar.gz

cd uwsgi-1.4.3
make -f Makefile
cp uwsgi ../../uwsgi/

cd ../../
rm -rf downs

##
## Write scripts and config
##
mkdir uwsgi/tmp

# uWSGI ini config
echo "[uwsgi]
http-socket = 127.0.0.1:${webfaction_app_port}
pythonpath = /home/${USER}/webapps/${webfaction_app_name}/web2py
daemonize = /home/${USER}/webapps/${webfaction_app_name}/uwsgi/tmp/uwsgi.log
pidfile = /home/${USER}/webapps/${webfaction_app_name}/uwsgi/tmp/uwsgi.pid
module = wsgihandler
workers = 6
reload-on-rss = 42
no-orphans = true
auto-procname = true
procname-prefix = web2py-
" > uwsgi/uwsgi.conf

# Server start/stop/reload script
echo "#!/bin/bash
if [ "\$1" = 'start' ]; then
    /home/${USER}/webapps/${webfaction_app_name}/uwsgi/uwsgi --ini /home/${USER}/webapps/${webfaction_app_name}/uwsgi/uwsgi.conf
elif [ "\$1" = 'stop' ]; then
    /home/${USER}/webapps/${webfaction_app_name}/uwsgi/uwsgi --stop /home/${USER}/webapps/${webfaction_app_name}/uwsgi/tmp/uwsgi.pid 
elif [ "\$1" = 'reload' ]; then
    /home/${USER}/webapps/${webfaction_app_name}/uwsgi/uwsgi --reload /home/${USER}/webapps/${webfaction_app_name}/uwsgi/tmp/uwsgi.pid 
else
    echo \"No command specified (start/stop/reload)\"
fi
" > server
chmod +x server

# Script to relocate a web2py app's static directory to a seperate static handler app
echo "#!/bin/sh
echo \"This script will move all files from an existing web2py application's\"
echo \"static directory into a static app to serve static content more efficiently.\"
echo \"You must create a static app according to the Webfaction docs before running this script.\"
echo \" \"
echo \"***BACK UP YOUR WEB2PY APP BEFORE RUNNING THIS SCRIPT!***\"

echo \"What is the name of your web2py app with static content?\"
read  web2py_app

echo \"What is the name of your Webfaction application hosting web2py?\"
read  webfaction_web2py_app

echo \"What is the name of your Webfaction static application?\"
read  webfaction_static_app

mkdir /home/\${USER}/webapps/\${webfaction_static_app}/\${web2py_app}

mv /home/\${USER}/webapps/\${webfaction_web2py_app}/web2py/applications/\${web2py_app}/static/* /home/\${USER}/webapps/\${webfaction_static_app}/\${web2py_app}

rmdir /home/\${USER}/webapps/\${webfaction_web2py_app}/web2py/applications/\${web2py_app}/static

# create symlink from web2py app static directory so static files created within the app will land in the static app directory
ln -s /home/\${USER}/webapps/\${webfaction_static_app}/\${web2py_app} /home/\${USER}/webapps/\${webfaction_web2py_app}/web2py/applications/\${web2py_app}/static

echo \"All static content for \${web2py_app} is now being served from the /home/\${USER}/webapps/\${webfaction_static_app}/\${web2py_app} directory.\"
" > mvstatic.sh
chmod +x mvstatic.sh

echo "To start uWSGI with web2py run './server start'"
echo "from the ~/webapps/${webfaction_app_name} directory."
echo "You can also stop and reload the server using"
echo "'./server stop' and './server reload' respectively."
echo " "
echo "To serve static content through another server create a"
echo "second Webfaction app according to the documentation"
echo "on serving static content seperately"
echo "Run the 'mvstatic.sh' script and follow the instructions"
echo "to have your web2py app's static content served from the static app."

asked 29 Dec '12, 23:56

HittingSmoke
4822030
accept rate: 8%

edited 30 Dec '12, 01:57

Great; thanks for sharing!

(30 Dec '12, 00:04) ryans ♦♦
Be the first one to answer this question!
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:

×186
×24
×17

question asked: 29 Dec '12, 23:56

question was seen: 3,342 times

last updated: 30 Dec '12, 01:57

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