PostgreSQL9+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-9.4.4
# chown postgres /usr/local/src/postgresql-9.4.4

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

# yum install readline-devel zlib-devel
# su postgres
$ tar xvzf postgresql-9.4.4.tar.gz
$ cd postgresql-9.4.4
$ ./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-9.4.4/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>'

postgresql-8.3.0以降

logging_collector = on
log_filename = '%m-%d.log'
log_truncate_on_rotation = on
log_min_error_statement = error
log_line_prefix = '<%t %u %d %p>'

自動VACUUMの設定

postgresql-8.1からは自動でVACUUMが実行されるように設定できるようになった
postgresql-8.3.0以降はデフォルトで有効になった

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

postgresql-8.3.0以降

track_counts = on
autovacuum = on

バージョン9以降のハマりどころ

以前のバージョンでは動作していたのに、9にバージョンアップしたら動かない・・・という場合は以下を変更して再起動すれば幸せになれるかも。

standard_conforming_strings = off
default_transaction_deferrable = on

チューニング

デフォルトでは不十分なのでこちらを参考に合わせてやっておこう
PostgreSQLのチューニング

DBユーザー作成

いつの間にか対話モードではなくなっているので、以下のオプションが必要

$ createuser --interactive user1
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

Apacheのインストール

http://httpd.apache.org/[�O�������N]

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

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

DAVを使用する場合は --enable-dav

# tar xvzf httpd-2.2.19.tar.gz
# cd httpd-2.2.19
# ./configure \
  --with-mpm=prefork \
  --enable-rewrite \
  --enable-so \
  --with-included-apr
# make
# make install

apache2.2.4あたりからは
「configure: error: Cannot use an external APR-util with the bundled APR」
とエラーが出るようなので --with-included-apr を追加する

apache2.4あたりからはaprが別になったみたいなので、別途入れる
PCREとやらも要る模様
http://apr.apache.org/[�O�������N]
http://pcre.org/[�O�������N]

pcre2-10.10.tar.gz だとうまくできませんでした

# tar xvzf apr-1.5.2.tar.gz
# cd apr-1.5.2
# ./configure --prefix=/usr/local/apr
# make
# make install
# cd ..
# tar xvzf apr-util-1.5.4.tar.gz
# cd apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make
# make install
# cd ..
# tar xvzf pcre-8.37.tar.gz
# cd pcre-8.37
# ./configure --prefix=/usr/local/pcre
# make
# make install
# cd ..
# tar xvzf httpd-2.4.12.tar.gz
# cd httpd-2.4.12
# ./configure \
  --with-mpm=prefork \
  --enable-rewrite \
  --enable-so \
  --with-apr=/usr/local/apr \
  --with-apr-util=/usr/local/apr-util \
  --with-pcre=/usr/local/pcre
# make
# make install

Apacheの設定

nobody権限になるように変更
特に理由は無し。昔からの個人的な名残

<IfModule !mpm_netware_module>
User nobody
Group nobody
</IfModule>

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

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」の下にでも追加しとけばいいだろう

Apache2.4系の場合

vi /usr/local/apache2/conf/httpd.conf
LoadModule cgi_module modules/mod_cgi.so
LoadModule userdir_module modules/mod_userdir.so
LoadModule rewrite_module modules/mod_rewrite.so

自動起動の設定

# 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のインストール

http://jp.php.net/downloads.php[�O�������N]

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

Amazon ECSなどを利用する場合は有効にしておく

--enable-simplexml

cURLは先にインストールしておく
RPMで入っていればOK

--with-curl

fopen('https://example.com/index.html');[�O�������N]
などとしたい場合は指定が必要

--with-openssl

EC-CUBE2.13系を使用する場合は必要
MySQLは先にインストールしておく
ソースからのインストールはMySQL5.0インストールを参照

--with-mysql

5.3.5くらいから --with-ttf オプションは無効
5.4.0くらいから --enable-zend-multibyte オプションは無効

# yum install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl curl-devel openssl-devel
# tar xvzf php-5.6.10.tar.gz
# cd php-5.6.10
# ./configure \
    --with-apxs2=/usr/local/apache2/bin/apxs \
    --enable-zend-multibyte \
    --enable-mbstring \
    --enable-mbregex \
    --with-zlib \
    --with-gd \
    --with-zlib-dir=/usr \
    --with-jpeg-dir=/usr \
    --with-png-dir=/usr \
    --with-freetype-dir=/usr \
    --enable-gd-native-ttf \
    --enable-gd-jis-conv \
    --enable-sockets \
    --enable-simplexml \
    --with-curl \
    --with-pgsql \
    --with-mysql=/usr/local/mysql \
    --with-sqlite=shared \
    --enable-pdo=shared \
    --with-pdo-pgsql=shared \
    --with-pdo-mysql=shared \
    --with-pdo-sqlite=shared \
    --with-ttf
# make
# make install

その他configureオプションについて

php.iniの設定

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

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

;; ヘッダに表示される X-Powered-By: PHP/x.x.x を非表示にする
expose_php = Off

magic_quotes_gpc = Off ; 5.4.0で削除

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

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

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

;; エンコーディングしない
mbstring.http_input = pass 

;; エンコーディングしない
mbstring.http_output = pass

;; 自動エンコード
;; 文字化けの原因になるので、自力でエンコード
mbstring.encoding_translation = Off

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

mbstring.func_overload = 0

;; 変数のパースの順番を設定
variables_order = "EGPCS"

;; $_REQUEST 配列に登録する順序(PHP5.3.0から導入)
;; 指定しないと$_REQUESTでcookieが取得できないので注意
request_order = "GPC"

;; 5.5.1あたりからは error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
error_reporting  =  E_ALL & ~E_NOTICE

log_errors = Off

;; 5.3.0以降 メールのヘッダに X-PHP-Originating-Script が追加される
;; ヘッダがくずれるのでOff
mail.add_x_header = Off

;; 5.1.0以降date, strtotimeなどを使うとE_NOTICEやE_WARNINGが発生するので設定しておく
date.timezone = Asia/Tokyo

; 4.3.0から追加5.3.0以降でdefault1になったので、バージョンアップ時携帯電話のセッション管理でハマる可能性あり
session.use_only_cookies = 1 or 0

; Zend_Sessionを使用するならばコメントをはずしておくと吉
session.save_path = "/tmp"

httpd.confの書き換え

AddType application/x-httpd-php .php

を追加してApache再起動


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