| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Django 運用環境の構築

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Django 運用環境の構築

Django | Python |

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

インストール

mod_python のインストール

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

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

  1. # yum install mod_python
  2. Loading "installonlyn" plugin
  3. Setting up Install Process
  4. Setting up repositories
  5. Reading repository metadata in from local files
  6. Parsing package install arguments
  7. Resolving Dependencies
  8. --> Populating transaction set with selected packages. Please wait.
  9. ---> Downloading header for mod_python to pack into transaction set.
  10. mod_python-3.2.8-3.1.i386 100% |=========================| 26 kB 00:00
  11. ---> Package mod_python.i386 0:3.2.8-3.1 set to be updated
  12. --> Running transaction check
  13.  
  14. Dependencies Resolved
  15.  
  16. =============================================================================
  17. Package Arch Version Repository Size
  18. =============================================================================
  19. Installing:
  20. mod_python i386 3.2.8-3.1 core 257 k
  21.  
  22. Transaction Summary
  23. =============================================================================
  24. Install 1 Package(s)
  25. Update 0 Package(s)
  26. Remove 0 Package(s)
  27.  
  28. Total download size: 257 k
  29. Is this ok [y/N]: y
  30. Downloading Packages:
  31. (1/1): mod_python-3.2.8-3 100% |=========================| 257 kB 00:00
  32. Running Transaction Test
  33. Finished Transaction Test
  34. Transaction Test Succeeded
  35. Running Transaction
  36. Installing: mod_python ######################### [1/1]
  37.  
  38. Installed: mod_python.i386 0:3.2.8-3.1
  39. Complete!

Windows 用に以下のサイトでバイナリが配布されている

<blockquote>Python2.6 用は提供されていない模様 2010/01/21時点</blockquote>

ソースからインストール

ソースのダウンロード

  1. # wget http://ftp.kddilabs.jp/infosystems/apache/httpd/modpython/mod_python-3.3.1.tgz

解凍

  1. # tar xvf mod_python-3.3.1.tgz
  2. mod_python-3.3.1/
  3. mod_python-3.3.1/test/
  4. mod_python-3.3.1/test/testconf.py.in
  5. mod_python-3.3.1/test/httpdconf.py
  6. :

httpd-devel のインストール

mod_so.c が存在するかを確認
  1. # httpd -l
  2. Compiled in modules:
  3. core.c
  4. prefork.c
  5. http_core.c
  6. mod_so.c
httpd-devel のインストール
  1. # yum install httpd-devel
  2. 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
  3. Dependency Updated: httpd.i386 0:2.2.6-1.fc6 openldap.i386 0:2.3.30-3.fc6
  4. Complete!
apxs がどこにインストールされたかな
  1. # find / | grep apxs
  2. /usr/share/man/man8/apxs.8.gz
  3. /usr/sbin/apxs

コンパイル

configure
  • apxs および Pythonのバージョンも正しく検知されたっぽい
  1. # ./configure --with-apxs=/usr/sbin/apxs
  2. :
  3. checking for --with-apxs... /usr/sbin/apxs executable, good
  4. :
  5. checking for --with-python... no
  6. checking for python... /usr/local/bin/python
  7. checking Python version... 2.6
  8. checking Python install prefix... /usr/local
  9. checking checking where python libraries are installed... /usr/local/lib/python2.6
  10. config.status: creating dist/Makefile
  11. :
make
  1. # make
  2. :
  3. Compiling for DSO.
  4. :
  5. Now su and make install
  6. (or, if you only want to perform a partial install,
  7. you can use make install_dso and make install_py_lib)
make install
  1. #make install
  2. Performing DSO installation.
  3.  
  4. /usr/bin/install -c -d /usr/lib/httpd/modules
  5. /usr/bin/install -c src/mod_python.so /usr/lib/httpd/modules
  6.  
  7. Now don't forget to edit your main config and add
  8. LoadModule python_module /usr/lib/httpd/modules/mod_python.so
  9. and if your configuration uses ClearModuleList, then also
  10. AddModule mod_python.c
  11. :

テスト

  • mod_python が動作しているかテストする
Httpサーバーのドキュメントルート /var/www/html/ に、pytest というフォルダを作成し、mptest.py ファイルを置く
  • mptest.py
  1. from mod_python import apache
  2.  
  3. def handler(req):
  4. req.write("mod_python worked!")
  5. return apache.OK
/etc/httpd/conf/httpd.conf に以下の記述を追加
  1. <Directory "/var/www/html/pytest">
  2. AddHandler mod_python .py
  3. PythonHandler mptest
  4. PythonDebug On
  5. </Directory>
  1. LoadModule python_module /usr/lib/httpd/modules/mod_python.so


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

0347 mod py01.jpg

Django の設定

httpd.confの編集

  1. <Location "/py/mysite">
  2. SetHandler python-program
  3. PythonHandler django.core.handlers.modpython
  4. SetEnv DJANGO_SETTINGS_MODULE mysite.settings
  5. PythonPath "sys.path+['/var/www/html/py']"
  6. PythonDebug On
  7. </Location>
  • ソースコードからコンパイルした場合、以下も忘れずに追記
  1. LoadModule python_module /usr/lib/httpd/modules/mod_python.so
以下のエラー

<blockquote>yum でバイナリをインストールしたため、以下のエラー。ソースコードから再インストールを行う。</blockquote>

  • 現在の環境は、もともとPython2.4 がデフォルトで入っていたところに、2.6を入れいているが、mod_python 自体もバイナリは Python2.4 でコンパイルされている?
  1. Mod_python error: "PythonHandler django.core.handlers.modpython"
  2.  
  3. Traceback (most recent call last):
  4.  
  5. File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 287, in HandlerDispatch
  6. log=debug)
  7.  
  8. File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 461, in import_module
  9. f, p, d = imp.find_module(parts[i], path)
  10.  
  11. ImportError: No module named django
  • いったん削除
  1. # yum erase mod_python
  2. Running Transaction
  3. Removing : mod_python ######################### [1/1]
  4.  
  5. Removed: mod_python.i386 0:3.2.8-3.1
  6. Complete!

チュートリアルで作成したアプリケーションを呼び出してみる。

エラー
  1. ImproperlyConfigured: Error loading psycopg2 module: libpq.so.5: cannot open shared object file: No such file or directory
  1. # cp /usr/local/pgsql/lib/libpq.so.5 /usr/lib
  2. # ldconfig

テスト

mysite.urls.py
  1. urlpatterns = patterns(,
  2. (r'^py/mysite/firsttest/$', 'mysite.firsttest.views.index'),
  3. )
mysite.firsttest.views.py
  1. # -*- encoding: utf-8 -*-
  2. from django.http import HttpResponse
  3. def index(request):
  4. return HttpResponse('Hello Django!')
OK

0348 mod py02.jpg