2010年2月26日金曜日

RedmineをPostgreSQLで構築

0 コメント
ubuntu8.04 LTS Serverに、データベースをPostgreSQLを使ってRedmineサーバーを構築した時の手順です。
Redmine用のユーザーとデータベースを次の構成で作成します。
ubuntuのユーザー名redmine
redmineのホームディレクトリ/home/redmine
データベース名redmine
データベースのユーザー名redmine

Apache2をインストールします。
$ sudo apt-get install apache2

PostgreSQLをインストールします。
$ sudo apt-get install postgresql
$ su - postgres
$ createuser -P redmine
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
$ createdb redmine -O redmine -E UTF8
$ exit

rubyとruby on Railsをインストールします。
$ cd
$ sudo apt-get install ruby
$ sudo apt-get install libpgsql-ruby
$ sudo apt-get install rdoc irb ri ruby1.8-dev libyaml-ruby libzlib-ruby libopenssl-ruby
$ sudo apt-get install imagemagick librmagick-ruby1.8 build-essential
$ wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
$ tar zxvf rubygems-1.3.5.tgz
$ cd rubygems-1.3.5
$ sudo ruby setup.rb
$ sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
$ sudo gem update --system
$ sudo gem install rails --version 2.1.2
$ sudo gem install postgres-pr
$ sudo gem install rake

Redmineをインストールします。
$ cd
$ wget http://rubyforge.org/frs/download.php/67144/redmine-0.8.7.tar.gz
$ tar zxvf redmine-0.8.7.tar.gz
$ mv redmine-0.8.7 redmine
$ cd redmine
$ cp config/database.yml.example config/database.yml
$ vi config/database.yml
内容を次のように変更します。
production:
  adapter: postgresql
  database: redmine
  host: localhost
  username: redmine
  password: パスワードを指定します。
  encoding: utf8
続けて
$ rake config/initializers/session_store.rb
$ rake db:migrate RAILS_ENV=production
$ rake load_default_data RAILS_ENV=production
(in /home/redmine/rubygems-1.3.5/redmine)

Select language: bg, ca, cs, da, de, en, es, fi, fr, he, hu, it, ja, ko, lt, nl, no, pl, pt, pt-br, ro, ru, sk, sr, sv, th, tr, uk, vn, zh, zh-tw [en] ja
「languageを選択して」といわれるので、jaと入力します。
====================================
Default configuration data loaded.

Note: The rake task load_default_data has been deprecated, please use the replacement version redmine:load_default_data
$ cp config/email.yml.example config/email.yml
$ vi config/email.yml
次のように変更します。
production:
  delivery_method: :smtp
  smtp_settings:
    address: SMTPサーバーを指定します。
    port: SMTPのポートを指定します。
    domain: ドメインを指定します。
    #authentication: :login
    #user_name: redmine@example.net
    #password: redmine

ここで、一旦サーバーを起動してテストしてみます。
$ script/server -e production
ウェブブラウザから、http://サーバーアドレス:3000/にアクセスしてみます。
このとき、ログインユーザーはadminでパスワードもadminです。
無事ログインできたら、Administration→Settingをクリックして、以下の設定を変更します。
  • Default languageをJapaneseにします。
  • Users display formatをAdmin Redmineにします(名前の表示を氏 名の順に表示)。
  • Attachment max. sizeを、添付ファイルの上限サイズに変更します(Kbyte単位)。
  • Host name and pathをホストアドレスにします。
これで、ウェブブラウザを閉じて、ターミナルもCtrl+Cでサーバースクリプトを停止します。

Passenger(Ruby on RailsアプリケーションをApacheで動かすためのソフトウエア)をインストールします。
$ sudo apt-get install apache2-prefork-dev libapr1-dev libaprutil1-dev
$ sudo gem install passenger
$ sudo passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v2.2.8.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.
1
1を入力します。
~中略~
--------------------------------------------
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
   PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
   PassengerRuby /usr/bin/ruby1.8

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.


