cacti の文字化けを直す

cactiのグラフの表示のところで、文字化けが発生して
いろいろ試したけど直らなかった。。
んでphpに直書きで無理やり直したけど、そのときのメモ。

graph_image.php

はじめはrrdtoolあたりをずっと見てたんだけど
追ってくと描画をコントロールしてるのはgraph_image.phpみたい。
なんでこいつに無理やりLANGの設定を書いちゃう。

vi /CACTI_HOME/graph_image.php
--------------------------------------------
include("./include/auth.php");
include_once("./lib/rrd.php");
の後ろぐらいに
putenv("LANG=C");
setlocale(LC_ALL, 'C');
を追加
--------------------------------------------

めんどくさいんでLANGはCにしたけど、適当な文字コードを指定したら色々直るかも

MySQL + Q4M インストール ハマリどころまとめ

Q4Mをインストールしたときの作業メモ。
とエラーが数回でたので回避方法メモ。

MySQLのインストール

MySQLのバージョンは5.1以上でないといけないみたいです。
今回はソースからインストールしました。

$ cd /usr/local/src
$ wget http://www-jp.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.46.tar.gz/from/http://ftp.iij.ad.jp/pub/db/mysql/
$ tar zxf mysql-5.1.46.tar.gz
$ cd mysql-5.1.46
$ ./configure \
  --prefix=/usr/local/mysql \
  --with-mysqld-user=mysql \
  --with-unix-socket-path=/tmp/mysql.sock \
  --witad-safe-client \
  --with-extra-charsets=all \
  --with-big-tables \
  --with-readline \
  --with-plugins=max
$ make 
$ make test
$ sudo make install

configureのオプションは色々なのでこれはただの例です。

あとは初期DBを作って起動させておきます。

$ cp -ip /usr/local/src/mysql-5.1.46/support-files/my-large.cnf /etc/my.cnf 
$ cd /usr/local/mysql/
$ sudo ./bin/mysql_install_db 
$ sudo chown -R mysql:mysql var
$ sudo ./bin/mysqld_safe &

これでとりあえず起動できます。

q4mのインストール

公式からダウンロードしてソースからインストします

$ cd /usr/local/src
$ wget http://q4m.31tools.com/dist/q4m-0.9.2.tar.gz
$ tar zxf q4m-0.9.2.tar.gz
$ cd q4m-0.9.2
$ ./configure --prefix=/usr/local/mysql --with-mysql=/usr/local/src/mysql-5.1.46
$ make 
$ make test
$ sudo make install
$ /usr/local/mysql/bin/mysql -uroot < support-files/install.sql 

これでインストールできました。
同封されているrun_tests.plでテストしたり、
MySQLにログインしてshow enginesを打ってインストールできたか確認できます。

mysql> show engines;
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine     | Support | Comment                                                        | Transactions | XA   | Savepoints |
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ndbcluster | NO      | Clustered, fault-tolerant tables                               | NULL         | NULL | NULL       |
| MRG_MYISAM | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| QUEUE      | YES     | Queue storage engine for MySQL                                 | NO           | NO   | NO         |
| CSV        | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| FEDERATED  | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| ARCHIVE    | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance         | NO           | NO   | NO         |
| BLACKHOLE  | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
+------------+---------+----------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)

mysql> quit

EngineにQUEUEが追加されてる。

ハマリどころ

2つほどエラーがでました。

q4mのmakeでエラー

エラー内容は↓な感じです。

