[Web design] Deploy web2py server with Nginx in Debian

Web2py is a great python web platform. However I don’t find any article on how to integrate it to Nginx. I also found a few issues that were not that easy to solve.

Web2py doesn’t need to be installed. Simple download the latest version.

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

Install uwsgi by:

apt-get install uwsgi
apt-get install uwsgi-plugin-python

Add a setting profile to /etc/uwsgi/apps-available/web2py.xml

<uwsgi>
    <socket>/tmp/web2py.socket</socket>
    <pythonpath>/www/web2py/</pythonpath>
    <mount>/=wsgihandler:application</mount>
    <master/>
    <processes>1</processes>
    <harakiri>60</harakiri>
    <reload-mercy>8</reload-mercy>
    <cpu-affinity>1</cpu-affinity>
    <stats>/tmp/stats.socket</stats>
    <max-requests>2000</max-requests>
    <limit-as>512</limit-as>
    <reload-on-as>256</reload-on-as>
    <reload-on-rss>192</reload-on-rss>
    <uid>nginx</uid>
    <gid>nginx</gid>
    <no-orphans/>
    <plugins>python</plugins>
</uwsgi>

make a link to app-enable:

ln -s /etc/uwsgi/apps-available/web2py.xml /etc/uwsgi/apps-enabled/

restart uwsgi service:

service uwsgi restart

Modify nginx vhost file, adding the following to server section:

        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
            alias /www/web2py/applications/$1/static/$2;
            #remove next comment on production
            #expires max;
        }
        location / {
            root /www/web2py/applications/;
            uwsgi_pass      unix:///tmp/web2py.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
        }

change the folder name as you need.
Go to /www/web2py directory and set the password for 80 port, change owner of the folder to nginx:

chown -R nginx:nginx /www/web2py
cd /www/web2py
sudo -u nginx python -c "from gluon.main import save_password; save_password(raw_input('admin password: '),80)"

till now, you should be able to visit the default welcome project by the top level domain.

If you need to deploy the project at a subdirectory, you need to change the nginx settings to:

        location ~* ^/(\w+)/static(?:/_[\d]+\.[\d]+\.[\d]+)?/(.*)$ {
            alias /www/web2py/applications/$1/static/$2;
            #remove next comment on production
            #expires max;
        }
        location /app/ {
            root /www/web2py/applications/;
            uwsgi_pass      unix:///tmp/web2py.socket;
            include         uwsgi_params;
            uwsgi_param     UWSGI_SCHEME $scheme;
            uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
            uwsgi_param SCRIPT_NAME /app;
            uwsgi_modifier1 30;
        }

Web2py is located at http://yourdomain.com/app now.
Due to the security reason, by default, web2py doesn’t allow you to connect from remote. To change this, you need to modify the file applications/admin/model/access.py by adding

request.is_local=True

before

if request.is_https:
    session.secure()
elif not request.is_local and not DEMO_MODE:
    raise HTTP(200, T('Admin is disabled because insecure channel'))
« »

Leave a Reply

Your email address will not be published. Required fields are marked *