WebFaction
Community site: login faq
2
3

How do I install Nginx 1.2.3 with uWSGI 1.2 for serving a Django application?

asked 19 Aug '12, 01:33

ryans ♦♦
5.0k73159
accept rate: 43%

edited 19 Aug '12, 01:54


Here is an installation guide for Nginx + uWSGI.

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

bash -e build_uwsgi.sh | tee build_uwsgi.log

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

First, create a new Custom Application (listening on port), and note the assigned port. If you don't have a Django application (or other WSGI-compatible app) already, create one also. Then, fill in the appropriate variables at the top of this script.

# Set environment variables
APPNAME=uwsgi_server       # Name of the uWSGI Custom Application
APPPORT=12345              # Assigned port for the uWSGI Custom Application
PYTHON=python2.7           # Django python version
DJANGOAPP=django           # Django application name
DJANGOPROJECT=myproject    # Django project name

mkdir -p $HOME/webapps/$APPNAME/{bin,nginx,src,tmp}

###########################################################
# nginx 1.2.3
# original: http://nginx.org/download/nginx-1.2.3.tar.gz
###########################################################
cd $HOME/webapps/$APPNAME/src
wget 'http://mirror.ryansanden.com/uwsgi-1.2/nginx-1.2.3.tar.gz'
tar -xzf nginx-1.2.3.tar.gz
cd nginx-1.2.3
./configure \
  --prefix=$HOME/webapps/$APPNAME/nginx \
  --sbin-path=$HOME/webapps/$APPNAME/nginx/sbin/nginx \
  --conf-path=$HOME/webapps/$APPNAME/nginx/nginx.conf \
  --error-log-path=$HOME/webapps/$APPNAME/nginx/log/nginx/error.log \
  --pid-path=$HOME/webapps/$APPNAME/nginx/run/nginx/nginx.pid  \
  --lock-path=$HOME/webapps/$APPNAME/nginx/lock/nginx.lock \
  --with-http_flv_module \
  --with-http_gzip_static_module \
  --http-log-path=$HOME/webapps/$APPNAME/nginx/log/nginx/access.log \
  --http-client-body-temp-path=$HOME/webapps/$APPNAME/nginx/tmp/nginx/client/ \
  --http-proxy-temp-path=$HOME/webapps/$APPNAME/nginx/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=$HOME/webapps/$APPNAME/nginx/tmp/nginx/fcgi/
make && make install

###########################################################
# uwsgi 1.2
# original: http://projects.unbit.it/downloads/uwsgi-1.2.tar.gz
###########################################################
cd $HOME/webapps/$APPNAME/src
wget 'http://mirror.ryansanden.com/uwsgi-1.2/uwsgi-1.2.tar.gz'
tar -xzf uwsgi-1.2.tar.gz
cd uwsgi-1.2
$PYTHON uwsgiconfig.py --build
mv ./uwsgi $HOME/webapps/$APPNAME/bin
ln -s $HOME/webapps/$APPNAME/nginx/sbin/nginx $HOME/webapps/$APPNAME/bin

mkdir -p $HOME/webapps/$APPNAME/nginx/tmp/nginx/client

cat << EOF > $HOME/webapps/$APPNAME/nginx/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    access_log  ${HOME}/logs/user/access_${APPNAME}.log combined;
    error_log   ${HOME}/logs/user/error_${APPNAME}.log  crit;

    include mime.types;
    sendfile on;

    server {
        listen 127.0.0.1:${APPPORT};

        location / {
            include uwsgi_params;
            uwsgi_pass unix://${HOME}/webapps/${APPNAME}/uwsgi.sock;
        }
    }
}
EOF

cat << EOF > $HOME/webapps/$APPNAME/wsgi.py
import sys, os

sys.path = ['${HOME}/webapps/${DJANGOAPP}/${DJANGOPROJECT}/${DJANGOPROJECT}',
            '${HOME}/webapps/${DJANGOAPP}/${DJANGOPROJECT}',
            '${HOME}/webapps/${DJANGOAPP}/lib/${PYTHON}',
           ] + sys.path

os.environ['DJANGO_SETTINGS_MODULE'] = '${DJANGOPROJECT}.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()
EOF

# make the start, stop, and restart scripts
cat << EOF > $HOME/webapps/$APPNAME/bin/start
#!/bin/bash

APPNAME=${APPNAME}

# Start uwsgi
\${HOME}/webapps/\${APPNAME}/bin/uwsgi \\
  --uwsgi-socket "\${HOME}/webapps/\${APPNAME}/uwsgi.sock" \\
  --master \\
  --workers 1 \\
  --max-requests 10000 \\
  --harakiri 60 \\
  --daemonize \${HOME}/webapps/\${APPNAME}/uwsgi.log \\
  --pidfile \${HOME}/webapps/\${APPNAME}/uwsgi.pid \\
  --vacuum \\
  --python-path \${HOME}/webapps/\${APPNAME} \\
  --wsgi wsgi

# Start nginx
\${HOME}/webapps/\${APPNAME}/bin/nginx
EOF

cat << EOF > $HOME/webapps/$APPNAME/bin/stop
#!/bin/bash

APPNAME=${APPNAME}

# stop uwsgi
\${HOME}/webapps/\${APPNAME}/bin/uwsgi --stop \${HOME}/webapps/\${APPNAME}/uwsgi.pid

# stop nginx
kill \$(cat \${HOME}/webapps/\${APPNAME}/nginx/run/nginx/nginx.pid)
EOF

cat << EOF > $HOME/webapps/$APPNAME/bin/restart
#!/bin/bash

