Home > World Of ICT > How to setup Django with uWSGI and Nginx using CentOS x.x (Release 6.4)

How to setup Django with uWSGI and Nginx using CentOS x.x (Release 6.4)


Django merupakan Python web framework yang berfungsi untuk mempermudah proses pembuatan web, framework ini  sepenuhnya gratis dan dapat dipergunakan/modifikasi sesuai keinginan programmer,  selain Django ada juga framework open source yang dibuat untuk  pengembangan web populer lainnya yaitu CodeIgniter (http://en.wikipedia.org/wiki/CodeIgniter).

Django menggunakan engine Python dan CodeIgniter  berbasis PHP.  Django  mengenal istilah  Model, Template dan View.  Model adalah layer yang digunakan untuk berinteraksi dengan database, Template adalah layer presentasi untuk HTML, XML dan lainnya, sedangkan View adalah layer yang berisikan logika yang mengolah data dari model dan mengirimkannya ke dalam Template.

uWSGI merupakan aplikasi yang dapat menjalankan WSGI (http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface) melalui Unix Socket Interface dengan WSGI protocol,  Nginx merupakan salah satu web server open source yang handal dan banyak digunakan pada aplikasi dengan load trafik yang tinggi, disini saya coba dokumentasikan bagaimana proses integrasi Django, uSWGI, Python dan Nginx.

Notes:
  1. Tutorial Django-uWSGI-Nginx ini telah diujicobakan pada Centos 6.4 berbasis virtualenv
  2. port 8000 adalah port default yang digunakan untuk menjalankan Django

  • Install Virtual Env
yum install python-virtualenv* -y
virtualenv uwsgi-tuto

--- Wil show this process
New python executable in uwsgi-tuto/bin/python
Installing setuptools............done.
Installing pip...............done.

cd uwsgi-tuto
source bin/activate

shell akan berubah seperti ini

(uwsgi-tuto)[root@sia uwsgi-tuto]#
  • Install Django
Install Django pada lingkungan  virtualenv dan create test project:
pip install Django

----Will show this 
Downloading/unpacking Django
Downloading Django-1.5.1.tar.gz (8.0Mb): 8.0Mb downloaded
Running setup.py egg_info for package Django
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: Django
Running setup.py install for Django
changing mode of build/scripts-2.6/django-admin.py from 644 to 755
warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /root/uwsgi-tuto/bin/django-admin.py to 755
Successfully installed Django
Cleaning up...
(uwsgi-tuto)
django-admin.py startproject mysite
cd mysite
  • Install uWSGI
pip install uWSGI (Syaratnya sudah harus terinstall GCC sebelumnya)

---- Will show this 
(uwsgi-tuto)[root@siak jajalmaning]# pip install uwsgi
Downloading/unpacking uwsgi
Downloading uwsgi-1.9.14.tar.gz (660kB): 660kB downloaded
Running setup.py egg_info for package uwsgi
Installing collected packages: uwsgi
Running setup.py install for uwsgi
using profile: buildconf/default.ini
detected include path: ['/usr/local/include', '/usr/lib/gcc/x86_64-redhat-linux/4.4.7/include', '/u sr/include']
Patching "bin_name" to properly install_scripts dir
detected CPU cores: 6
...
...
...
...
*** uWSGI is ready, launch it with /root/uwsgi-tuto/bin/uwsgi 
*** Successfully installed uwsgi Cleaning up...
  • Create Test APP

vi  test.py

Kita coba mulai dengan kata-kata hello world

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

Jalankan Test APP

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

---- Will show this
*** Starting uWSGI 1.9.14 (64bit) on [Thu Aug 22 13:08:11 2013] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-3) on 22 August 2013 11:40:35
os: Linux-2.6.32-358.14.1.el6.x86_64 #1 SMP Tue Jul 16 23:51:20 UTC 2013
nodename: siak.unila.ac.id
machine: x86_64
clock source: unix
detected number of CPU cores: 6
current working directory: /usr/share/nginx/html/uwsgi-tuto/jajalmaning
detected binary path: /usr/share/nginx/html/uwsgi-tuto/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 22374
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 8964)
uwsgi socket 0 bound to TCP address 127.0.0.1:58382 (port auto-assigned) fd 3
Python version: 2.6.6 (r266:84292, Jul 10 2013, 22:48:45) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x18fe760
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72776 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x18fe760 pid: 8963 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 8963, cores: 1)

Tinggal buka browser dan arahkan ke port 8000

1
  • Konfigurasi Nginx dan uWSGI 
Cari file uwsgi_params
(uwsgi-tuto)[root@my.server.com:~]locate uwsgi_params
/etc/nginx/uwsgi_params

Check  directory:
(uwsgi-tuto)[root@my.server.com:~]pwd
/root/uwsgi-tuto/mysite

Copy uwsgi_params ke  curren location:

(uwsgi-tuto)[root@my.server.com:~]cp /etc/nginx/uwsgi_params . -v
`/etc/nginx/uwsgi_params' -> `./uwsgi_params'

(uwsgi-tuto)[root@my.server.com:~]ls -alh
total 24K
drwxr-xr-x 3 root root 4.0K May 27 10:48 .
drwxr-xr-x 6 root root 4.0K May 27 10:33 ..
-rw-r--r-- 1 root root 249 May 27 10:24 manage.py
drwxr-xr-x 2 root root 4.0K May 27 10:43 mysite
-rw-r--r-- 1 root root 124 May 27 10:34 test.py
-rw-r--r-- 1 root root 623 May 27 10:48 uwsgi_params
(uwsgi-tuto)[root@my.server.com:~]

  • Konfigurasi  Nginx default.conf
Kira-kira isinya akan seperti ini
### example.conf nginx configuration

# the upstream component nginx needs to connect to
upstream django {
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {

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

# the domain name used for the test, replace it with your own domain.
server_name example.com www.example.com;

# the default charset
charset utf-8;

# Set max upload size
client_max_body_size 100M; # adjust according to your needs

# Django media & static files
location /media {
alias /root/uwsgi-tuto/mysite/mysite/media;
}

location /static {
alias /root/uwsgi-tuto/mysite/mysite/static;
}

# All the other stuff is handled directly by Django
location / {
uwsgi_pass django;
include /root/uwsgi-tuto/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
  • Terakhir jalankan  Django app dengan uWSGI serta Nginx
uwsgi --socket :8001 --module mysite.wsgi &

Browsing seperti biasa, lalu Django framework sudah berjalan.

2

Sumber :

  1. Nginx: http://nginx.org/en/docs/
  2. Django: https://docs.djangoproject.com/
  3. uWSGI: http://uwsgi-docs.readthedocs.org/
  4. Nginx Tips : http://www.nginxtips.com

Demikian dan Terima Kasih.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: