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

ソース取得 http://www.postgresql.jp/~
常に最新バージョンを確認しましょう~
通信を暗号化する場合は ./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の設定 [#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-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

**ログ管理の設定 [#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>'

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の設定 [#b59b7981]
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以降のハマりどころ [#u2f912f0]
以前のバージョンでは動作していたのに、9にバージョンアップしたら動かない・・・という場合は以下を変更して再起動すれば幸せになれるかも。

 standard_conforming_strings = off
 default_transaction_deferrable = on

**チューニング [#s385b4e6]

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

*Apacheのインストール [#ye12defa]
http://httpd.apache.org/

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]]

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/~
http://pcre.org/

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の設定 [#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
 
 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

**自動起動の設定 [#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

-[[SimpleXML関数:http://jp2.php.net/manual/ja/ref.simplexml.php]]

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

-[[cURL:http://curl.haxx.se/]]

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

-httpsのコンテンツ取得

fopen('https://example.com/index.html');~
などとしたい場合は指定が必要
 --with-openssl

-MySQL

MySQLは先にインストールしておく~
ソースからのインストールは[[MySQL5.0インストール]]を参照
 --with-mysql

-[[PHP Data Objects (PDO) :http://jp2.php.net/manual/ja/ref.pdo.php]]
共有モジュール(shared)でインストールするのが推奨されている~
(※[[zlib:http://jp2.php.net/manual/ja/ref.zlib.php]] 拡張モジュールが必要)
 --enable-pdo=shared
データベース固有のPDOドライバを使用する必要があので、RDBMSの種類に合わせて下記を追加~
php.ini ファイルの編集もする必要がある。
参照[[PHP Data Objects (PDO) :http://jp2.php.net/manual/ja/ref.pdo.php]]
--PostgreSQL
 --with-pdo-pgsql=shared
--SQLite
 --with-pdo-sqlite=shared
--MySQL
 --with-pdo-mysql=shared

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.3.6.tar.gz
 # cd php-5.3.6
 # ./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オプションについて [#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.iniの設定 [#b8122ccd]
 # cp /usr/local/src/php-5.1.4/php.ini-dist /usr/local/lib/php.ini
 # cp /usr/local/src/php-5.3.5/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の書き換え [#o315704c]

 AddType application/x-httpd-php .php
を追加してApache再起動
#exlink

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