--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   <virtualhost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public    # <-- be sure to point to 'public'!
      <directory /somewhere/public>
         AllowOverride all              # <-- relax Apache security settings
         Options -MultiViews            # <-- MultiViews must be turned off
      </Directory>
   </VirtualHost>

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.
と表示されたらOK。
Apache2の設定方法も親切に教えてくれるので、それを参考にApache2の設定を行います。
$ cd /etc/apache2/mods-available/
$ sudo vi passenger.load
次のように編集します。
LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8/ext/apache2/mod_passenger.so
続けて
$ sudo vi passenger.conf
次のように編集します。
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.2.8
PassengerRuby /usr/bin/ruby1.8
続けて
$ sudo a2enmod passenger
$ cd /etc/apache2/sites-available/
$ sudo cp default default.org
$ sudo vi default
DocumentRootを
        DocumentRoot /var/www/
から
        DocumentRoot /home/redmine/redmine/public
に変更します。またDocumentRootの下に以下を追加します。
        <directory /home/redmine/redmine/public>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
続けて
$ sudo a2enmod rewrite
$ sudo /etc/init.d/apache2 restart
でapacheを再起動します。
これで、ウェブブラウザでサーバーに接続できればインストール完了です。


[おまけ]
・PDFのCSVの文字化けを回避する方法。
/home/redmine/redmine/lang/ja.ymlを
general_csv_encoding: SJIS
general_pdf_encoding: UTF-8
から
general_csv_encoding: CP932
general_pdf_encoding: CP932
に変更します。

・リポジトリのソースコードのエンコーディングを設定。
管理→設定→リポジトリ→リポジトリのエンコーディングの欄に
SO-2022-JP, EUC-JP, UTF-8, SHIF_JIS, WINDOWS-31J
を設定します。

2010年2月25日木曜日

PostgreSQLをソースからビルド

0 コメント
ubuntu8.04 LTS Serverで、PostgreSQLをソースからビルドする手順です。

コンパイラなどのビルドに必要なパッケージがインストールされていないときは、build-essentialをインストールします。
$ sudo apt-get install build-essential

ビルドに必要なパッケージをインストールします。
sudo apt-get install libreadline5-dev zlib1g-dev

PostgreSQLの管理者のpostgresユーザーを作成します。
$ sudo adduser postgres
Adding user `postgres' ...
Adding new group `postgres' (1001) ...
Adding new user `postgres' (1001) with group `postgres' ...
Creating home directory `/home/postgres' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for postgres
Enter the new value, or press ENTER for the default
        Full Name []:
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [y/N] y

PostgreSQLのディレクトリを作成します。
$ sudo mkdir /usr/local/pgsql
sudo chown postgres:postgres /usr/local/pgsql

ソースをダウンロードしてきます。今回は現時点で最新の8.4.2を使います。
$ cd /usr/local/src
sudo wget ftp://ftp2.jp.postgresql.org/pub/postgresql/source/v8.4.2/postgresql-8.4.2.tar.gz
sudo tar zxvf postgresql-8.4.2.tar.gz
sudo chown -R postgres:postgres postgresql-8.4.2

ユーザーをpostgresに切り替えてビルドします。
$ su - postgres
Password:
$ cd /usr/local/src/postgresql-8.4.2/
$ ./configure
$ make
$ make install

postgresユーザーの環境変数の設定を行います。
~/.profileの末尾に
# User specific environment and startup programs
PGHOME=/usr/local/pgsql
PGDATA=$PGHOME/data
PGLIB=$PGHOME/lib
PATH=$PATH:$HOME/bin:$PGHOME/bin
export PGHOME PGDATA PGLIB PATH
を追加して、
$ source ~/.profile
で環境変数の反映します。

PostgreSQLの初期化を行います。
$ initdb --no-locale

postgresユーザーから元のユーザーに戻ります。
$ exit

/etc/ld.so.confにライブラリを追加します。
sudo vi /etc/ld.so.conf
で、/usr/local/pgsql/libを追加して、
sudo ldconfig -v
を実行します。

OS起動時にPostgreSQLが自動起動するように設定します。
$ sudo cp /usr/local/src/postgresql-8.4.2/contrib/start-scripts/linux /etc/init.d/postgresql
$ sudo chmod 755 /etc/init.d/postgresql
$ sudo update-rc.d postgresql start 90 2 3 4 5 . stop 10 0 1 6 .

PostgreSQLを起動します。
$ sudo /etc/init.d/postgresql start

2010年2月22日月曜日

bashのコマンドラインのヒストリ機能をvi風に

0 コメント
bashのコマンドラインのヒストリは、ctrl+rで検索したりしますが、いまひとつ使いにくいので、vi風のヒストリに変更します。
$ set -o vi
これで、ヒストリがviのようになります。
例えば、過去のコマンドを検索するときは、escキーを押して、/検索文字で検索され、nキーで次候補、shift+nキーで前候補が検索されます。また、escキーを押して、kキーで1つ前のコマンド、jキーで1つ先のコマンドに移動し、hキーとlキーで左右のカーソル移動などなどです。

ただ、毎回 set -o vi と入力するのも面倒なので、~/.inputrcというファイルを用意します。
set editing-mode vi
set keymap vi
これで、ログイン時にヒストリがviモードになります。

デフォルトのエディタを変更

0 コメント
ubuntu8.04 LTS Serverで、デフォルトのエディタを変更する方法です。

$ sudo update-alternatives --config editor
`editor' を提供する 3 個の alternatives があります。

  選択肢       alternative
