CentOS に Django をデプロイ - with python2.6, mod_python, mysql

Ubuntu でしか作ったことなかったけど、CentOS 5 系 で Django をデプロイしてみる

  1. とりあえずパッケージを最新に更新
    # yum update
    
  2. python 2.6 のインストール
    CentOS ってデフォルトでは 2.4 なんですね。2.6.4 をソースから入れる。まず zlib, sqlite を入れる。sqlite いれないと import sqlite3 できない
    # yum install zlib zlib-devel sqlite-devel
    
    python ソースのダウンロードとビルド、インストール
    # wget http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
    # tar xvfz Python-2.6.4.tgz
    # cd Python-2.6.4
    
    /opt/python2.6 にインストール。path は個人的な趣味。 --enable-shared オプションを追加する
    # ./configure --prefix=/opt/python2.6 --with-threads --enable-shared
    
    Setup ファイルで zlib のところのコメントを外す。
    # vi Modules/Setup
    
    --- #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
    +++ zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
    
    # make
    # make install
    
  3. python 2.6 環境設定 (2.4 と共存させる)
    パス周りを設定
    # vi /etc/ld.so.conf.d/opt-python2.6.conf
    # /sbin/ldconfig
    # sudo ln -s /opt/python2.6/bin/python /usr/bin/python2.6
    # cd
    
    個人的な趣味で、PATH は.bash_profile, alias は .bashrc にセット
    # vi .bash_profile
    
    --- PATH=$PATH:$HOME/bin
    +++ PATH=$PATH:$HOME/bin:/opt/python2.6/bin/
    
    # vi .bashrc
    
    +++ alias python='python2.6'
    
    # source ~/.bash_profile
    # source ~/.bashrc
    
    確認
    # python2.6 -V
    Python 2.6.4
    # python -V
    Python 2.6.4
    
  4. setuptools のインストール
    オプションで python の path を渡す
    # wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg
    # sudo sh setuptools-0.6c11-py2.6.egg --prefix=/opt/python2.6
    
  5. PIL のインストール
    # yum install libjpeg-devel freetype*
    # wget http://effbot.org/media/downloads/Imaging-1.1.7b1.tar.gz
    # cd Imaging-1.1.7b1
    # python setup.py build
    # python setup.py install
    
    インストールをテスト
    # python selftest.py
    --------------------------------------------------------------------
    PIL 1.1.7b1 TEST SUMMARY
    --------------------------------------------------------------------
    Python modules loaded from ./PIL
    Binary modules loaded from /opt/python2.6/lib/python2.6/site-packages/PIL
    --------------------------------------------------------------------
    *** PIL CORE support not installed
    *** TKINTER support not installed
    --- JPEG support ok
    --- ZLIB (PNG/ZIP) support ok
    *** FREETYPE2 support not installed
    *** LITTLECMS support not installed
    --------------------------------------------------------------------
    Running selftest:
    --- 57 tests passed.
    
  6. Django のインストール
    easy_install さまさま
    # sudo easy_install Django
    Installed /opt/python2.6/lib/python2.6/site-packages/Django-1.1.1-py2.6.egg
    
  7. MySQL, MySQL-python のインストール
    エンコードは utf8 に。
    # yum install mysql-devel
    # vi /etc/my.cnf
    
    +++
    [mysql]
    default-character-set = utf8
    +++
    
    # /etc/init.d/mysqld start
    
    起動時にサービスが開始されるように設定
    # /sbin/chkconfig --levels 235 mysqld on
    
    python ドライバを入れておく
    # easy_install MySQL-python
    
  8. MySQL の設定
    # mysqladmin -u root password 'パスワード';
    # mysqladmin -u root -h localhost password 'パスワード';
    # mysql -u root -p
    Enter password:
    
    mysql> CREATE DATABASE mysite;
    Query OK, 1 row affected (0.36 sec)
    
    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysite             |
    | mysql              |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)
    
    mysql> CREATE USER mysiteuser IDENTIFIED BY 'パスワード';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> GRANT ALL PRIVILEGES ON mysite.* TO mysiteuser IDENTIFIED BY 'パスワード';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> GRANT ALL PRIVILEGES ON mysite.* TO mysiteuser@localhost IDENTIFIED BY 'パスワード';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> quit
    Bye
    # mysql -u mysiteuser -p
    Enter password: 
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysite             |
    | test               |
    +--------------------+
    3 rows in set (0.00 sec)
    
    mysql> use mysite;
    Database changed
    mysql> show tables;
    Empty set (0.00 sec)
    
    mysql> quit
    Bye
    
  9. Apache, mod_python のインストール
    # yum install httpd-devel
    # wget http://ftp.kddilabs.jp/infosystems/apache/httpd/modpython/mod_python-3.3.1.tgz
    # tar xvzf mod_python-3.3.1.tgz
    # cd mod_python-3.3.1
    # ./configure --with-python=/opt/python2.6/bin/python --with-apxs=/usr/sbin/apxs
    # make
    
  10. Apache, mod_python の設定
    • URL: http://domain.com/mysite/
    • admin URL: http://domain.com/mysite/admin/
    • media URL: http://domain.com/media/
    • django プロジェクト (settings.py) のデプロイ先: /var/www/django/mysite
    • media の展開先: /var/www/html/media
    • log の保存先: /var/log/django/
    Note: /var/www/html はデフォルトで公開されていると想定
    admin media は /path/to/django/contrib/admin/media/ を /var/www/html/ 内に ln-s でシンボリックリンクを作成し、 chown しておく
    # mkdir /var/www/django/mysite
    # mkdir /var/www/django/mysite/.python-eggs
    # chown apache:apache -R /var/www/django/
    # chmod a+x /var/www/django/
    # mkdir /var/www/html/media
    # chown apache:apache -R /var/www/html/
    # mkdir /var/log/django/mysite
    # chown apache:apache -R /var/log/django/
    # chmod a+x /var/log/django/
    # vi /etc/httpd/conf.d/python.conf
    
    +++ 
    <Location /mysite>
        SetHandler python-program
        PythonPath "sys.path + ['/var/www/django']"
        PythonHandler django.core.handlers.modpython
        PythonOption django.root /mysite
        SetEnv PYTHON_EGG_CACHE /var/www/django/mysite/.python-eggs
        SetEnv DJANGO_SETTINGS_MODULE mysite.settings
        PythonDebug On
    </Location>
    +++ 
    
    /etc/init.d/httpd reload
    
  11. アプリケーションのデプロイと起動 SCP なり FTP なり git なり easy_install なりなんでもいいので、アプリケーションを /var/www/django/mysite/ に置く。syncdb したら動きます。
    # cd /var/www/django/mysite/
    # settings.py syncdb
    

admin User を shell で作る
shell でやる

# python manage.py shell
>>> from django.contrib.auth.models import User
>>> user = User(username='admin', email='foo@bar.com', password='パスワード')
>>> user.is_active=True
>>> user.is_staff=True
>>> user.is_superuser=True
>>> user.save()

/mysite/admin を IP アドレスでアクセス制御する
/etc/httpd/conf.d/python.conf を以下のようにする。

<Location "/mysite/">
    SetHandler python-program
    PythonPath "sys.path + ['/var/www/django']"
    PythonHandler django.core.handlers.modpython
    PythonOption django.root /mysite
    SetEnv PYTHON_EGG_CACHE /var/www/django/mysite/.python-eggs
    SetEnv DJANGO_SETTINGS_MODULE mysite.settings
    PythonDebug On
</Location>
<LocationMatch "/mysite/admin/">
    Order deny,allow
    Deny from all
    Allow from "ipaddress"
</LocationMatch>

コメント

このブログの人気の投稿

Python から Win32 API 経由で印刷する

財務諸表 (Financial Statements)

Netflix のスケール - オートメーション編