-
Notifications
You must be signed in to change notification settings - Fork 8
New Server
This section is intended for those who have a brand new server or one without any django or python web server services
The following instructions are for Ubuntu 11, but the steps should be translatable to your OS of choice. It also describes a possible web server setup and not the only way to do this as your needs may differ.
I just yanked the steps from http://senko.net/en/django-nginx-gunicorn/ as he has already done a nice job explaining the setup.
Required
- nginx
- MySQL
- Python 2.6+
- pip
- virtualenv
- gunicorn
- PIL
Next we'll get virtualenv going. This simply stores everything python related in one nice little package so we don't mess up anything system wide. May seem like overkill but it does make deployment and management a lot easier.
PIP
curl http://python-distribute.org/distribute_setup.py | python
curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python
virtualenv
pip install virtualenv
Next, cd into wherever you want to store you web stuff, i.e. /var. The virtualenv stuff is optional.
virtualenv www
cd www
source bin/activate
Install python packages
pip install django
pip install django-frog
pip install pillow
pip install haystack
pip install south
Make a folder for the static files
mkdir /var/www/static
ln -s /usr/local/lib/python27/site-packages/frog/static /var/www/static/frog
##Project Dir
First make a project dir using django, I'll name it dev:
python django-admin.py startproject dev
##MySQL I'm using MySQL, but you could use postgres if that's what you are already using. Ubuntu comes with MySQL 5, so no need to install anything further. We just need the python bindings:
apt-get install python-mysqldb
Next we just need to create a database and add a user. For this tutorial, I'll use 'frog' as the database and 'django' as the user:
> mysql -u root -p
mysql> create database frog;
mysql> create user 'django'@'localhost' identified by 'django';
mysql> grant all on frog.* to 'django'@'localhost' identified by 'django';
##Django Settings Edit /var/www/dev/dev/settings.py and set the following values:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'frog',
'USER': 'django',
'PASSWORD': 'django',
'HOST': '',
'PORT': '',
}
}
MEDIA_ROOT = '/var/www/static/'
MEDIA_URL = 'http://127.0.0.1/static/'
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"frog.context_processors.media",
)
# Needed for redirect
LOGIN_URL = '/frog'
In the INSTALLED_APPS section, add 'frog' and 'south' to the list and remove the 'django.contrib.staticfiles' as it will just cause problems.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.comments',
'haystack',
'django.contrib.admin',
'frog',
'south',
)
If you want users to be created automatically, set the following:
AUTHENTICATION_BACKENDS = ('frog.auth.SimpleAuthBackend',)
SESSION_COOKIE_AGE = 31556926 # 1 year
You'll also need to edit the dev/urls.py file to add the frog urls:
...
url(r'^frog/', include('frog.urls')),
...
##Nginx The idea here is that nginx will sit in front of everything. Nginx is a lightweight and fast webserver, though for this tutorial, we'll just be using it to serve static files like images and javascript. So Nginx will redirect traffic to gunicorn when it's a django request and just server the static files when not. I believe this is a typical setup. So gunicorn will listen on port :8000 and Nginx on port :80.
Grab nginx:
sudo apt-get install nginx
Nginx will have a more fleshed out conf file. So edit /etc/nginx/nginx.conf. You'll only need to edit the server section of the default conf:
...
http {
...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
...
server {
listen 80;
server_name dev;
access_log logs/www/dev.log main;
client_max_body_size 512M;
# serve django admin files
# This location may differ based on your setup
location /media {
alias /usr/lib/python2.7/site-packages/django/contrib/admin;
expires 30d;
}
# serve static files
location /static {
alias /var/www/static;
expires 30d;
}
# pass requests for dynamic content
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://127.0.0.1:8000/;
}
}
...
}
...
##Gunicorn
Gunicorn will run all of the django/python stuff behind nginx. Install gunicorn:
pip install gunicorn
Next we'll create a service script for it, /var/dev/gunicorn.sh:
#!/bin/bash
set -e
LOGFILE=/var/log/gunicorn/frog.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=3
# user/group to run as
USER=your_unix_user
GROUP=your_unix_group
cd /var/www/dev
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn dev.wsgi:application \
--user=$USER --group=$GROUP --log-level=debug \
--log-file=$LOGFILE 2>>$LOGFILE
Make it executable
chmod ug+x /var/www/dev/gunicorn.sh
Next create an Upstart service, /etc/init/gunicorn.conf
description "Frog Django instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /var/www/dev/gunicorn.sh
Make a symlink to init.d
sudo ln -s /lib/init/upstart-job /etc/init.d/gunicorn
##Starting servers
Now just start Gunicorn and then nginx
service gunicorn start
service nginx start
You should be all set, just restart the web servers and follow the install instructions for frog.