APPNAME=${APPNAME}

\${HOME}/webapps/\${APPNAME}/bin/stop
sleep 5
\${HOME}/webapps/\${APPNAME}/bin/start
EOF

chmod 755 $HOME/webapps/$APPNAME/bin/{start,stop,restart}

Then, to start the stack, use:

$HOME/webapps/$APPNAME/bin/start

And to stop it, you have:

$HOME/webapps/$APPNAME/bin/stop
permanent link

answered 19 Aug '12, 01:47

ryans ♦♦
5.0k73159
accept rate: 43%

edited 30 Aug '12, 05:00

How do I write a script, if I use virtualenv?

(17 Jan '13, 08:07) rendrom
1

I'm fairly sure this guide is orthogonal to virtualenv, because any applications or scripts you run would implement the virtualenv environment via activate_this.py. That all happens after uWSGI is already running the app.

(17 Jan '13, 20:51) ryans ♦♦

How much memory improvement do you see by setting up Django this way vs. configuring Django via the Webfaction control panel?

(21 Feb '13, 22:32) ehutch

@ehutch there is not necessarily a memory usage improvement; it really depends on the Django application.

(22 Feb '13, 01:33) waynek

Here is a 2014 update along with instructions for using virtualenv.

I just successfully installed Nginx 1.4.7 with uwsgi 2.0.3 using a script loosely based on the script by RyanS.

I did not create a Django app as mentioned by RyanS. I did create the custom port. I removed the DJANGOAPP and DJANGOPROJECT variables from the script. I also removed this section:

cat << EOF > $HOME/webapps/$APPNAME/wsgi.py
import sys, os

sys.path = ['${HOME}/webapps/${DJANGOAPP}/${DJANGOPROJECT}/${DJANGOPROJECT}',
            '${HOME}/webapps/${DJANGOAPP}/${DJANGOPROJECT}',
            '${HOME}/webapps/${DJANGOAPP}/lib/${PYTHON}',
           ] + sys.path

os.environ['DJANGO_SETTINGS_MODULE'] = '${DJANGOPROJECT}.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()
EOF

This section creates the wsgi.py. I did not need it because I already had one in my Django project.

In RyanS's script he shows you where he got the nginx and uwsgi code. Then in the script he has you get the code from a mirror. You can just replace the wget's to the mirror with wget's directly to nginx.org and projects.unbit.it.

Next I ran the script using the command Ryans gave. It installed without a problem!

To link the app with my Django code, I made a symbolic link to wsgi.py in my Django code base.

Here is my wsgi.py code that fires up virtualenv:

import os
import sys

virtualenv_root = os.path.expanduser('~/.virtualenvs/my_virtualenv')
activate_this = "%s/bin/activate_this.py" % virtualenv_root
execfile(activate_this, dict(__file__=activate_this))

workspace = os.path.expanduser('~/path_to_my_django_code')
sys.path.insert(0,workspace)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I should also mention that I created a website from this app by linking it to a domain name using the Webfaction Control Panel.

As for memory usage, it was about half that of the same Django code running under Apache and mod_wsgi. No noticable difference in performance. However, my websites have very low volume. I probably am not taking advantage of nginx's strengths.

How am I going to setup my next website? Given how smooth the nginx and uwsgi install was, I will go with nginx.

permanent link

answered 22 Apr '14, 17:43

cwurld2
5137
accept rate: 0%

How does nginx/uwsgi get restarted if the server reboots? Does it automatically run every app's bin/start on boot up?

(02 May '14, 08:15) pcglue

Typically you should have a cron job that tries to start your backend server every 20 minutes like the example below.

4,24,44 * * ~/webapps/$APPNAME/bin/start

You also can have a cron entry that starts the process at boot time of the server.

(02 May '14, 12:20) aaront ♦♦

@aaront, @ryans, Great posts, I got this to work by manually running the commands in the script but if I try bash -e build_uwsgi.sh | tee build_uwsgi.log, I get ": command not found:" I tried adding "#!/bin/sh" at the top, but no effect. The variable assignment lines work fine. Its getting caught up at some point after, the blank line or the mkdir line. the .log file is empty. any thoughts?

(29 Oct '14, 23:05) rsp

@rsp I've looked through your home directory and I don't see your build_uwsgi.sh script anywhere. If you'd like us to look into this further, please open a support ticket to let us know the exact location of your script.

(29 Oct '14, 23:46) seanf

@seanf where to get build_uwsgi.sh ?my account is patchserver2

update: found it below!

(17 Dec '14, 08:52) lifepatch

Hi, How did you make a "symbolic link to wsgi.py in my Django code base"?? I'm almost there. Just need to figure this out. :)

(09 Jun '15, 14:50) Rijo John

@Rijo John - there's no need to follow this manual procedure. There's a one-click install available here: web2py + nginx + uwsgi installer for WebFaction

That said, if you want to make a symlink, you do that with the ln command, eg: ln -s /path/to/wsgi.py /path/to/symlink

(09 Jun '15, 15:30) seanf

Thanks Sean, does it work for Django too?

(09 Jun '15, 15:34) Rijo John

Oh, snap. I missed that part of your question :\

No, the installer that I linked is only for web2py.

(09 Jun '15, 15:42) seanf

It's fine, your symlink helped! Everything working excellently :D Thanks :)

(10 Jun '15, 03:49) Rijo John
showing 5 of 10 show 5 more comments
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:

×185
×17

question asked: 19 Aug '12, 01:33

question was seen: 10,892 times

last updated: 10 Jun '15, 03:49

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2016 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