Django1.4環境構築

そろそろDjango1.4を使ってみる。
ディレクトリ構成等が変更されていたので少し変えてみた。

mod_wsgiインストール

cd /usr/local/src
wget -c http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
tar zxvf mod_wsgi-3.3.tar.gz
cd mod_wsgi-3.3
./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/bin/python
make
make install

Apache設定変更

vi /usr/local/apache/conf/httpd.conf
# コメントイン
LoadModule wsgi_module	modules/mod_wsgi.so

Django用vhosts作成

mkdir -p /usr/local/apache/vhosts/xxx.com/{logs,etc,ssl.crt,ssl.key}
vi /usr/local/apache/conf/conf.d/xxx.com.conf
WSGIPythonPath /usr/local/django/mysite

<VirtualHost xx.xx.xx.xx:80>
    ServerName xxx.com
    ErrorLog /usr/local/apache/vhosts/xxx.com/logs/error_log
    CustomLog "/usr/local/apache/vhosts/xxx.com/logs/access_log" combined

    Alias /static /usr/local/django/mysite/htdocs/static/
    WSGIScriptAlias / /usr/local/django/mysite/htdocs/django.wsgi
</VirtualHost>

#<VirtualHost xx.xx.xx.xx:443>
#    ServerName xxx.com
#    ErrorLog /usr/local/apache/vhosts/xxx.com/logs/ssl_error_log
#    CustomLog "/usr/local/apache/vhosts/xxx.com/logs/ssl_access_log" combined
#
#    Alias /static /usr/local/django/mysite/htdocs/static/
#    WSGIScriptAlias / /usr/local/django/mysite/htdocs/django.wsgi
#
#    SSLEngine on
#    SSLCertificateFile /usr/local/apache/vhosts/xxx.com/ssl.crt/server.crt
#    SSLCertificateKeyFile /usr/local/apache/vhosts/xxx.com/ssl.key/server.key
#</VirtualHost>

<Directory /usr/local/django/mysite/htdocs>
    Options Includes ExecCGI FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
</Directory>

mysqlユーザ作成

groupadd -g 2008 mysql
useradd -u 2008 -g mysql -d /home/mysql -m -s /bin/bash mysql

MySQLインストール

apt-get install mysql-server mysql-client libmysqlclient-dev

MySQL文字コード設定

vi /etc/mysql/my.cnf
[mysqld]
# 追記
character-set-server = utf8
skip-character-set-client-handshake
collation-server = utf8_general_ci
init-connect = SET NAMES utf8

[mysql]
# 追記
default-character-set = utf8

MySQL再起動、初期設定

/etc/init.d/mysql restart
mysql_secure_installation

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

mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 43
Server version: 5.1.61-0+squeeze1 (Debian)

Copyright (c) 2000, 2011, 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              |
+--------------------+
2 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              |
+--------------------+
3 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

Djangoインストール

pip install mysql-python
pip install django

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

mkdir -p /usr/local/django
cd /usr/local/django
django-admin.py startproject mysite
cd mysite
python manage.py startapp myapp

Django設定

DB,Timezone,言語,Templateディレクトリを設定

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 = (
    '/usr/local/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!',
        },
    )

htdocs配置

mkdir -p /usr/local/django/mysite/htdocs/static
mv -i /usr/local/django/mysite/mysite/wsgi.py /usr/local/django/mysite/htdocs/django.wsgi

Apache再起動

chmod 755 /usr/local/django/mysite/apache/django.wsgi
/usr/local/apache/bin/httpd -t
/etc/init.d/apache stop
/etc/init.d/apache start
ps -ef | grep httpd
tail /usr/local/apache/logs/error_log