PostgreSQL8+PHP5+Apache2インストール

PostgreSQL8.0の解説[�O�������N]
Apache2.2の解説[�O�������N]

PostgreSQLとMySQLの比較[�O�������N]

注意事項

バージョン8.1.4, 8.0.8, 7.4.13, 7.3.15以前のバージョンにはマルチバイト処理にSQLインジェクションの脆弱性があります。
詳細(SRA OSS)[�O�������N]
詳細(ITpro)[�O�������N]

PostgreSQLのインストール

PostgreSQL,PHP,ApacheのRPMは全て削除しておきます
バージョンアップする場合はバックアップを取って止めておきましょう

postgres ユーザーの作成

# /usr/sbin/useradd -d /usr/local/pgsql postgres
# chmod 755 /usr/local/pgsql

作業ディレクトリ作成

# mkdir /usr/local/src/postgresql-8.1.3
# chown postgres /usr/local/src/postgresql-8.1.3

ソース取得 http://www.postgresql.jp/[�O�������N]
常に最新バージョンを確認しましょう
通信を暗号化する場合は ./configure --with-openssl とする

# su postgres
$ tar xvzf postgresql-8.1.3.tar.gz
$ cd postgresql-8.1.3
$ ./configure
$ gmake
$ gmake check
$ gmake install

SSLの設定

$ cd
$ cd data
$ openssl req -new -text -out server.req
Generating a 1024 bit RSA private key
...++++++
.....++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:JP
State or Province Name (full name) [Berkshire]:Shiga
Locality Name (eg, city) [Newbury]:Otsu
Organization Name (eg, company) [My Company Ltd]:Self
Organizational Unit Name (eg, section) []:Engineer
Common Name (eg, your name or your server's hostname) []:example.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

パスフレーズの削除

$ openssl rsa -in privkey.pem -out server.key
$ rm -f privkey.pem

自己署名

$ openssl req -x509 -in server.req -text -key server.key -out server.crt
$ chmod og-rwx server.key

pg_hba.conf にhostsslを記述
postgresql.conf のsslをonにする

クライアント側にもPostgreSQLを --with-openssl 付きでインストールしておく
起動する必要は無し

パスの設定

postgresとPostgreSQLを使用するユーザー全ての.bashrcに以下を追加

PATH="$PATH":/usr/local/pgsql/bin
export POSTGRES_HOME=/usr/local/pgsql
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/data
export MANPATH="$MANPATH":$POSTGRES_HOME/man
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIB"

設定の反映

$ source ~/.bashrc

データベース初期化

$ initdb --encoding=EUC_JP --no-locale

一行追加

# vi /etc/ld.so.conf
/usr/local/pgsql/lib
# /sbin/ldconfig

自動起動の設定

# cp /usr/local/src/postgresql-8.1.3/contrib/start-scripts/linux /etc/rc.d/init.d/postgresql
# chmod 755 /etc/rc.d/init.d/postgresql
# /sbin/chkconfig --add postgresql
# /etc/rc.d/init.d/postgresql start

ログ管理の設定

$ vi /usr/local/pgsql/data/postgresql.conf
redirect_stderr = on
log_filename = '%m-%d.log'
log_truncate_on_rotation = on
log_min_error_statement = error
log_line_prefix = '<%t %u %d %p>'

自動VACUUMの設定

PostgreSQL8.1からは自動でVACUUMが実行されるように設定できるようになった

$ vi /usr/local/pgsql/data/postgresql.conf
stats_row_level = on
autovacuum = on

Apacheのインストール

http://www.apache.jp/[�O�������N]

MPMはworkerもしくはperchildだとPHPが安定動作しないらしい
参考[�O�������N]

SSLを有効にする場合は --enable-ssl
参考[�O�������N]

# tar xvzf httpd-2.2.2.tar.gz
# cd httpd-2.2.2
# ./configure --with-mpm=prefork --enable-rewrite
# make
# make install

Apacheの設定

Apache2.0系の場合

vi /usr/local/apache2/conf/httpd.conf
ServerTokens Prod
ServerSignature Off
TraceEnable off

Apache2.2系の場合

Apache2.2系から目的別の細かい設定は別ファイルに分離されている。
apache2/conf/extra/ 内に httpd-xxxxxxx.conf の形式でファイルが存在する。
これらを読み込むためには httpd.conf 内で該当箇所のコメントを外す必要がある
今回設定する項目があるのは httpd-default.conf

vi /usr/local/apache2/conf/httpd.conf

394行目あたり

Include conf/extra/httpd-default.conf

httpd.conf で読み込ませたファイルを編集

vi /usr/local/apache2/conf/extra/httpd-default.conf
ServerTokens Prod
ServerSignature Off
TraceEnable off

「TraceEnable」は最初書いてないかも
その場合「ServerSignature」の下にでも追加しとけばいいだろう

自動起動の設定

# cp /usr/local/apache2/bin/apachectl /etc/rc.d/init.d/httpd
# chmod 755 /etc/rc.d/init.d/httpd
# vi /etc/rc.d/init.d/httpd

以下を追加

#!/bin/sh
# chkconfig: - 85 15
# description: Apache is a World Wide Web server.  It is used to serve \
#              HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf
# /sbin/chkconfig --add httpd
# /etc/rc.d/init.d/httpd start

ログのローテーション設定

ログをZIP圧縮する場合 compress

# vi /etc/logrotate.d/apache
/usr/local/apache2/logs/*_log
{
  rotate 4
  compress
  weekly
  sharedscripts
  postrotate
  /usr/local/apache2/bin/apachectl restart
  endscript
}
# /usr/sbin/logrotate -f /etc/logrotate.d/apache

コマンドにて /usr/local/apache2/logs 内に access_log.1 error_log.1 が作成されていればOK

PHP5のインストール

PHPめもも参考にして下さい :D

Amazon ECSなどを利用する場合はSimpleXML関数を有効にしておく
--enable-simplexml

cURL[�O�������N]を利用する場合は先にインストールしておく
RPMで入っていればOK
--with-curl

MySQLを使用する場合は
--with-mysql

# tar xvzf php-5.1.4.tar.gz
# cd php-5.1.4
# ./configure --with-pgsql --with-apxs2=/usr/local/apache2/bin/apxs \
--enable-zend-multibyte --enable-mbstring --with-gd \
--with-zlib-dir=/usr --with-jpeg-dir=/usr --with-png-dir=/usr \
--with-freetype-dir=/usr --with-ttf --enable-gd-native-ttf \
--enable-gd-jis-conv --enable-sockets --enable-simplexml --with-curl \
--with-mysql

php.iniの設定

# cp /usr/local/src/php-5.1.4/php.ini-dist /usr/local/lib/php.ini
;; 出力バッファリングを有効にする
output_buffering = On 

;; mb_output_handlerによる出力変換を有効にする 
output_handler = mb_output_handler

magic_quotes_gpc = Off

;; HTTPヘッダ charset を設定
default_charset = Shift_JIS

;;日本語主体で使用
mbstring.language = Japanese

;; 内部エンコーディングをEUC-JPに設定
mbstring.internal_encoding = EUC-JP 

;; HTTP入力エンコーディング変換をautoに設定
mbstring.http_input = auto 

;; SJISに変換
mbstring.http_output = SJIS

mbstring.encoding_translation = On
mbstring.detect_order = auto
 
;; 無効な文字を出力しない
mbstring.substitute_character = none;

mbstring.func_overload = 0
# make
# make install

httpd.confの書き換え

AddType application/x-httpd-php .php

を追加してApache再起動


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS