It’s very hard to find article to help setup UWSGI and Django 1.7 in production. I use EC2 with Ubuntu 14.04. The only reason why I need to use uwsgi because Apache mod-wsgi can’t run together with gevent. It’s always throw LoopExit exception.
Here is solution. First, we need to install required softwares :
1. Install dependecies
1 | sudo apt-get install uwsgi build-essential python-dev uwsgi-plugin-python nginx |
2. Setup wsgi configuration
Here is my example, i create “/etc/uwsgi/apps-available/ecommerce-dev.ini” and symlink to “/etc/uwsgi/apps-enabled/ecommerce-dev.ini”. This is the content :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | [uwsgi] ;enable master process manager master = true ;spawn 2 uWSGI worker processes workers = 2 ;unix socket (referenced in nginx configuration) socket = /tmp/ecommerce-dev.sock # set mode of created UNIX socket chmod-socket = 666 # place timestamps into log log-date = true # user identifier of uWSGI processes uid = www-data # group identifier of uWSGI processes gid = www-data ; number of worker processes ;processes = 2 ;vacuum = true ; project-level logging to the logs/ folder ;logto = /var/yodi/ecommerce-dev/uwsgi.log ; django >= 1.4 project chdir = /var/yodi/ecommerce-dev wsgi-file=/var/yodi/ecommerce-dev/ecommerce/wsgi.py ;enable-threads = true virtualenv = /home/ubuntu/.virtualenvs/production vacuum = true env = DJANGO_SETTINGS_MODULE=ecommerce.settings pidfile = /tmp/ecommerce-dev.pid ;harakiri = 20 # respawn processes taking more than 20 seconds ;max-requests = 5000 # respawn processes after serving 5000 requests |
3. Register WSGI services
1 | sudo vim /etc/init/uwsgi.conf |
And write this :
1 2 3 4 5 6 7 8 9 | description "uWSGI" start on runlevel [2345] stop on runlevel [06] respawn env UWSGI=/usr/local/bin/uwsgi env LOGTO=/var/log/uwsgi-emperor.log exec $UWSGI –master –emperor /etc/uwsgi/apps-enabled –die-on-term –uid www-data –gid www-data –logto $LOGTO |
4. Running services
1 | sudo service uwsgi start |
5. Setup NGINX to listen UWSGI socket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | server { listen 82; server_name dev.custoom.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; root /var/yodi/ecommerce-dev; client_body_timeout 460; client_header_timeout 460; send_timeout 460; client_max_body_size 10m; keepalive_timeout 300; index index.php index.html index.htm; # Check if a file exists at /var/www/domain/ for the incoming request. # If it doesn’t proxy to Apache/Django. try_files $uri @django; location / { uwsgi_pass unix:/tmp/ecommerce-dev.sock; include uwsgi_params; } } |
6. Running NGINX
1 | sudo service nginx restart |
Success! 😀