Skip to content

Instantly share code, notes, and snippets.

@fduran
Last active February 19, 2016 00:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fduran/779752da0f76bb27bf33 to your computer and use it in GitHub Desktop.
Save fduran/779752da0f76bb27bf33 to your computer and use it in GitHub Desktop.
Django Install - Nginx uwsgi

Django Install - Nginx uwsgi

Once per server:

apt-get update && apt-get upgrade
apt-get install python-setuptools python-dev build-essential
easy_install -U pip
pip install virtualenv

Per project:

cd /home
mkdir geobouncer && cd geobouncer
virtualenv --no-site-packages venv
source venv/bin/activate
pip install django

django-admin.py startproject geobouncer cd geobouncer python manage.py runserver 0.0.0.0:8080

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

uwsgi --http :8000 --module geobouncer.wsgi --virtualenv /home/geobouncer/venv

apt-get install nginx

mkdir static

For sqlite3:
mkdir db
touch db/sqlite.db
chown -R www-data:www-data db

For msyql:
apt-get install libmysqlclient-dev
apt-get install python-dev
pip install MySQL-python

python manage.py startapp app (creates app/models.py views.py skeleton)

mkdir app/templates
nano geobouncer/settings.py
add PROJECT_ROOT = ‘/home/geobouncer/geobouncer’
DATABASES = STATICFILES_DIRS = PROJECT_ROOT + ‘/static’
TEMPLATE_DIRS = PROJECT_ROOT + ‘/templates’
INSTALLED_APPS = (app, needed if syncdb)

nano geobouncer/urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'app.views.home', name='home'),
)
nano app/views.py
from django.shortcuts import render
def home(request):
return render(request, "index.html")

nano app/templates/index.html
hello

uwsgi --http :8000 --module geobouncer.wsgi --virtualenv /home/geobouncer/venv
should show “hello” mesage

/etc/init.d/nginx start
mkdir nginx
nano nginx/nginx.conf (from https://gist.github.com/evildmp/3094281 , ip based) ln -s /home/geobouncer/geobouncer/nginx/nginx.conf /etc/nginx/sites-enabled/

echo “static test” > nano static/test.html
/etc/init.d/nginx restart
http://(ip):8000/static/test.html should show “static test” message

uwsgi --socket :8001 --wsgi-file test.py
http://(ip):8000/ should show test file output

socket in /home/geobouncer/geobouncer/nginx/nginx.conf instead of port:

touch /tmp/uwsgi.sock
chown www-data:www-data /tmp/uwsgi.sock
sudo -uwww-data /home/geobouncer/venv/bin/uwsgi --socket /tmp/uwsgi.sock --wsgi-file /tmp/test.py

uwsgi --uid www-data --socket /tmp/uwsgi.sock --wsgi-file /tmp/test.py

django:
uwsgi --uid www-data --socket /tmp/uwsgi.sock --module geobouncer.wsgi --virtualenv /home/geobouncer/venv

change port in nginx.conf to 80 then rm /etc/nginx/sites-enabled/default

mkdir uwsgi
nano uwsgi/uwsgi.ini
[uwsgi]
master = true
processes = 2
socket = /tmp/uwsgi.sock
chmod-socket = 664
chdir = /home/geobouncer/geobouncer
module = geobouncer.wsgi
home = /home/geobouncer/venv
vacuum = true
uid = www-data

uwsgi --ini /home/geobouncer/geobouncer/uwsgi/uwsgi.ini

nano /etc/init/uwsgi.conf
description "uwsgi tiny instance"
start on runlevel [2345]
stop on runlevel [06]

exec uwsgi --ini /home/geobouncer/geobouncer/uwsgi/uwsgi.ini

service uwsgi start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment