当サイトをご利用頂き有難うございます。
当サイトは今月末日にモールタイプの総合検索サイトへリニューアルの予定です。
サイト名も変更となりますが、今後とも宜しくお願いいたします。

php+php_Smarty

アプリケーションの稼動に必要なソフトの中から、今回は php5 をインストールします。
現在の最新バージョンは5.2.8(2009/01/16リリース)ですが
CentOS5にはちょっと古いバージョン5.1.6 がインストール済みになっています。
ベースは古いですがセキュリティパッチは施されている様です。

また、テンプレートエンジンにはphp-Smartyを使用します。
遅いという評判もありますが、暫くはphp-Smartyで行きます。

①phpの確認

   # su + 管理者権限パスワード

   # yum list | grep php  を入力

     php.i386    5.1.6-20.el5_2.1    installed  →  と表示されればOKです。

      * 古いのですがこのままにしておきます。セキュリティと安定化が優先なので、
        必要性が生じない限り、CentOS5の自動バージョンアップに同期させます。
         最新のバージョンはFedoraサーバーで検証します。

②php-pear(PHPの拡張ライブラリ)のインストール

   # yum -y install php-pear  を入力

    Complete!

③php-mbstring(日本語の処理)のインストール

   # yum -y install php-mbstring  を入力

    Complete!

④php-mysql(PHPからはMySQLに接続するのに必要)のインストール

   # yum -y install php-mysql  を入力

    Complete!

⑤php-Smarty(テンプレートエンジン)のダウンロード

  Fedoraのリポジトリには在るのですが、
  CentOSのリポジトリには無いのでサイトSmarty : Downloadsから直接ダウンロードしてインストールします。

      * 最新版を確認してバージョンを置き換えます。

   # wget http://www.smarty.net/do_download.php?download_file=Smarty-2.6.22.tar.gz
       →  Smartyのダウンロード

   # tar xvzf Smarty-2.6.22.tar.gz

⑥Smarty 用のディレクトリを作成

   # mkdir -p /var/www/html/smarty   →  環境に合わせ変更(プログラム毎に配置します)
   # cd /var/www/html/smarty
   # mkdir templates templates_c configs cache   →  Smarty 用に4つのディレクトリを作成

⑦Smartyを配置

   # mv Smarty-2.6.22 Smarty   →  ディレクトリ名を変更
   # mkdir /usr/local/lib/php   →  smartyを配置するディレクトリを作成 
   # mv Smarty /usr/local/lib/php/   →  展開したsmartyを配置ディレクトリへ移動

⑧フォルダのパーミッションを変更(書き込みが出来るようにします)

   # chmod 777 /var/www/html/smarty/templates_c   →  パーミッションを変更

   # chmod 777 /var/www/html/smarty/cache   →  パーミッションを変更

⑨smarty ディレクトリの所有権の変更

   # chown -R apache:apache /var/www/html/smarty   →  所有権の変更

    * 既定(httpd.conf)で apache:apache が設定されています。

⑩php.iniの編集(日本語が使えるようにします)

   # vi /etc/php.ini  を入力

;;;;;;;;;;;;;;;;;;;;
; Language Options ;
;;;;;;;;;;;;;;;;;;;;

;allow_call_time_pass_reference = Off
allow_call_time_pass_reference = On   →  関数の引数としてリファレンスを使用

( 省略 )

output_buffering = On   →  HTTP BODYを出力した後でもHTTPヘッダが出力できます。
output_handler = mb_output_handler   →  PHPからの標準出力を指定の関数にリダイレクト
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Log errors to specified file.
;error_log = filename
error_log = "/var/log/php_error"   →  追加

;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;

;register_argc_argv = Off
register_argc_argv = On   →  修正

( 省略 )

;default_charset = Shift_JIS
default_charset = EUC-JP

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
include_path = ".:/usr/share/pear:/usr/local/lib/php/Smarty/libs"   →  追加

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Maximum allowed size for uploaded files.
; upload_max_filesize = 2M         →  コメントアウト
multibyte/upload_max_filesize = 100M   →  リストアで利用できるバックアップデータの最大サイズ

;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;

( 省略 )

[mbstring]------- コメント(;)の削除 & 設定変更(環境に合わせ変更します) -------
                  
 mbstring.language = Japanese
 mbstring.internal_encoding = UTF-8
 mbstring.http_input = auto
 mbstring.http_output = UTF-8
 mbstring.encoding_translation = On
 mbstring.detect_order = auto

     * この設定は上手く環境に合わせ構築しておかないと、後々に使用する、
       phpMyAdmin等での文字化けに悩む事になります。
        php関連の文字化けが発生したら、真っ先にココの設定を調整してみましょう。

⑪Smarty_App クラスファイルを作成

    /var/www/html/smarty/Smarty_App.php として保存

    ----------- Smarty_App.php -----------
    <?php
    require_once 'Smarty.class.php';

    class Smarty_App extends Smarty {
     private $smartyAppBase = '/var/www/html/smarty';
     public function __construct() {
     parent :: __construct();

     $this->template_dir = $this->smartyAppBase.'/templates/';
     $this->compile_dir = $this->smartyAppBase.'/templates_c/';
     $this->config_dir = $this->smartyAppBase.'/configs/';
     $this->cache_dir = $this->smartyAppBase.'/cache/';

     $this->caching = true;
     }
    }
    ?>
    --------------------------------- 以上 ----

⑫Smarty テスト用の php ファイルを作成

    /var/www/html/test_smarty.php として保存

    ----------- test_smarty.php -----------
    <?php
    require_once '/var/www/html/smarty/Smarty_App.php';

    $smarty = new Smarty_App;
    $smarty->assign('var1', 'test_smarty.php');
    $inc_path = ini_get('include_path');
    $smarty->assign('var2', $inc_path);
    $smarty->display('test_smarty.tpl')
    ?>
    --------------------------------- 以上 ----

⑬Smarty テンプレートファイルを作成

    /var/www/html/smarty/templates/test_smarty.tpl として保存

    ----------- test_smarty.tpl -----------
    <HTML>
     <HEAD>
     <META http-equiv=Content-Type content="text/html; charset=utf-8">
     <TITLE>test_smarty.tpl</TITLE>
     </HEAD>
     <BODY>
     file name = {$var1}<BR/>
     php include_path = {$var2}<BR/>
     SMARTY_DIR = {$smarty.const.SMARTY_DIR}<BR/>
     </BODY>
    </HTML>
    --------------------------------- 以上 ----


⑭httpd(Apache)を再起動

   # service httpd restart を入力

⑮確認

  ブラウザで Smarty テスト用の php ファイル(test_smarty.php)にアクセス


インストール終了です。