Django framework va nginx + uwsgi

Django framework va nginx + uwsgi

Salom. Shu kunlarda ayrim sabablarga ko’ra Pythonda kod yozishga to’g’ri keldi. Aniqrog’i django da sayt qilish kerak. Shunga o’zim ham o’rganuvchi bo’lganligim tufayli yana kimgadir foydam tegishi uchun nginx va django ni bog’lab ishlatish haqida qaror qildim.

Buning uchun biz WSGI (Web Server Gateway Interface) dan foydalanamiz. To’liq: ru.wikipedia.org/wiki/WSGI

Avvalo nginx ning oxirgi versiyasini o’rnatishimiz kerak. Chunki wsgi oxirgi versiyalarida mavjud. Buning uchun
/etc/apt/source.list ga

deb http://nginx.org/packages/ubuntu/ lucid nginx

ning faqat o’zini yozib

apt-get update
apt-get install nginx

Qo’shimcha kutubxonalar so’rashi mumkin. Shularni o’rnating va yana boshqdan buyruqlarni bajaring. O’rnatib bo’lganingizdan keyin /etc/apt/source.list ni o’z holiga qaytarib «apt-get update» qilish esdan chiqmasin.
To’liq: wiki.nginx.org/Install

Keyingi qadam «pip» ni o’rnatamiz. Buning uchun

cd
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

To’liq: pip.readthedocs.org/en/latest/user_guide.html

Endi UWSGI ni o’rnatamiz, bu WSGI bilan ishlaydigan serverdir.

apt-get install python-dev
pip install uwsgi

To’liq: uwsgi-docs.readthedocs.org/en/latest/Install.html

UWSGI ni o’rnatgan bo’lsangiz kelin uni bir tekshirib ko’raylik. Bitta test.py fayl yarating

# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    #return [b"Hello World"] # python3
    return ["Hello World"] # python2

va

uwsgi --http 127.0.0.1:8000 --wsgi-file test.py

agar hammasi OK bo’lsa 127.0.0.1:8000 ga kirsangiz «Hello World» matnini ko’rishingiz kerak.

Endi django bilan shug’ullanamiz. Kerakli versiyani tanlab

pip install Django==1.6.5

To’liq: docs.djangoproject.com/en/1.6/intro/install/

Django ni o’rnatilganini tekshirish:

python -c "import django; print django.get_version()"
1.6.5

Django ham o’rnatilgan bo’lsa endi django da bitta proyekt yaratamiz.

mkdir /var/wwwpy
cd /var/wwwpy
django-admin.py startproject myprj
cd myprj
chmod 755 manage.py
./manage.py runserver

Umuman ushbu kodda django da proyekt yaratilib o’zining kichik web serverini ishga tushirildi. Djangoni dokumentatsiyasida ushbu web serverni faqat kod yozish mobaynida ishlatish tavsiya qilingan. Shuning uchun ham nginx bilan bog’langan. Agar hammasi OK bo’lsa 127.0.0.1:8000 ga kiring

It worked!
Congratulations on your first Django-powered page.

ko’rinishidagi tekst chiqishi kerak.

Agar chiqqan bo’lsa endi nginx va uwsgi larni sozlashni boshlaymiz (djangi serverini o’chiring, ctrl+c ni bosing).
/etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    inlcude /etc/nginx/sites-enabled/*;
}

/etc/nginx da «sites-enabled» nomli papka yaratamiz va
/etc/nginx/sites-enabled/myprj fayl

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

server {
    # the port your site will be served on
    listen      80;

    server_name myprj;

    # the domain name it will serve for
    # server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /var/wwwpy/myprj/media;  # your Django project's media files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

keyin hosts ga myprj domenni qo’shamiz

echo "127.0.0.1 myprj" >> /etc/hosts

myprj ga kirsangiz

502 Bad Gateway

xatosi chiqsa hayron bo’lmang, biz to’g’ri yo’lda ketyapmiz. Endiki masala «127.0.0.1:8001» ga uwsg serverini ishga tushirishimiz kerak. Buning uchun

uwsgi -s 127.0.0.1:8001 --pythonpath /var/wwwpy/myprj/ --module myprj.wsgi

va yana myprj ga kiramiz

It worked!
Congratulations on your first Django-powered page.

chiqishi kerak. Chiqqan bo’lsa sizni tabrikliman siz nginx, django va uwsgi larni to’g’ri sozlab bo’ldingiz. Bu yog’iga pythonda kod yozish qoldi holos.

Har gal serverni yuklab o’tirmaslik uchun /etc/init.d/myprj faylga yozamiz

#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi app server
# Description:       starts uwsgi app server using start-stop-daemon
### END INIT INFO
 
PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/uwsgi
PYTHONPATH=/var/wwwpy/myprj
MODULE=myprj.wsgi
 
OWNER=uwsgi
 
NAME=uwsgi
DESC=uwsgi
 
test -x $DAEMON || exit 0
 
# Include uwsgi defaults if available
if [ -f /etc/default/uwsgi-graphite ] ; then
        . /etc/default/uwsgi-graphite
fi
 
set -e
 
DAEMON_OPTS="-s 127.0.0.1:8001 -d /var/log/uwsgi-graphite.log --pythonpath $PYTHONPATH --module $MODULE"
 
case "$1" in
  start)
        echo -n "Starting $DESC: "
        start-stop-daemon --start --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --signal 3 --quiet --retry 2 --stop 
                --exec $DAEMON
        echo "$NAME."
        ;;
  reload)
        killall -1 $DAEMON
        ;;
  force-reload)
        killall -15 $DAEMON
       ;;
  restart)
        echo -n "Restarting $DESC: "
        start-stop-daemon --signal 3 --quiet --retry 2 --stop 
                --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  status)
        killall -10 $DAEMON
        ;;
      *)
            N=/etc/init.d/$NAME
            echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
            exit 1
            ;;
    esac
    exit 0

endi siz be’malol /etc/init.d/myprj (start|stop|restart) qilsangiz bo’ladi. «chmod 755 /etc/init.d/myprj» qilish esdan chiqmasin :).

Savollar bo’lsa marhamat.

Texnologiyalar
Django framework va nginx + uwsgi