ha_queue.cc:32:24: error: mysql_priv.h: No such file or directory
ha_queue.cc:46:4: error: #error "support for 64-bit file offsets is mandatory"
queue_cond.h: In member function 'queue_cond_t::value_t queue_cond_t::pow_func::bop(const queue_cond_t::value_t&, const queue_cond_t::value_t&) const':
queue_cond.h:274: error: 'pow' was not declared in this scope
queue_cond.h: In member function 'void queue_cond_t::set_value(size_t, const queue_cond_t::value_t&)':
queue_cond.h:289: error: 'assert' was not declared in this scope
queue_cond.h: In member function 'queue_cond_t::value_t queue_cond_t::get_value(size_t) const':
queue_cond.h:301: error: 'assert' was not declared in this scope
ha_queue.h: At global scope:
ha_queue.h:39: error: 'uchar' does not name a type
ha_queue.h:72: error: ISO C++ forbids declaration of 'uchar' with no type
ha_queue.h:72: error: expected ';' before '*' token
ha_queue.h:73: error: expected `;' before 'static'
ha_queue.h:79: error: 'my_off_t' does not name a type
ha_queue.h:93: error: 'my_off_t' does not name a type
ha_queue.h:94: error: 'my_off_t' has not been declared
ha_queue.h: In constructor 'queue_row_t::queue_row_t(unsigned int, unsigned int)':
ha_queue.h:55: error: 'assert' was not declared in this scope
ha_queue.h:56: error: 'int4store' was not declared in this scope
ha_queue.h: In member function 'unsigned int queue_row_t::size() const':
ha_queue.h:60: error: 'uint4korr' was not declared in this scope
<省略>
make[2]: *** [libqueue_engine_la-ha_queue.lo] Error 1
make[2]: Leaving directory `/usr/local/src/q4m-0.9.2/src'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/usr/local/src/q4m-0.9.2/src'
make: *** [all-recursive] Error 1

こんな感じです。"error: mysql_priv.h: No such file or directory"と言っているので
単純なヘッダーファイルが見つからないエラーですが、これはconfigureのオプションが間違っていました。
このときのconfigureオプションは

./configure --with-mysql=/usr/local/mysql

としていて--with-mysqlにMySQLのインストールパスを指定していました。
これがまったくの勘違いで--with-mysqlへはソースファイルのパスを指定しなければなりません。
(てかインストールマニュアル読めよ俺)
インストールマニュアルには

--with-mysql=mysql-source-dir
set the directory of MySQL source code (required)
--prefix=mysql-dir
set the directory under which MySQL 5.1 is installed (optional)

と書いてあるのでMySQLのインストールパスは--prefixで指定します。
なので正しくはこうですね。

./configure --prefix=/usr/local/mysql --with-mysql=/usr/local/src/mysql-5.1.46

これで上記エラーは解消できました。

install.sqlしたときのエラー

無事make installまで終わって最後にinstall.sqlを実行したときにエラーがでました。

$ /usr/local/mysql/bin/mysql -uroot < support-files/install.sql 
ERROR 1289 (HY000): The 'plugin' feature is disabled; you need MySQL built with 'HAVE_DLOPEN' to have it working

これはMySQLインストール時のconfigureオプションに"--disable-shared"を付加してた為です。
また、"--with-mysqld-ldflags=-all-static" "--with-client-ldflags=-all-static"辺りのオプションを付けてると
同様のエラーになるかもしれません(こっちは未検証)
どうでもいいけど最後の最後でエラーでてMySQLのconfigureからやり直しって精神的にパネェっす。

ソースファイルをrpm化する

ソースファイル(tarボール)からrpmファイルを作るときは
rpmbuildコマンドでもいいけど、SPECファイルがないとできないとかいろいろ制約がある。

rpm化するためには、もう一個checkinstallコマンドを使うやる方があるのでこっちでやってみた。

インストール

checkinstallはデフォのレポジトリでは入ってないかもしれない。
そんな時は↓のサイトを参考にDAGを入れた後にインストールする。

CentOS 5 で DAG(rpmforge) の yum repository 追加 - Enjoi Blog

