トップ 一覧 ping 検索 ヘルプ RSS ログイン

Django 運用環境の構築の変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Django 運用環境の構築
[Django][Python]

{{amazon 4048672096}}

*[mod_python マニュアル|http://www.python.jp/doc/contrib/modpython/index.html]

::注意点
+Pythonのバージョンにより mod_python をソースからコンパイルする
+httpd_devel をインストールしないと、mod_python がコンパイルできない
+PostgreSQLの共有ライブラリを /usr/lib 等にコピーして利用できるようにしておく必要あり

!!インストール
!mod_python のインストール
::Apache、Python は以下のバージョン(うまくいかなければ改めてソースから・・・)
*mod_python 3.2.8-3.1
*httpd-2.2.3-5
*python 2.6.2 r262

""Pythonを 2.4 から 2.6にしていたため、yumで取得したバイナリとは Pythonのバージョンが異なり失敗。以下、ソースからコンパイルを行う。2.4 なら問題ない。

 # yum install mod_python
 Loading "installonlyn" plugin
 Setting up Install Process
 Setting up repositories
 Reading repository metadata in from local files
 Parsing package install arguments
 Resolving Dependencies
 --> Populating transaction set with selected packages. Please wait.
 ---> Downloading header for mod_python to pack into transaction set.
 mod_python-3.2.8-3.1.i386 100% |=========================|  26 kB    00:00     
 ---> Package mod_python.i386 0:3.2.8-3.1 set to be updated
 --> Running transaction check
 
 Dependencies Resolved
 
 =============================================================================
  Package                 Arch       Version          Repository        Size 
 =============================================================================
 Installing:
  mod_python              i386       3.2.8-3.1        core              257 k
 
 Transaction Summary
 =============================================================================
 Install      1 Package(s)         
 Update       0 Package(s)         
 Remove       0 Package(s)         
 
 Total download size: 257 k
 Is this ok [y/N]: y
 Downloading Packages:
 (1/1): mod_python-3.2.8-3 100% |=========================| 257 kB    00:00     
 Running Transaction Test
 Finished Transaction Test
 Transaction Test Succeeded
 Running Transaction
   Installing: mod_python                   ######################### [1/1] 
 
 Installed: mod_python.i386 0:3.2.8-3.1
 Complete!

!Windows 用に以下のサイトでバイナリが配布されている
*http://ftp.riken.jp/net/apache/httpd/modpython/win/3.3.1/
""Python2.6 用は提供されていない模様 2010/01/21時点
!!ソースからインストール
*http://httpd.apache.org/modules/python-download.cgi
!ソースのダウンロード
 # wget http://ftp.kddilabs.jp/infosystems/apache/httpd/modpython/mod_python-3.3.1.tgz
!解凍
 # tar xvf mod_python-3.3.1.tgz 
 mod_python-3.3.1/
 mod_python-3.3.1/test/
 mod_python-3.3.1/test/testconf.py.in
 mod_python-3.3.1/test/httpdconf.py
        :
!httpd-devel のインストール
::mod_so.c が存在するかを確認
 # httpd -l
 Compiled in modules:
   core.c
   prefork.c
   http_core.c
   mod_so.c
::httpd-devel のインストール
 # yum install httpd-devel
       :
 i386 0:2.1.22-4 db4-devel.i386 0:4.3.29-9.fc6 expat-devel.i386 0:1.95.8-8.2.1 openldap-devel.i386 0:2.3.30-3.fc6
 Dependency Updated: httpd.i386 0:2.2.6-1.fc6 openldap.i386 0:2.3.30-3.fc6
 Complete!

::apxs がどこにインストールされたかな
 # find / | grep apxs
 /usr/share/man/man8/apxs.8.gz
 /usr/sbin/apxs
!コンパイル
::configure
*apxs および Pythonのバージョンも正しく検知されたっぽい
 # ./configure --with-apxs=/usr/sbin/apxs
           :
 checking for --with-apxs... /usr/sbin/apxs executable, good
           :
 checking for --with-python... no
 checking for python... /usr/local/bin/python
 checking Python version... 2.6
 checking Python install prefix... /usr/local
 checking checking where python libraries are installed... /usr/local/lib/python2.6
 config.status: creating dist/Makefile
           :
::make
 # make
         :
 Compiling for DSO.
         :
 Now su and make install
   (or, if you only want to perform a partial install,
    you can use make install_dso and make install_py_lib)

::make install
 #make install
 Performing DSO installation.
 
 /usr/bin/install -c -d /usr/lib/httpd/modules
 /usr/bin/install -c src/mod_python.so /usr/lib/httpd/modules
 
 Now don't forget to edit your main config and add
     LoadModule python_module /usr/lib/httpd/modules/mod_python.so
 and if your configuration uses ClearModuleList, then also
     AddModule mod_python.c
         :

!!テスト
*mod_python が動作しているかテストする
::Httpサーバーのドキュメントルート /var/www/html/ に、pytest というフォルダを作成し、mptest.py ファイルを置く
*mptest.py
 from mod_python import apache 
 
 def handler(req):
         req.write("mod_python worked!")
         return apache.OK

::/etc/httpd/conf/httpd.conf に以下の記述を追加
 <Directory "/var/www/html/pytest">
     AddHandler mod_python .py
     PythonHandler mptest
     PythonDebug On
 </Directory>

 LoadModule python_module /usr/lib/httpd/modules/mod_python.so


::Apache を再起動し、mptest.pyにアクセス
*再起動
 # /sbin/service httpd restart
 httpd を停止中:                                            [  OK  ]
 httpd を起動中:                                            [  OK  ]

*http://192.168.24.14/pytest/mptest.py
{{ref_image mod_py01.jpg}}
!!Django の設定
!httpd.confの編集
*http://michilu.com/django/doc-ja/modpython/
*http://www.python.jp/doc/contrib/modpython/inst-configure.html

*以下を追記
**[PythonPath ディレクティブ|http://www.python.jp/doc/contrib/modpython/dir-other-pp.html]でプロジェクトのパスを指定
 <Location "/py/mysite">
     SetHandler python-program
     PythonHandler django.core.handlers.modpython
     SetEnv DJANGO_SETTINGS_MODULE mysite.settings
     PythonPath "sys.path+['/var/www/html/py']"
     PythonDebug On
 </Location>

*ソースコードからコンパイルした場合、以下も忘れずに追記
 LoadModule python_module /usr/lib/httpd/modules/mod_python.so

::以下のエラー
""yum でバイナリをインストールしたため、以下のエラー。ソースコードから再インストールを行う。
*現在の環境は、もともとPython2.4 がデフォルトで入っていたところに、[[2.6を入れいている|Django インストール]]が、mod_python 自体もバイナリは Python2.4 でコンパイルされている?
 Mod_python error: "PythonHandler django.core.handlers.modpython"
 
 Traceback (most recent call last):
 
   File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 287, in HandlerDispatch
     log=debug)
 
   File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 461, in import_module
     f, p, d = imp.find_module(parts[i], path)
 
 ImportError: No module named django

*いったん削除
 # yum erase mod_python
       :
 Running Transaction
   Removing  : mod_python                   ######################### [1/1]
 
 Removed: mod_python.i386 0:3.2.8-3.1
 Complete!

![[チュートリアルで作成したアプリケーション|Django 最初のアプリケーション 1]]を呼び出してみる。
*http://192.168.24.14/py/mysite/polls/
::エラー
 ImproperlyConfigured: Error loading psycopg2 module: libpq.so.5: cannot open shared object file: No such file or directory

*[[バックエンドに設定|PostgreSQL 8.3.5 インストール]]した、[PostgreSQLのライブラリを認識していないようだ|http://wiki.minaco.net/index.php?cmd=read&page=Python%2FDjango%2F%E3%83%81%E3%83%A5%E3%83%BC%E3%83%88%E3%83%AA%E3%82%A2%E3%83%AB]

 # cp /usr/local/pgsql/lib/libpq.so.5 /usr/lib
 # ldconfig

!テスト
::mysite.urls.py
 urlpatterns = patterns('',
     (r'^py/mysite/firsttest/$', 'mysite.firsttest.views.index'),
 )

::mysite.firsttest.views.py
 # -*- encoding: utf-8 -*-
 from django.http import HttpResponse
 def index(request):
         return HttpResponse('Hello Django!')
::OK
{{ref_image mod_py02.jpg}}