OSQAの鯖を立ててみる(trunk失敗)

基本は、以下のいしもとさんの情報をなぞるだけ。
http://d.hatena.ne.jp/atsuoishimoto/20110510/1305012665
ターゲットは、AtomなQNAPマシン。
ずっと前にredmine立てる勉強してたときに使ってたもの。
http://d.hatena.ne.jp/kinneko/20110527/p6
http://d.hatena.ne.jp/kinneko/20110527/p1
http://d.hatena.ne.jp/kinneko/20110526/p36
http://d.hatena.ne.jp/kinneko/20110526/p37
去年の5月なのね...
久しぶりに電源を入れたら、236日起動してないってfsckかかったよ。
確かミニマムインストールから、パッケージ追加していったやつだったよな。
なので、sudoも入っていない。
とりあえず、squeezeではあるので、apt-get update && upgradeする。
さすがに時計は数分ズレている。
ntpdateを追加インストールして時刻合わせする。

# ntpdate ntp.ring.gr.jp
# hwclock -w

ホスト名がredmineなので、OSQAに変更する。

# vi /etc/hostname
# vi /etc/hosts

kernelも上がったので、再起動しておく。
redmineの残骸を削除。

root@OSQA:~# apt-get remove --purge redmine redmine-sqlite
root@OSQA:~# apt-get autoremove

残パッケージは216ある。
まだ多い気がするけど、とりあえずこんなもんで進める。

python 2.6以降

OSQAは、Python/Django上で動作する。
Pythonのバージョン確認。2.6以降が必要。
pythonはいってねぇ...
入れる。

root@OSQA:~# apt-get install python
The following NEW packages will be installed:
  python python-minimal python2.6 python2.6-minimal

python-setuptoolsやdjangoを入れる。

root@OSQA:~# apt-get install python-setuptools
The following NEW packages will be installed:
  python-pkg-resources python-setuptools
root@OSQA:~# apt-get install python-django
The following NEW packages will be installed:
  javascript-common libjs-jquery python-django python-support wwwconfig-common

easy_installで、必要なパッケージを自動インストールする。

root@OSQA:~# easy_install South django django-debug-toolbar markdown html5lib python-openid
install_dir /usr/local/lib/python2.6/dist-packages/
Searching for South
Reading http://pypi.python.org/simple/South/
Reading http://south.aeracode.org/
Reading http://south.aeracode.org/wiki/Download
Best match: South 0.7.3
Downloading http://pypi.python.org/packages/source/S/South/South-0.7.3.tar.gz#md5=32bd32fd98a912547976c2434703109e
Processing South-0.7.3.tar.gz
Running South-0.7.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-0pNbUu/South-0.7.3/egg-dist-tmp-EFQOC1
zip_safe flag not set; analyzing archive contents...
south.migration.base: module references __file__
south.tests.__init__: module references __file__
Adding South 0.7.3 to easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/South-0.7.3-py2.6.egg
Processing dependencies for South
Finished processing dependencies for South
Searching for django
Best match: Django 1.2.3
Adding Django 1.2.3 to easy-install.pth file

Using /usr/lib/pymodules/python2.6
Processing dependencies for django
Finished processing dependencies for django
Searching for django-debug-toolbar
Reading http://pypi.python.org/simple/django-debug-toolbar/
Reading http://rob.cogit8.org/blog/2008/Sep/19/introducing-django-debug-toolbar/
Reading http://github.com/robhudson/django-debug-toolbar/downloads
Reading http://robhudson.github.com/django-debug-toolbar/
Reading https://github.com/django-debug-toolbar/django-debug-toolbar
Reading https://github.com/django-debug-toolbar/django-debug-toolbar/downloads
Best match: django-debug-toolbar 0.9.1
Downloading http://pypi.python.org/packages/source/d/django-debug-toolbar/django-debug-toolbar-0.9.1.tar.gz#md5=bd6a888e1ea147e59f021be4e8d149c3
Processing django-debug-toolbar-0.9.1.tar.gz
Running django-debug-toolbar-0.9.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-uezLfL/django-debug-toolbar-0.9.1/egg-dist-tmp-EVpX4Q
Adding django-debug-toolbar 0.9.1 to easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/django_debug_toolbar-0.9.1-py2.6.egg
Processing dependencies for django-debug-toolbar
Finished processing dependencies for django-debug-toolbar
Searching for markdown
Reading http://pypi.python.org/simple/markdown/
Reading http://www.freewisdom.org/projects/python-markdown
Reading https://sourceforge.net/project/showfiles.php?group_id=153041
Reading http://www.freewisdom.org/projects/python-markdown/
Best match: Markdown 2.1.0
Downloading http://pypi.python.org/packages/source/M/Markdown/Markdown-2.1.0.zip#md5=22f580f3cbd7ed94321ef8ed1a51e784
Processing Markdown-2.1.0.zip
Running Markdown-2.1.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-LJyFk7/Markdown-2.1.0/egg-dist-tmp-r3Vbuq
zip_safe flag not set; analyzing archive contents...
Adding Markdown 2.1.0 to easy-install.pth file
Installing markdown_py script to /usr/local/bin

Installed /usr/local/lib/python2.6/dist-packages/Markdown-2.1.0-py2.6.egg
Processing dependencies for markdown
Finished processing dependencies for markdown
Searching for html5lib
Reading http://pypi.python.org/simple/html5lib/
Reading http://code.google.com/p/html5lib/
Best match: html5lib 0.90
Downloading http://html5lib.googlecode.com/files/html5lib-0.90.zip
Processing html5lib-0.90.zip
Running html5lib-0.90/setup.py -q bdist_egg --dist-dir /tmp/easy_install-R4C5Tq/html5lib-0.90/egg-dist-tmp-89xM_m
zip_safe flag not set; analyzing archive contents...
Adding html5lib 0.90 to easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/html5lib-0.90-py2.6.egg
Processing dependencies for html5lib
Finished processing dependencies for html5lib
Searching for python-openid
Reading http://pypi.python.org/simple/python-openid/
Reading http://www.openidenabled.com/openid/libraries/python/
Reading http://github.com/openid/python-openid
Reading http://github.com/openid/python-openid/tarball/2.2.5
Reading http://openidenabled.com/python-openid/
Reading http://github.com/openid/python-openid/tarball/2.2.4
Best match: python-openid 2.2.5
Downloading http://pypi.python.org/packages/source/p/python-openid/python-openid-2.2.5.zip#md5=f89d9d4f4dccfd33b5ce34eb4725f751
Processing python-openid-2.2.5.zip
Running python-openid-2.2.5/setup.py -q bdist_egg --dist-dir /tmp/easy_install-IZRTUf/python-openid-2.2.5/egg-dist-tmp-WML6OE
warning: no files found matching 'CHANGELOG'
warning: no files found matching '*.css' under directory 'doc'
warning: no files found matching '*.html' under directory 'doc'
zip_safe flag not set; analyzing archive contents...
Adding python-openid 2.2.5 to easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/python_openid-2.2.5-py2.6.egg
Processing dependencies for python-openid
Finished processing dependencies for python-openid

DBを確認

今回は、SQLiteを使うので、入っているかチェック。

root@OSQA:~# dpkg -l | grep sqlite
ii  libaprutil1-dbd-sqlite3             1.3.9+dfsg-5                 The Apache Portable Runtime Utility Library - SQLite3 Driver
ii  libsqlite3-0                        3.7.3-1                      SQLite 3 shared library

OSQA最新バージョンの確認

http://www.osqa.net/download/
SVNから落とせとある。
そーなのか...
まず、svnを入れないと。

root@OSQA:~# apt-get install subversion
The following NEW packages will be installed:
  libneon27-gnutls libserf-0-0 libsvn1 libxml2 sgml-base subversion xml-core

