*PostgreSQL8+PHP5+Apache2インストール [#k562d2b8] #contents [[PostgreSQL8.0の解説:http://itpro.nikkeibp.co.jp/members/ITPro/oss/20050124/155152/]]~ [[Apache2.2の解説:http://www.atmarkit.co.jp/flinux/special/apache22/apache01.html]] [[PostgreSQLとMySQLの比較:http://www.thinkit.co.jp/free/article/0603/10/1/]] *注意事項 [#ac168e03] バージョン8.1.4, 8.0.8, 7.4.13, 7.3.15以前のバージョンにはマルチバイト処理にSQLインジェクションの脆弱性があります。~ [[詳細(SRA OSS):http://www.sraoss.co.jp/PostgreSQL/8.1.4/changes.html]]~ [[詳細(ITpro):http://itpro.nikkeibp.co.jp/article/COLUMN/20060530/239359/]] *PostgreSQLのインストール [#rb7a0383] 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/~ 常に最新バージョンを確認しましょう~ 通信を暗号化する場合は ./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の設定 [#a7c33acc] $ 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 付きでインストールしておく~ 起動する必要は無し **パスの設定 [#p07099bd] 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 **自動起動の設定 [#bbdee3b2] # 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 **ログ管理の設定 [#e788b083] -エラーが発生した場合のSQL文は記録する -ログファイルは1日単位で分割し、過去1ヶ月保存する $ 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の設定 [#b59b7981] PostgreSQL8.1からは自動でVACUUMが実行されるように設定できるようになった $ vi /usr/local/pgsql/data/postgresql.conf stats_row_level = on autovacuum = on **チューニング [#s385b4e6] デフォルトでは不十分なのでこちらを参考に合わせてやっておこう~ [[PostgreSQLのチューニング]] *Apacheのインストール [#ye12defa] http://www.apache.jp/ MPMはworkerもしくはperchildだとPHPが安定動作しないらしい~ [[参考:http://jp2.php.net/manual/ja/install.unix.apache2.php]] SSLを有効にする場合は --enable-ssl~ [[参考:http://www.stackasterisk.jp/tech/systemConstruction/apache09_03.jsp]] # tar xvzf httpd-2.2.2.tar.gz # cd httpd-2.2.2 # ./configure --with-mpm=prefork --enable-rewrite --enable-so # make # make install **Apacheの設定 [#p77fe41d] nobody権限になるように変更~ 特に理由は無し。昔からの個人的な名残 <IfModule !mpm_netware_module> User nobody Group nobody </IfModule> -Apacheのバージョンなどを表示させないようにする (ServerTokens, ServerSignature) -TRACEメソッドに応答しないようにする (TraceEnable) ''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」の下にでも追加しとけばいいだろう **自動起動の設定 [#b486844c] # 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 **ログのローテーション設定 [#u509434c] ログを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のインストール [#ffe23a4b] http://jp.php.net/downloads.php [[PHPめも]]も参考にして下さい :D Amazon ECSなどを利用する場合は[[SimpleXML関数:http://jp2.php.net/manual/ja/ref.simplexml.php]]を有効にしておく~ &color(black){--enable-simplexml}; [[cURL:http://curl.haxx.se/]]を利用する場合は先にインストールしておく~ RPMで入っていればOK~ &color(black){--with-curl}; MySQLを使用する場合は~ &color(black){--with-mysql}; # tar xvzf php-5.1.4.tar.gz # cd php-5.1.4 # ./configure \ --with-apxs2=/usr/local/apache2/bin/apxs \ --enable-zend-multibyte \ --enable-mbstring \ --enable-mbregex \ --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-pgsql \ --with-mysql # make # make install **その他configureオプションについて [#pcce1c29] -[[SOAP関数:http://jp2.php.net/manual/ja/ref.soap.php]]を利用する場合下記を追加(ibxml-2.5.4 以上が必要)~ --enable-soap -[[FTP関数:http://jp2.php.net/manual/ja/ref.ftp.php]]を利用する場合下記を追加~ --enable-ftp -[[PHP Data Objects (PDO) :http://jp2.php.net/manual/ja/ref.pdo.php]]を利用する場合下記を追加~ 共有モジュール(shared)でインストールするのが推奨されている~ --with-zlib --enable-pdo=shared また、データベース固有の PDO ドライバ を使用する必要があので、~ データベースの種類に合わせて下記も追加~ php.ini ファイルの編集もする必要がある。 参照[[PHP Data Objects (PDO) :http://jp2.php.net/manual/ja/ref.pdo.php]] --PostgreSQL --with-pgsql=shared --with-pdo-pgsql=shared --SQLite --with-sqlite=shared --with-pdo-sqlite=shared --MySQL --with-mysql=shared --with-pdo-mysql=shared **php.iniの設定 [#b8122ccd] # 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 ;; 自動エンコード ;; ホントはOffにして自力でエンコードする方が文字化けの心配もいらない mbstring.encoding_translation = On mbstring.detect_order = auto ;; 無効な文字を出力しない mbstring.substitute_character = none; mbstring.func_overload = 0 ; Zend_Sessionを使用するならばコメントをはずしておくと吉 session.save_path = "/tmp" **httpd.confの書き換え [#o315704c] AddType application/x-httpd-php .php を追加してApache再起動 #exlink