-----------------------------------------------
          1    /usr/bin/vim.tiny
          2    /bin/ed
*+        3    /bin/nano

デフォルト[*] のままにするには Enter、さもなければ選択肢の番号のキーを押してください:1
Using '/usr/bin/vim.tiny' to provide 'editor'.

PostgreSQLをインストール後に、initdbする。

0 コメント
ubuntuでパッケージからPostgreSQLをインストールした後で、EUC_JPなデータベースを作ろうとするとエラーになるときの対応法です。

ubuntu8.04 LTS ServerにPostgreSQLを次の手順でインストールします。
$ sudo apt-get install postgresql-8.3
postgresユーザーになって、EUC_JPでcreatedbしてみます。
$ su - postgres
$ createdb データベース名 -O オーナー -E EUC_JP
そうすると
createdb: database creation failed: ERROR:  encoding EUC_JP does not match server's locale ja_JP.UTF-8
DETAIL:  The server's LC_CTYPE setting requires encoding UTF8.
と表示され、データベースを作成することが出来ません。

対応方法として、これで正しいのか確証はないですけど、次の手順で対応しました。
$ sudo localedef -i ja_JP -f euc-JP ja_JP.EUC-JP
$ cd /var/lib/postgresql/8.3
$ sudo mv main main_org
$ sudo mkdir main
$ sudo chown -R postgres:postgres main
$ su - postgres
$ /usr/lib/postgresql/8.3/bin/initdb -D /var/lib/postgresql/8.3/main --encoding=UTF8 --no-locale
$ exit
$ sudo mv main main_new
$ sudo mv main_org main
$ sudo /etc/init.d/postgresql-8.3 stop
$ sudo mv main main_org
$ sudo mv main_new main
$ sudo su - root
# cd /var/lib/postgresql/8.3/main
# ln -s /etc/postgresql-common/root.crt root.crt
# ln -s /etc/ssl/certs/ssl-cert-snakeoil.pem server.crt
# ln -s /etc/ssl/private/ssl-cert-snakeoil.key server.key
# exit
$ sudo /etc/init.d/postgresql-8.3 start

改めで、EUC_JPのデータベースを作成してみます。
$ su - postgres
$ createdb xxxx -E EUC_JP
$ psql -l
        List of databases
   Name    |  Owner   | Encoding
-----------+----------+----------
 postgres  | postgres | UTF8
 template0 | postgres | UTF8
 template1 | postgres | UTF8
 xxxx      | postgres | EUC_JP
(4 rows)
として、EUC_JPのデータベースを作成することが出来ました。

2010年2月3日水曜日

oracleインスタントクライアントのインストール

0 コメント
ubuntuで、PHPからoracleに接続するために、oracleインスタントクライアントをインストールします。

まず、OTNから以下のファイルをダウンロードします。
  • Instant Clientパッケージ - Basic
  • Instant Clientパッケージ - SDK
  • Instant Clientパッケージ - SQL*Plus

