How to deploy web2py using nginx?
Asked Answered
C

2

15

web2py is an awesome python framework which has great documentation including several deployment recipes. Yet what I miss there is the recipe for deploying using nginx (preferably with uwsgi). There are some incomplete notes around the web (like here), but I couldn't find any complete, stand-alone guide.

Caylacaylor answered 17/3, 2011 at 5:16 Comment(0)
C
11

OK, looking closer into the web2py email list that I linked above, I figured out that the copmlete solution is already there. I could follow the instructions and, thanks pbreit's brilliant post, now my deployment works like a charm (using only 38MB RAM in idle state) with nginx+uwsgi.

Here are the parts that I used (I just stripped down the fabfile.py to use it on command line) Note: where there is 'put('....' I used nano text editor to create and edit files

apt-get -y install build-essential psmisc python-dev libxml2 libxml2-dev python-setuptools
cd /opt; 
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
tar -zxvf uwsgi*
mv /opt/uwsgi*/ /opt/uwsgi/
cd /opt/uwsgi/; python setup.py install
chown -R www-data:www-data /opt/uwsgi
touch /var/log/uwsgi.log
chown www-data /var/log/uwsgi.log
apt-get -y install libpcre3-dev build-essential libssl-dev
cd /opt; wget http://nginx.org/download/nginx-0.8.54.tar.gz
cd /opt; tar -zxvf nginx*
cd /opt/nginx*/; ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module
cd /opt/nginx*/; make
cd /opt/nginx*/; make install
adduser --system --no-create-home --disabled-login --disabled-password --group nginx
cp /opt/uwsgi*/nginx/uwsgi_params /opt/nginx/conf/uwsgi_params

wget https://library.linode.com/web-servers/nginx/installation/reference/init-deb.sh
mv init-deb.sh /etc/init.d/nginx

chmod +x /etc/init.d/nginx
/usr/sbin/update-rc.d -f nginx defaults
/etc/init.d/nginx start

cd /opt/
wget https://library.linode.com/web-servers/nginx/python-uwsgi/reference/init-deb.sh
mv /opt/init-deb.sh /etc/init.d/uwsgi
chmod +x /etc/init.d/uwsgi

echo 'PYTHONPATH=/var/web2py/ MODULE=wsgihandler' >> /etc/default/uwsgi
/usr/sbin/update-rc.d -f uwsgi defaults
/etc/init.d/uwsgi start


rm /opt/nginx/conf/nginx.conf
# modify nginx.conf below and save it as /opt/nginx/conf/nginx.conf
cd /opt/nginx/conf; openssl genrsa -out server.key 1024
cd /opt/nginx/conf; openssl req -batch -new -key server.key -out server.csr
cd /opt/nginx/conf; 
openssl x509 -req -days 1780 -in server.csr -signkey server.key -out server.crt

/etc/init.d/nginx restart 

nginx.conf

     user www-data;
     worker_processes  4;

     events {
         worker_connections  1024;

     }

     http {
         include       mime.types;
         default_type  application/octet-stream;
         keepalive_timeout  2;
         sendfile        on;
         #tcp_nopush     on;
         tcp_nodelay     on;
         gzip  on;
         server {
             listen       80;
             server_name  example.com www.example.com;

             location / {
                 uwsgi_pass 127.0.0.1:9001;
                 include uwsgi_params;
             }

             location /static {
                 root /var/web2py/applications/init/;
             }
         }

  # HTTPS server
     server {
         listen       443;
         server_name  www.example.com example.com;
         ssl                  on;
         ssl_certificate      /opt/nginx/conf/server.crt;
         ssl_certificate_key  /opt/nginx/conf/server.key;

         location / {
             uwsgi_pass 127.0.0.1:9001;
             include uwsgi_params;
             uwsgi_param     UWSGI_SCHEME $scheme;
         }


   location /static {
             root /var/web2py/applications/init/;
         }
     }

 }

Derived from web2py email list With the help from this Linode post

Caylacaylor answered 17/3, 2011 at 5:16 Comment(4)
mind that this config file has some path remapping... seems to assume one init app.Underweight
@massimo: what should be changed to support multiple apps?Dragging
"chown -R www-data:www-data /opt/uwsgi" is a serious security issue.Spelaean
@Spelaean - Please, some links to why that's a "serious security issue" and, more importantly, suggestions for how to better do that would be most appreciated. (NOTE: I am not disagreeing, just asking for helpful posts beyond "don't do it like that".)Microorganism
R
2

There is a solution here: http://www.web2pyslices.com/slice/show/1495/updated-uwsgi-nginx-script-for-ubuntu-1110

Reilly answered 16/7, 2012 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.