rackhubでDjango環境構築

Nginx+gunicorn+MySQL環境でDjangoを動かすメモ。
rackhubは久々に触ったけど結構変わってる。
以前は/rhbにインストールされていたがstowで管理されるようになったみたい。
pipもインストールされてるし、pythonbrewも入ってるのでより開発環境が整備された印象。
とりあえずこんにちわ世界まで。

django,gunicornインストール

export PATH=$PATH:/usr/local/mysql/bin
export LD_LIBRARY_PATH=/usr/local/mysql/lib
pip install mysql-python
pip install django
pip install gunicorn

MySQL初期化、初期設定

sudo rm -rf /usr/local/mysql/data
sudo mkdir -p /usr/local/mysql/data
sudo /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
sudo chown -R mysql. /usr/local/mysql
sudo /usr/local/mysql/mysql.server start
export PATH=$PATH:/usr/local/mysql/bin
mysql_secure_installation

MySQLユーザ、データベース作成

/usr/local/mysql/bin/mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.6.5-m8 Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database django;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| django             |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

mysql> grant all privileges on django.* to django@localhost identified by 'secret';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user where user='django';
+--------+-----------+
| user   | host      |
+--------+-----------+
| django | localhost |
+--------+-----------+
1 row in set (0.00 sec)

mysql> quit
Bye

Nginx設定ファイル作成

sudo vi /usr/local/nginx/conf.d/django
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 15;
proxy_send_timeout 15;
proxy_read_timeout 15;

upstream django {
    server 127.0.0.1:8000;
}

server {
    listen 80 default;
    server_name xxx.rackbox.net;

    location / {
        proxy_pass http://django;
    }
}

Nginx起動

sudo nginx

Djangoプロジェクト、アプリ作成

mkdir ~/django
cd ~/django
django-admin.py startproject mysite
cd mysite
python manage.py startapp myapp

Django設定

vi mysite/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'django',
        'PASSWORD': 'secret',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

TIME_ZONE = 'Asia/Tokyo'

LANGUAGE_CODE = 'ja'

TEMPLATE_DIRS = (
    '/home/rackhuber/django/mysite/templates',
)

syncdb

python manage.py syncdb

indexページのurl設定

vi mysite/urls.py
urlpatterns = patterns('',
    url(r'^$', 'myapp.views.index'),
)

Template作成

mkdir -p templates/myapp
vi templates/base.html
<!DOCTYPE html>
<html lang="ja">
<head>
  <title>{{ title }}</title>
  {% block head %} {% endblock %}
</head>
<body>
{% block content %} {% endblock %}
</body>
</html>
vi templates/myapp/index.html
{% extends "base.html" %}

{% block head %}
{% endblock %}

{% block content %}
{{ message }}
{% endblock %}

View作成

vi myapp/views.py
#!/usr/bin/python
# coding: utf-8

from django.views.generic.simple import direct_to_template

def index(request):
    return direct_to_template(request, 'myapp/index.html',
        extra_context = {
            'title': 'Hello!',
            'message': 'Hello World!',
        },
    )

gunicorn起動

cd $HOME/django/mysite
gunicorn_django --access-logfile=/home/rackhuber/access.log --error-logfile=/home/rackhuber/error.log --log-level=info --daemon --pid=/home/rackhuber/gunicorn.pid --user=rackhuber --group=rackhuber --workers=1 --bind=127.0.0.1:8000