Prototyping a tiny project with Django 1.4 on Ubuntu.
Want to use gunicorn and nginx for Django.
Here we go!
1.suppose the django project is already there, now install gunicorn and nginx
sudo apt-get install gunicorn nginx
2.append gunicorn to settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'myApplication'
'djangorestframework',
'gunicorn',
)
3.run application inside gunicorn
python manage.py run_gunicorn
4. append a new server in nginx configuration (/etc/nginx/nginx.conf)
sudo vi /etc/nginx/nginx.conf
http {
##...
##...
server {
listen 80;
server_name 192.168.85.172; #yousite.com
#access_log /var/log/your_website_log main;
location / {
proxy_pass http://localhost:8000/;
proxy_redirect off;
}
}
}
Note: Here 192.168.85.172 is the local ip of the testing server. 'server_name' is 'localhost' by default, but I could't make it bind to port 80 successfully until I changed it as such. If I don't change 'server_name', I have to bind to other port (e.g 8001) on my Ubuntu 12.04
Now access nginx http://localhost/admin is actually redirected to django on gunicornhttp://localhost:8000/admin
5. if you're running J2EE on Jetty/Tomcat...
just change change the proxy_pass primitive to such as 'proxy_pass http://localhost:8080/;' after starting jetty/tomcat