Setup Vagrant Nginx Django uwsgi di Localhost

Karena versi OS local tidak selalu sama dengan yang di server, maka kadang saya ingin menyamakan environment OS dengan yang di server, disinilah vagrant banyak berperan. Asumsinya Anda sudah punya punya vagrant, nginx, django project beserta dengan uwsgi. Mari kita mulai..

Setup Vagrant

Tambahkan baris berikut VagrantFile. Ini berarti mem-forward port 8000 ke 8080, sehingga kita bisa akses port vagrant tersebut dari browser di OS host dengan port 8080. (http://localhost:8080)

config.vm.network "forwarded_port", guest: 8000, host: 8080

Setup Django

Tidak ada yang spesial disini, saya prefer menggunakan virtualenv

virtualenv -p python3 myproject
cd myproject
source .bin/activate
pip install django
pip install uwsgi
django-admin startproject myproject
cd myproject
django-admin startapp myapp

Setup uwsgi

/home/ubuntu/myproject/bin/uwsgi \
--soct /tmp/uwsgi.sock \
--chdir \
/home/ubuntu/myproject/myproject/ \
--module myproject.wsgi \
--env DJANGO_SETTINGS_MODULE=myproject.settings \
--virtualenv /home/ubuntu/myproject/ \
--chmod-socket=666

Setup Nginx

Silakan path disesuaikan, karena saya menggunakan compiled nginx, maka path nginx.conf saya ada di

/usr/local/nginx/nginx.conf

Dan berikut isinya:

load_module "modules/ngx_pagespeed.so";

user www-data www-data;
worker_processes 1;

events {
   worker_connections 1024;
}

http {

   fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
   fastcgi_cache_key "$scheme$request_method$host$request_uri";

   server {
      pagespeed on;
      pagespeed FileCachePath /var/ngx_pagespeed_cache;
      listen 8000;
      server_name localhost;

      location / {
         try_files $uri @app;

         add_header X-FastCGI-Cache $upstream_cache_status;
         fastcgi_cache MYAPP;
         fastcgi_cache_valid 200 2m;
      }

      location @app {
         include uwsgi_params;
         uwsgi_pass unix:/tmp/uwsgi.sock;
      }

   }

}
Advertisements

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