[Django] gunicorn deployment minutes

Source: Internet
Author: User

[Django] gunicorn deployment minutes

Using Gunicorn to deploy the Django application, you do not have to write the operation step by step. Simply record the important points and check them later. The main method is to reverse proxy Nginx to Gunicorn and Gunicorn wsgi to start Django.

Features

Gunicorn is a Python wsgi Application Server Based on the prefork mode. It supports Unix like systems.

Using epoll (Linux) non-blocking network I/O models

You can choose to synchronize multiple Worker types, such as Event-based (gevent tornado) and multi-thread-based

High performance, comparable to uwsgi

Easy to configure and use

Python 2.x> = 2.6 or Python 3.x> = 3.2

Operation

Gunicron installation is very simple pip install gunicorn or use easy_install, the source code can be, basically pure Python code, the installation is generally relatively smooth, it is best to use with virtualenv.

Django is the simplest deployment (wsgi only contains the wsgi. py file in the django Project)

Gunicorn wsgi: application # Eight workergunicorn-w 8 wsgi: application # specify the port number gunicorn-w 8-B 0.0.0.0: 8888 wsgi: application # unix socketgunicorn-w 8 -- bind unix: /xx/mysock. sock wsgi: application # Use gevent for Asynchronization (by default, worker is synchronous) gunicorn-w 8 -- bind 0.0.0.0: 8000-k 'gevent' wsgi: application # There are many options, view the document or use -- help to view -- log-level = DEBUG -- timeout = 100
Reference script

The following is just a reference example, not the actual deployment configuration.

how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx

Deployment Script Reference 1

#!/bin/bashNAME="djangotut" # Name of the applicationDJANGODIR=/xxx/django_project # Django project directorySOCKFILE=/xxx/gunicorn.sock # we will communicte using this unix socketUSER=osboxes # the user to run asGROUP=osboxes # the group to run asNUM_WORKERS=8 # how many worker processes should Gunicorn spawnMAX_REQUESTS=100000 # reload the application server for each requestDJANGO_SETTINGS_MODULE=django_project.settings # which settings file should Django useDJANGO_WSGI_MODULE=django_project.wsgi # WSGI module nameecho “Starting $NAME as `whoami`”# Activate the virtual environmentcd $DJANGODIRsource ~/.virtualenvs/django-tutorial-env/bin/activateexport DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULEexport PYTHONPATH=$DJANGODIR:$PYTHONPATH# Create the run directory if it doesn’t existRUNDIR=$(dirname $SOCKFILE)test -d $RUNDIR || mkdir -p $RUNDIR# Start your Django Unicorn# Programs meant to be run under supervisor should not daemonize themselves (do not use –daemon)exec ~/.virtualenvs/django-tutorial-env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \–name $NAME \–workers $NUM_WORKERS \–max-requests $MAX_REQUESTS \–user=$USER –group=$GROUP \–bind=0.0.0.0:3000 \–log-level=error \–log-file=-

A simple shell script used by myself. refer to section 2.

#!/bin/sh#file: gun.sh#start and stop gunicorn+django appP=8000worker=1host="0.0.0.0"case "$@" in start) gunicorn -b $host:$P -w $worker -k 'gevent' wsgi:application -D ;; stop) kill -9 `ps aux|grep gunicorn|grep $P|awk '{print $2}'|xargs` ;; restart) kill -9 `ps aux|grep gunicorn|grep $P|awk '{print $2}'|xargs` sleep 1 gunicorn -b $host:$P -w $worker -k 'gevent' wsgi:application -D ;; reload) ps aux |grep gunicorn |grep $P | awk '{print $2}'|xargs kill -HUP ;; status) pids=$(ps aux|grep gunicorn|grep $P) echo "$pids" ;; *) echo 'unknown arguments args(start|stop|restart|status|reload)' exit 1 ;;esac
Nginx configuration example
server { listen 80; server_name 0.0.0.0; client_max_body_size 4G; location /static/ { alias /xxx/static/; } location /media/ { alias /xxx/media/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; # Try to serve static files from nginx, no point in making an # *application* server like Unicorn/Rainbows! serve static files. if (!-f $request_filename) { proxy_pass http://app_server; break; } }}
Optimization Use meinheld to replace the default worker, which is also an event-based async worker, but faster than gevent.
pip install -U meinheldgunicorn --workers=2 --worker-class="egg:meinheld#gunicorn_worker" wsgi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.