次の手順でインストールします。
$ sudo mkdir -p /opt/oracle
$ cd /opt/oracle
$ sudo unzip ダウンロードしたInstant Clientパッケージ - Basic
$ sudo unzip ダウンロードしたInstant Clientパッケージ - SDK
$ sudo unzip ダウンロードしたInstant Clientパッケージ - SQL*Plus
$ sudo mv instantclient_10_2 instantclient
$ cd instantclient
$ sudo ln -s libclntsh.so.10.1 libclntsh.so
$ sudo ln -s libocci.so.10.1 libocci.so
$ sudo su -
# echo /opt/oracle/instantclient > /etc/ld.so.conf.d/oracle-instantclient
# exit
$ sudo pecl install oci8
画面に
 1. Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : autodetect

1-1, 'all', 'abort', or Enter to continue:
と表示されるので、all と入力します。

続いて、
Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client [autodetect] :
と表示されるので、 instantclient,/opt/oracle/instantclient と入力します。

続いて、
 1. Please provide the path to the ORACLE_HOME directory. Use 'instantclient,/path/to/instant/client/lib' if you're compiling with Oracle Instant Client : instantclient,/opt/oracle/instantclient

1-1, 'all', 'abort', or Enter to continue:
と表示されるので、エンターキーを押します。

しばらくすると、
Build process completed successfully
Installing '/usr/lib/php5/20060613/oci8.so'
install ok: channel://pecl.php.net/oci8-1.3.5
configuration option "php_ini" is not set to php.ini location
You should add "extension=oci8.so" to php.ini
と表示されればOK。

PHPから利用できるように
$ sudo su -
# echo extension=oci8.so >> /etc/php5/apache2/php.ini
# exit
$ sudo /etc/init.d/apache2 restart
を行い、apacheを再起動します。

tnsnames.oraにサーバー情報を追加します。
$ cd /opt/oracle
$ sudo vi tnsnames.ora

環境変数の設定を追加します。
$ cd ~/
$ vi .profile
末尾に
LD_LIBRARY_PATH=/opt/oracle/instantclient
PATH=/opt/oracle/instantclient:$PATH
NLS_LANG=JAPANESE_JAPAN.AL32UTF8
TNS_ADMIN=/opt/oracle
export LD_LIBRARY_PATH PATH TNS_ADMIN NLS_LANG
を追加します。
$ source ~/.profile
を行って環境変数を設定してから
$ sqlplus ユーザー/パスワード@接続文字
で接続できれば完了!

[補足]
phpinfo()の結果を見ると、NLS_LANGとLD_LIBRARY_PATH等の環境変数の設定が反映されていないので、
$ sudo vi /etc/init.d/apache2
で、
ENV="env -i LANG=C PATH=/usr/local/bin:/usr/bin:/bin"


ENV="env -i LANG=C PATH=/opt/oracle/instantclient:/usr/local/bin:/usr/bin:/bin NLS_LANG=JAPANESE_JAPAN.AL32UTF8 LD_LIBRARY_PATH=/opt/oracle/instantclient TNS_ADMIN=/opt/oracle"

と修正して、apacheを再起動した。

あと、もしかしたら
$ sudo dpkg-reconfigure apache
したかも。

2010年2月1日月曜日

ユーザーにsudoを許可する

0 コメント
ubuntuでは、su - rootは使わず、sudoでやるのが一般的らしい。
ただ最初はインストール時に作ったユーザーのみがsudoできるようで、あとで追加したユーザーがsudoできるようにする方法のまとめ。

sudoに関する設定は /etc/sudoers に記述してあるようで、インストール直後は
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults        env_reset

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Uncomment to allow members of group sudo to not need a password
# (Note that later entries override this, so you might need to move
# it further down)
# %sudo ALL=NOPASSWD: ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
というようになっていて最後の、%admin ALL=(ALL) ALL という部分が「ユーザーがadminというグループに属していればsudoが使える」という設定らしい。
なので、
$ sudo usermod -G admin ユーザー
コマンドで、ユーザーをadminグループに属させることでOK。