(私の環境はCent5(32bitなので)
$ wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
$ sudo rpm -ivh rpmforge-release-0.3.6-1.el5.rf.i386.rpm
$ yum search checkins
$ sudo yum install checkinstall.i386

これでインストールできました。

rpm化する

まぁ例なんでなんでもいいんですけど。
/usr/local/src配下にたまたまphp-5.3.2.tar.gzがあったんでこれでやってみる。


まず展開して

$ tar zxf php-5.3.2.tar.gz
$ cd php-5.3.2

configureまでやる。configureのオプション指定したい場合はここで指定。

$ ./configure
<略>

で無事configureが通ったらcheckinstallコマンドを打つ(root権限)。

$
$ sudo /usr/sbin/checkinstall

checkinstall 1.6.0, Copyright 2002 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.


The package documentation directory ./doc-pak does not exist.
Should I create a default set of package docs?  [y]: y を入力

Preparing package documentation...OK

Please choose the packaging method you want to use.
Slackware [S], RPM [R] or Debian [D]? R を入力(rpmのRね)

Please write a description for the package.
End your description with an empty line or EOF.
>>

**************************************
**** RPM package creation selected ***
**************************************

This package will be built according to these values:

1 -  Summary: [ Package created with checkinstall 1.6.0 ]
2 -  Name:    [ php ]
3 -  Version: [ 5.3.2 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ Applications/System ]
7 -  Architecture: [ i386 ]
8 -  Source location: [ php-5.3.2 ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Provides: [ php ]

Enter a number to change any of them or press ENTER to continue: (エンター)

Installing with make install...

<略(makeをしてる)>

**********************************************************************

 Done. The new package has been saved to

 /usr/src/redhat/RPMS/i386/php-5.3.2-1.i386.rpm
 You can install it in your system anytime using:

      rpm -i php-5.3.2-1.i386.rpm

**********************************************************************

はい。これで/usr/src/redhat/RPMS/i386/配下にrpmファイルができました。

$ ls -l /usr/src/redhat/RPMS/i386/*
-rw-r--r-- 1 root root 4820819  5月  4 20:26 /usr/src/redhat/RPMS/i386/php-5.3.2-1.i386.rpm

あとはこれをrpmコマンドでインストールだけです。

Catalyst + Apache + fastcgi (daemontools)

Catalystを使うときは大概mod_perlで動かしてたんだけど
今回はちょっとfastcgiを使ってみた。

インストール環境

Catalystインストール

Catalyst本体

sudo cpan Catalyst::Runtime
sudo cpan Catalyst::Devel
sudo cpan Catalyst::View::TT

catalyst.pl MyApp

fastcgiインストール

ソースをもってきてインストール

$ wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
$ tar zxf mod_fastcgi-2.4.6.tar.gz 

インストールは「INSTALL.AP2」に載っている通りです。

$ cd mod_fastcgi-2.4.6
$ cp -p Makefile.AP2 Makefile
$ make
$ sudo make install

※apacheのインストディレクトリが/usr/local/apache2以外の場合は
Makefileの

  top_dir      = /usr/local/apache2

を適宜変更。

apacheの設定

apache2.2系はconf/extra配下に各設定ファイルを作ってhttpd.confでインクルードしてるので
Catalystの設定はhttpd-catalyst.confとか作ってやるのが個人的には好きです。

$ pwd
/usr/local/apache2/conf/extra
$ vi httpd-catalyst.conf

設定内容はこんな感じ。

<IfModule mod_fastcgi.c>
    FastCgiExternalServer /tmp/foo.fcgi -socket /tmp/bar.socket -idle-timeout 120
</IfModule>

<VirtualHost *:80>
    ServerName hoge-baz.net
    RewriteEngine on
    RewriteRule ^/(.*) /tmp/foo.fcgi/$1 [L]
    <Files myapp_fastcgi.pl>
        SetHandler fastcgi-script
    </Files>
</VirtualHost>

これでapacheの設定は終わり

Catalystをfastcgiで起動

Catalyst側はMyAppのscript配下にあるmyapp_fastcgi.plを実行してやる。

$ cd MyApp
$ ./script/myapp_fastcgi.pl -n 3 -l '/tmp/bar.socket'

これでソケットを通してapacheとcatalystが連携できました。

daemontoolsを使ってfastcgiを管理する

fastcgiはdaemontoolsを使ってプロセス監視をすると便利です。
daemontoolsそのものは省略。
runファイルはこんな感じ。

#!/bin/sh
exec 2>&1
exec env - \
PATH='/bin:/usr/bin:/usr/local/bin:/usr/local/MyApp/script' \
DBIC_TRACE=1 \
CATALYST_DEBUG=1 \
DBIC_NO_WARN_BAD_PERL=1 \
setuidgid hoge \
myapp_fastcgi.pl -n 3 -l '/tmp/bar.socket'

こうやっとくと例えばプログラムを更新した際は
daemontoolsのコマンド使って

svc -t /service/myapp

ってやればfastcgiが再起動できる

PostgreSQLインストールエラー could not determine argument types

ポスグレ?んなもん片手で入れてやんよ!と意気込んでみたものの、
configure時にエラーでまくり、且つぐぐっても情報が少なかったのでそのときの作業メモ。

インストール環境

  • OS : CentOS 5.4
  • Kernel : 2.6.18-128.1.10.el5

PostgreSQLのバージョンは

です。古いね。

configureをしてみる。

srcファイルを展開後、configureを行うとエラーがでます。

  $ tar zxf postgresql-7.1.3.tar.gz
 $ cd postgresql-7.1.3
  $ ./configure --enable-multibyte=EUC_JP
  <省略>
  checking types of arguments for accept()... configure: error: could not determine argument types
    /\___/ヽ   ヽ
   /    ::::::::::::::::\ つ
  . |  ,,-‐‐   ‐‐-、 .:::| わ
  |  、_(o)_,:  _(o)_, :::|ぁぁ
.   |    ::<      .::|あぁ
   \  /( [三] )ヽ ::/ああ
   /`ー‐--‐‐―´\ぁあ

よくでる依存関係のエラーではなく、直訳するとなんか引数がおかしいとのこと。
正直よくわからん。

でconfigure実行時に出力される「config.log」をみてみると山のようにerrorがはかられてるわけなんだが、
一番はじめにerrorがはかれるとこはここ。

  $ cat config.log
  <略>
  configure:1396: checking for Cygwin environment
  configure:1412: gcc -c -O2  conftest.c 1>&5
  configure: In function 'main':
  configure:1408: error: '__CYGWIN32__' undeclared (first use in this function)
  configure:1408: error: (Each undeclared identifier is reported only once
  configure:1408: error: for each function it appears in.) 

はいはい、configureの1408行目あたりでなんかおかしくなってると。
で、configureの1408行目はこんな感じ。

  if eval "test \"`echo '$''{'ac_cv_cygwin'+set}'`\" = set"; then  echo $ac_n "(cached) $ac_c" 1>&6
  else
    cat > conftest.$ac_ext <<EOF
  #line 1401 "configure"
  #include "confdefs.h"
  
  int main() {
  
  #ifndef __CYGWIN__
  #define __CYGWIN__ __CYGWIN32__
  #endif
  return __CYGWIN__;
  ; return 0; }
  EOF

なんかヘッダーファイル(confdefs.h)をincludeしてますね。
confdes.hを見てみる。

  $ cat confdefs.h 
  
  #define PG_VERSION "7.1.3"
  #define MULTIBYTE 1
  #define DEF_PGPORT 5432
  #define DEF_PGPORT_STR "5432"
  #define DEF_MAXBACKENDS 32
  #define PG_VERSION_STR "PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red   Hat 4.1.2-46)
  Copyright (C) 2006 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
  #define PG_KRB_SRVNAM "postgres"
  #define HAVE_CRYPT_H 1
  #define HAVE_ENDIAN_H 1
  <略>

defineにまじってなんか変なのあるしwww
なんかgccのバージョン取得するところがおかしそうだね。(というかgccのバージョンに対応してなさそう)
実は、同じ7系の7.4.9ではconfigureは普通にインストできてるので、
7.1.3と7.4.9のconfigureでgccをチェックしてるところを比べてみた。

  • 7.1.3
  cc_string="GCC `${CC} --version`"
  • 7.4.9
  cc_string="GCC `${CC} --version | sed q`"

微妙に違う。実際コマンドを打ってみた。

  $ gcc --version
  gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
  Copyright (C) 2006 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  $ 
  $ gcc --version | sed q
  gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
  $ 

ってことらしい(一行で出力させる)。なんで

  $ diff configure.backup configure
  1620c1620
  <   cc_string="GCC `${CC} --version`"
  ---
  >   cc_string="GCC `${CC} --version | sed q`"  $ 

こんな感じで修正して、もう一回configure。

 $ ./configure --enable-multibyte=EUC_JP

今度は無事成功。

makeしてみる

ここまできたらあとは楽勝とか考えてた時期が私にもありました。
makeするとエラーでまくります。そりゃ無理やりconfigure通してんだから出るよね。

make結果はこんなん。

  $ make
  <略>
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../../src/include   -c -o big5.o big5.c
  /usr/bin/ld -r -o SUBSYS.o common.o conv.o mbutils.o wchar.o wstrcmp.o wstrncmp.o big5.o
  make[4]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend/utils/mb' から出ます
  /usr/bin/ld -r -o SUBSYS.o fmgrtab.o adt/SUBSYS.o cache/SUBSYS.o error/SUBSYS.o fmgr/SUBSYS.o hash/SUBSYS.o    init/SUBSYS.o misc/SUBSYS.o mmgr/SUBSYS.o sort/SUBSYS.o time/SUBSYS.o mb/SUBSYS.o
  make[3]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend/utils' から出ます
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations  -Wl,-rpath,/usr/local/pgsql/lib -export-dynamic  access/SUBSYS.o bootstrap/SUBSYS.o catalog/SUBSYS.o parser/SUBSYS.o commands/SUBSYS.o executor/SUBSYS.o   lib/SUBSYS.o libpq/SUBSYS.o main/SUBSYS.o nodes/SUBSYS.o optimizer/SUBSYS.o port/SUBSYS.o postmaster/SUBSYS.o   regex/SUBSYS.o rewrite/SUBSYS.o storage/SUBSYS.o tcop/SUBSYS.o utils/SUBSYS.o -lz -lcrypt -lresolv -lnsl -ldl -  lm -lbsd -lreadline -ltermcap  -o postgres
  /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference in commands/SUBSYS.o
  /lib/libc.so.6: could not read symbols: Bad value
  collect2: ld はステータス 1 で終了しました
  make[2]: *** [postgres] エラー 1
  make[2]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend' から出ます
  make[1]: *** [all] エラー 2
  make[1]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src' から出ます
  make: *** [all] エラー 2
      ∧_∧
     ( ゚ω゚ ) makeは任せろー
 バリバリC□ l丶l丶
     /  (    ) もうやめて!
     (ノ ̄と、   i
            しーJ

でもこっちのエラーはぐぐったら情報が結構でてきたw
なんかerrono変数がわるさしてるらしい。
ヘッダーファイルをいじいじ

  $ diff ./src/include/postgres.h.backup ./src/include/postgres.h
  576,579c576,579
  < #define DATA(x) extern int errno
  < #define DESCR(x) extern int errno
  < #define DECLARE_INDEX(x) extern int errno
  < #define DECLARE_UNIQUE_INDEX(x) extern int errno
  ---
  > #define DATA(x) extern int no_such_variable
  > #define DESCR(x) extern int no_such_variable
  > #define DECLARE_INDEX(x) extern int no_such_variable
  > #define DECLARE_UNIQUE_INDEX(x) extern int no_such_variable
  $ 

もう一回makeしてみる。ヘッダー書き換えたのでmake cleanから

  $ make clean
  $ make
  <省略>
  make[3]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend/commands' に入ります
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../src/include   -c -o async.o async.c
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../src/include   -c -o creatinh.o creatinh.c
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../src/include   -c -o command.o command.c
  command.c: In function ‘AlterTableAddConstraint’:
  command.c:1441: 警告: dereferencing type-punned pointer will break strict-aliasing rules
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../src/include   -c -o comment.o comment.c
  gcc -O2 -Wall -Wmissing-prototypes -Wmissing-declarations -I../../../src/include   -c -o copy.o copy.c
  copy.c: In function ‘DoCopy’:
  copy.c:334: error: ‘errno’ undeclared (first use in this function)
  copy.c:334: error: (Each undeclared identifier is reported only once
  copy.c:334: error: for each function it appears in.)
  copy.c: In function ‘CopyTo’:
  copy.c:582: 警告: dereferencing type-punned pointer will break strict-aliasing rules
  copy.c:582: 警告: dereferencing type-punned pointer will break strict-aliasing rules
  copy.c: In function ‘CopyFrom’:
  copy.c:868: 警告: dereferencing type-punned pointer will break strict-aliasing rules
  copy.c:868: 警告: dereferencing type-punned pointer will break strict-aliasing rules
  copy.c: In function ‘CopyAttributeOut’:
  copy.c:1209: 警告: pointer targets in passing argument 2 of ‘pg_encoding_mblen’ differ in signedness
  make[3]: *** [copy.o] エラー 1
  make[3]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend/commands' から出ます
  make[2]: *** [commands-recursive] エラー 2
  make[2]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/backend' から出ます
  make[1]: *** [all] エラー 2
  make[1]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src' から出ます
  make: *** [all] エラー 2
  $

ふむふむ。
そしたらこんな感じで修正して・・・もう一回makeしてみる。

  $ diff ./src/backend/commands/copy.c.backup ./src/backend/commands/copy.c
  15c15
  < 
  ---
  > #include <errno.h>
  $
  $ make 
  <略>
  make[4]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/pl/plpgsql/src' から出ます
  make[3]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/pl/plpgsql' から出ます
  make[2]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src/pl' から出ます
  make[1]: ディレクトリ `/usr/local/src/postgresql-7.1.3/src' から出ます
  All of PostgreSQL successfully made. Ready to install.
  $ 
    /\___/ヽ
   /    ::::::::::::::::\  き
  . |  ,,-‐‐   ‐‐-、 .:::| た
  |  、_(o)_,:  _(o)_, :::|ぁぁ
.   |    ::<      .::|あぁ
   \  /( [三] )ヽ ::/ああ
   /`ー‐--‐‐―´\ぁあ

あとはmake installして、初期DBつくってできあがり。

もう…ゴールしてもいいよね…

rewriteでメンテモード

メンテする際は、apacheのrewriteを使用してリクエストをメンテページに飛ばす。

rewriteルール

  • 以下↓のルール以外は全てのリクエストをメンテのアナウンスページへ転送する
  • 特定IP(社内とか自分の家とか)の場合は転送しない
  • バーチャルホストで登録されてる他のURLは転送しない
  • 会員登録ページは転送しない

設定

apacheの設定ファイルなんでconfファイルは

  /usr/local/apache2.2/conf/httpd.conf

です。
べつにhttpd.confファイルに直で書いてもいいけど、今後の運用を考えて外部ファイルとする。

httpd.confファイルに以下を追加。

 Include conf/extra/httpd-rewrite.conf

書く場所はどこでもいい。

./conf/extra配下にhttpd-rewrite.confファイルを作成し、rewriteルールを記述する。

 RewriteEngine on
 RewriteCond %{HTTP_HOST} !^hoge\.piyo\.jp$
 RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
 RewriteCond %{REMOTE_ADDR} !^123\.12\.12\.12$
 RewriteCond %{REMOTE_ADDR} !^192\.168\.12\.1$
 RewriteCond %{REQUEST_URI} !^/css/
 RewriteCond %{REQUEST_URI} !^/entry
 RewriteCond %{REQUEST_URI} !^/maintenance/
 RewriteRule ^(.*)$ /maintenance/index.html

こんな感じ。

簡単に解説

 RewriteCond %{HTTP_HOST} !^hoge\.piyo\.jp$

でバーチャルホストだったら普通に表示。

 RewriteCond %{REMOTE_ADDR} !^123\.45\.67\.89$
 RewriteCond %{REMOTE_ADDR} !^123\.12\.12\.12$
 RewriteCond %{REMOTE_ADDR} !^192\.168\.12\.1$

で特定のIPだったら普通に表示。

 RewriteCond %{REQUEST_URI} !^/css/
 RewriteCond %{REQUEST_URI} !^/entry
 RewriteCond %{REQUEST_URI} !^/maintenance/

で登録ページや、メンテナンスページだったら普通に表示。
こじゃれたメンテページでcssつかってたらcssも追加。

 RewriteRule ^(.*)$ /maintenance/index.html

それ以外だったらメンテページを表示させると

Redmine を lighttpd + fcgi で動かす

Redmine(タスク管理ツール)をlighttpdで動かす。
ほんとはapache2.2を入れようと思ってたけど、サーバから新品だし
fcgiが入れやすいlighttpdにした。

システム構成

ソースをダウンロードをするところは基本/usr/local/src配下

Rubyのインストール

Rubyをインストールする。今回はソースからインスト。

  • ソースをダウンロード
 $ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
  • 展開後、3点セット
 $ tar zxvf ruby-1.8.7-p72.tar.gz
 $ cd ruby-1.8.7
 $ ./configure
 $ make
 $ sudo make install
  • インストできたか確認
 $ ruby -v
 ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux]

RubyGemsのインストール

RubyGemsはRubyのパッケージシステム。

  • ソースのダウンロード
 $ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
  • 展開、インストール
 $ tar zxvf rubygems-1.2.0.tgz
 $ cd rubygems-1.2.0
 $ sudo ruby setup.rb
  • インストできたか確認
 $ gem -v
 1.2.0

Ruby on Railsのインストール

↑でいれたgemを利用してインストール

  • インストール
 # gem install rails
  • インストできたか確認
 $ rails -v
 Rails 2.1.1

MySQLのインストール/設定

Redmineのコンフィグみると、mysqlの他、postgresやsqliteも対応してるみたい。
mysqlはめんどくさいのでパッケージでいれる。

  • インストール
 $ sudo yum install mysql.i386 mysql-server.i386 mysql-devel.i386

簡単すぎる…

  • 起動
 $ sudo /etc/init.d/mysqld start
  • rootのパスワードをかえる
 $ sudo mysqladmin -u root password hogehoge
  • Redmine用のDBをつくる。
 $ mysql -u root -p
 mysql> create database redmine default character set utf8;
 Query OK, 1 row affected (0.03 sec)
 
 mysql> grant all privileges on redmine.* to redmineuser@localhost;
 Query OK, 0 rows affected (0.04 sec)
 
 mysql> set password for redmineuser@localhost=password('hoge');
 Query OK, 0 rows affected (0.03 sec)

Redmineのインストール/設定

まぁインストールするディレクトリはどこでもいいんだけど
今回は/usr/local/rails配下とした。

  • ダウンロード
 $ cd /usr/local/rails
 $ svn checkout http://redmine.rubyforge.org/svn/trunk/ Redmine
  • 設定ファイル変更
 $ cd config
 $ mv database.yml.example database.yml
 $ vi database.yml

変更箇所は以下

 production:
  adapter: mysql
  database: redmine
  host: localhost
  username: redmineuser
  password: hoge
  encoding: utf8
  socket: /var/lib/mysql/mysql.sock

socket: 〜の設定とか地味に大切

  • Redmineで使用するDBテーブルを作成する。 
 $ pwd 
 /usr/local/rails/Redmine
 $ rake db:migrate RAILS_ENV="production"

ここでrailsのバージョンは2.1.0じゃないとだめって怒られた…

 $ sudo gem install -v=2.1.0 rails
 $ rake db:migrate RAILS_ENV="production"
 $ rake redmine:load_default_data RAILS_ENV="production"
 Select language: bg, cs, de, en, es, fr, he, it, ja, ko, nl, pl, pt, pt-br, ro, ru, sr, sv, zh, zh-tw [en] ja
                                                                                                           ~~~~
 ====================================
 Loading default configuration data for language: ja
[ja]を選択する
  • WEBrickで動作テスト
 $ ruby script/server -e production -p 80

ブラウザでみてみる。

lighttpdとfcgi

やっとか…これが書きたかっただけなのに…

fcgiのインストール
  • ダウンロードする
 $ wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
  • 展開後、3点セット
 $ tar zxvf fcgi-2.4.0.tar.gz
 $ cd fcgi-2.4.0
 $ ./configure
 $ make
 $ sudo make install
RubyのFastCGIライブラリインストール
  • インスト
 $ sudo gem install fcgi

これは簡単だな

lighttpdのインストール
  • ダウンロード
 $ wget http://www.lighttpd.net/download/lighttpd-1.4.19.tar.gz
  • 展開後、3点セット
 $ tar zxvf lighttpd-1.4.19.tar.gz
 $ cd lighttpd-1.4.19
 $ ./configure --prefix=/usr/local/lighttpd
 $ make 
 $ sudo make install

configureのオプションに--prefix=/usr/local/lighttpd

lighttpdの設定

あと少し!

  • lighttpdのユーザ/グループを追加
 $ sudo /usr/sbin/groupadd -g 8080 lighttpd
 $ sudo /usr/sbin/useradd -u 8080 -g lighttpd

あれ…ユーザ名がlighttpdっておかしいのかな…まぁいいか

  • confの設定
 $ cd /usr/local/lighttpd
 $ sudo mkdir conf
 $ cp /usr/local/src/lighttpd-1.4.19/doc/lighttpd.conf ./conf/
 $ vi ./conf/lighttpd.conf

diffは以下

 *** 12,27 ****
 ! #                               "mod_rewrite",
 ! #                               "mod_redirect",
 ! #                               "mod_alias",
 ! #                               "mod_fastcgi",
 --- 12,27 ----
 !                                 "mod_rewrite",
 !                                 "mod_redirect",
 !                                 "mod_alias",
 !                                 "mod_fastcgi",
 ***************
 *** 37,43 **** 
 ! server.document-root        = "/srv/www/htdocs/"  
 --- 37,45 ----
 ! server.document-root        = "/usr/local/rails/Redmine/public/" 
 ***************
 *** 138,144 ****
 ! #server.port                = 81
 --- 140,146 ----
 ! server.port                = 80 
 ***************
 *** 146,151 ****
 --- 148,154 ----
 + server.error-handler-404   = "/dispatch.fcgi" 
 ***************
 *** 188,196 **** 
 ! #server.groupname           = "wwwrun" 
 --- 191,200 ----
 ! server.username            = "lighttpd" 
 ! server.groupname           = "lighttpd"
 ***************
 *** 324,326 ****
 --- 328,344 ----
 +
 + ## fastcgi module -shiba
 +
 + fastcgi.server = (
 +     ".fcgi" => (
 +         "localhost" => (
 +             "socket" => "/usr/local/rails/Redmine/tmp/sockets/fcgi.socket",
 +             "bin-path" => "/usr/local/bin/ruby /usr/local/rails/Redmine/public /dispatch.fcgi",
 +             "bin-environment" => ("RAILS_ENV"=>"production"),
 +         )
 +     )
 + )
  • railsのオーナー調整

めんどくさい

 $ cd /usr/local/rails
 $ sudo chown -R lighttpd Redmine
起動してみる
#/usr/local/lighttpd/sbin/lighttpd -f /usr/local/lighttpd/conf/lighttpd.conf