OSQAを、どこに入れるか。
/home/OSQAか。
パーミッションはどうしたらいいのかな。
めんどくさいので専用ユーザーでも作るか。

root@OSQA:~# adduser osqa
root@OSQA:~# su - osqa
osqa@OSQA:~$ 

OSQAをチェックアウト。

osqa@OSQA:~$ svn co http://svn.osqa.net/svnroot/osqa/trunk .
(snip)
Checked out revision 1218.

落ちたのは、こんな感じ。

osqa@OSQA:~$ ls
cron           INSTALL  manage.py       settings_local.py.dist
forum          LICENSE  osqa.iml        settings.py
forum_modules  locale   osqa.wsgi.dist  tmp
__init__.py    log      rmpyc           urls.py

キャッシュディレクトリを作成。

osqa@OSQA:~$ mkdir cache

settings_local.py.distファイルをコピーし、設定ファイルを作成する。

osqa@OSQA:~$ cp settings_local.py.dist settings_local.py
osqa@OSQA:~$ vi settings_local.py

フォーム形式が少し変わっている。
入っているdjangoは、1.2.3-3+squeeze2。
とりあえず、こんな感じで。

osqa@OSQA:~$ diff -Naur settings_local.py.dist settings_local.py
--- settings_local.py.dist	2012-01-05 17:02:38.578122409 +0900
+++ settings_local.py	2012-01-05 17:16:02.132614872 +0900
@@ -26,7 +26,7 @@
 
 DATABASES = {
     'default': {
-        'ENGINE': 'django.db.backends.mysql',
+        'ENGINE': 'django.db.backends.sqlite',
         'NAME': 'osqa',
         'USER': 'root',
         'PASSWORD': '',
@@ -45,14 +45,14 @@
 APP_URL = 'http://'
 
 #LOCALIZATIONS
-TIME_ZONE = 'America/New_York'
+TIME_ZONE = 'Asia/Tokyo'
 
 #OTHER SETTINGS
 
 USE_I18N = True
-LANGUAGE_CODE = 'en'
+LANGUAGE_CODE = 'ja'
 
-DJANGO_VERSION = 1.1
+DJANGO_VERSION = 1.2
 OSQA_DEFAULT_SKIN = 'default'
 
 DISABLED_MODULES = ['books', 'recaptcha', 'project_badges']

DBの初期化

あー、ここでsudoいるのね... orz

root@OSQA:~# apt-get install sudo
root@OSQA:~# visudo 
osqa	ALL=(ALL) ALL

初期化。

osqa@OSQA:~$ sudo python manage.py syncdb --all
Error was: No module named sqlite.base

ダメか。
適当に名前書いたしな(^^;。
これか?

        'ENGINE': 'django.db.backends.sqlite3',
osqa@OSQA:~$ sudo python manage.py syncdb --all
Traceback (most recent call last):
  File "/home/osqa/forum_modules/mysqlfulltext/__init__.py", line 8, in <module>
    import MySQLdb
ImportError: No module named MySQLdb
Traceback (most recent call last):
  File "/home/osqa/../osqa/forum_modules/mysqlfulltext/__init__.py", line 8, in <module>
    import MySQLdb
ImportError: No module named MySQLdb
Syncing...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table forum_keyvalue
Creating table forum_user
Creating table forum_userproperty
Creating table forum_subscriptionsettings
Creating table forum_validationhash
Creating table forum_authkeyuserassociation
Creating table forum_tag
Creating table forum_markedtag
Creating table forum_node_tags
Creating table forum_node
Creating table forum_noderevision
Creating table forum_nodestate
Creating table forum_action
Creating table forum_actionrepute
Creating table forum_questionsubscription
Creating table forum_vote
Creating table forum_flag
Creating table forum_badge
Creating table forum_award
Creating table forum_openidnonce
Creating table forum_openidassociation
Creating table south_migrationhistory

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no

ここではnoを入れてファーストユーザーが管理者になるようにする。
以下、続き。

Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
Installing index for admin.LogEntry model
Installing index for forum.UserProperty model
Installing index for forum.ValidationHash model
Installing index for forum.AuthKeyUserAssociation model
Installing index for forum.Tag model
Installing index for forum.MarkedTag model
Installing index for forum.Node_tags model
Installing index for forum.Node model
Installing index for forum.NodeRevision model
Installing index for forum.NodeState model
Installing index for forum.Action model
Installing index for forum.ActionRepute model
Installing index for forum.QuestionSubscription model
Installing index for forum.Vote model
Installing index for forum.Flag model
Installing index for forum.Award model
No fixtures found.

Synced:
 > django.contrib.auth
 > django.contrib.contenttypes
 > django.contrib.sessions
 > django.contrib.sites
 > django.contrib.admin
 > django.contrib.sitemaps
 > django.contrib.markup
 > forum
 > south

Not synced (use migrations):
 - 
(use ./manage.py migrate to migrate these)

データベースにデータを登録する。

osqa@OSQA:~$ sudo python manage.py migrate forum --fake

なんかエラーでてますけど、使ってないのになぁ... 放置してみる。

ImportError: No module named MySQLdb

念のため、osqaユーザーをsudoできないようにしておく。

起動してみる

ダメな予感はするけど(^^;。

osqa@OSQA:~$ django-admin runserver --settings=settings

Error: Could not import settings 'settings' (Is it on sys.path? Does it have syntax errors?): No module named settings

あらそう。

osqa@OSQA:~$ django-admin runserver --settings=./settings
Traceback (most recent call last):
  File "/usr/bin/django-admin", line 5, in <module>
    management.execute_from_command_line()
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 429, in execute_from_command_line
    utility.execute()
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 252, in fetch_command
    app_name = get_commands()[subcommand]
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 101, in get_commands
    apps = settings.INSTALLED_APPS
  File "/usr/lib/pymodules/python2.6/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 40, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 73, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/pymodules/python2.6/django/utils/importlib.py", line 28, in import_module
    raise TypeError("relative imports require the 'package' argument")
TypeError: relative imports require the 'package' argument

あーあ、ダメだわ。
これはpython 2.7の問題だな。
django/utils/importlib.py
https://code.djangoproject.com/ticket/8193
https://code.djangoproject.com/browser/django/trunk/django/utils/importlib.py#L18
今の環境では無関係か。
なるほど、これかな?

osqa@OSQA:~$ cat osqa.wsgi.dist 
import os
import sys
sys.path.append('/path/to_dir_above')
sys.path.append('/path/to_dir_above/osqa')
# The first part of this module name should be identical to the directory name
# of the OSQA source.  For instance, if the full path to OSQA is
# /home/osqa/osqa-server, then the DJANGO_SETTINGS_MODULE should have a value
# of 'osqa-server.settings'.
os.environ['DJANGO_SETTINGS_MODULE'] = 'osqa.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

利用可能にして編集する。

osqa@OSQA:~$ cp osqa.wsgi.dist osqa.wsgi
osqa@OSQA:~$ vi osqa.wsgi

んでも、どう編集したらいいのだ?
これも古い書式だな。
http://wiki.osqa.net/display/docs/Installing+OSQA+on+WebFaction#InstallingOSQAonWebFaction-Step4%3ATheSettingsFile

osqa@OSQA:~$ diff -Naur osqa.wsgi.dist osqa.wsgi
--- osqa.wsgi.dist	2012-01-05 17:02:38.582120984 +0900
+++ osqa.wsgi	2012-01-05 18:09:33.665113729 +0900
@@ -1,7 +1,7 @@
 import os
 import sys
-sys.path.append('/path/to_dir_above')
-sys.path.append('/path/to_dir_above/osqa')
+sys.path.append('/home/osqa')
+#sys.path.append('/path/to_dir_above/osqa')
 # The first part of this module name should be identical to the directory name
 # of the OSQA source.  For instance, if the full path to OSQA is
 # /home/osqa/osqa-server, then the DJANGO_SETTINGS_MODULE should have a value

とりあえず、こんなんでどを?

+sys.path.append('/home/osqa/osqa')

これでは?
ダメだな。
これじゃないのか...


んなら、呼んで試すか。

osqa@OSQA:~$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import settings
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "settings.py", line 59, in <module>
    from settings_local import *
  File "settings_local.py", line 12, in <module>
    format='%(pathname)s TIME: %(asctime)s MSG: %(filename)s:%(funcName)s:%(lineno)d %(message)s',
  File "/usr/lib/python2.6/logging/__init__.py", line 1402, in basicConfig
    hdlr = FileHandler(filename, mode)
  File "/usr/lib/python2.6/logging/__init__.py", line 827, in __init__
    StreamHandler.__init__(self, self._open())
  File "/usr/lib/python2.6/logging/__init__.py", line 846, in _open
    stream = open(self.baseFilename, self.mode)
IOError: [Errno 13] Permission denied: '/home/osqa/log/django.osqa.log'

をや、まぢでこれ?

osqa@OSQA:~$ ls -al log/django.osqa.log 
-rw-r--r-- 1 root root 0 Jan  5 17:25 log/django.osqa.log

これか...

root@OSQA:~# chown osqa:osqa /home/osqa/log/django.osqa.log
osqa@OSQA:~$ ls -al log/django.osqa.log 
-rw-r--r-- 1 osqa osqa 0 Jan  5 17:25 log/django.osqa.log

違うな...


もっかい。

>>> import settings
Traceback (most recent call last):
  File "forum_modules/mysqlfulltext/__init__.py", line 8, in <module>
    import MySQLdb
ImportError: No module named MySQLdb

あ、これは前にも出てたな。


こっちかな?

osqa@OSQA:~$ PYTHONHOME=/home/osqa django-admin runserver --settings=settings
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "/usr/bin/django-admin", line 2, in <module>
    from django.core import management
ImportError: No module named django.core

ダメか。
site.py編集しちゃおう。

root@OSQA:~# find / | grep site.py
/usr/share/pyshared/site.py
/usr/lib/python2.5/site-packages/site.py
/usr/lib/python2.6/site.py
/usr/lib/python2.6/site.pyc
/usr/lib/python2.6/dist-packages/site.py
/usr/lib/python2.6/dist-packages/site.pyc

有効なのは、どれだ?
/usr/lib/python2.6/dist-packages/site.py -> ../../../share/pyshared/site.py
なので、以下の2つか。
/usr/lib/python2.6/site.py
/usr/share/pyshared/site.py
ファイルの中を見ると、前者だということがわかる。
でも、マニュアルで追加するような部分はないね。
ここが無難か。

osqa@OSQA:~$ vi settings_local.py
import os.path
import sys
sys.path.append('/home/osqa')
sys.path.append('/home/osqa/osqa')

ImportError: No module named MySQLdbに戻っただけ。
起動はローカルディレクトリなんだから、参照はしているよな。
無駄足か。


観念して入れるか。

root@OSQA:~# apt-get install python-mysqldb
The following NEW packages will be installed:
  libmysqlclient16 mysql-common python-mysqldb

settingsのimportは通るようになった。

osqa@OSQA:~$ django-admin runserver --settings=./settings
(snip)
TypeError: relative imports require the 'package' argument

ダメだわ。
django/utils/importlib.pyで、import_module(name, package=None):するときに、packageが渡ってない?
入れたdjangoは2.7用に書き換えられていて、2.6だとダメなのかもしれないな。


djangoはちゃんと入っているのか?

>>> import django
>>> django.VERSION
(1, 2, 3, 'final', 0)

問題なさそう。


起動はする?

osqa@OSQA:~$ python manage.py runserver
Validating models...
0 errors found

Django version 1.2.3, using settings 'osqa.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

manage.py からの起動は大丈夫そうね。
django-adminからは、なんで動かないのかな...