<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/install.unix.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'install.unix.apache2.php',
    1 => 'Apache 2.x (Unixシステム用)',
    2 => 'Apache 2.x (Unixシステム用)',
  ),
  'up' => 
  array (
    0 => 'install.unix.php',
    1 => 'Unix システムへのインストール',
  ),
  'prev' => 
  array (
    0 => 'install.unix.commandline.php',
    1 => 'CGI およびコマンドライン',
  ),
  'next' => 
  array (
    0 => 'install.unix.nginx.php',
    1 => 'Nginx 1.4.x (Unix システム用)',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'install/unix/apache2.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="install.unix.apache2" class="sect1">
 <h2 class="title">Apache 2.x (Unixシステム用)</h2>

 <p class="para">
  このセクションでは、PHPを Unix システム上の Apache 2.x にインストールする際の
  手引きと注意事項について説明します。
 </p>
   
 <div class="warning"><strong class="warning">警告</strong><p class="para">
Apache2 の MPM マルチスレッドモードを実運用環境で使用することは推奨されません。
代わりに prefork MPM を使ってください。これは Apache 2.0 および 2.2
におけるデフォルトの MPM です。prefork MPM を使う理由については、
<a href="faq.installation.php#faq.installation.apache2" class="link">マルチスレッド版 MPM の Apache2</a>の
FAQ エントリを参照してください。</p></div>
    
 <p class="para">
  <a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache ドキュメンテーション</a>
  を参照し、Apache 2.x の基本的な事項について理解しておくことを強く推奨します。
  Apache のインストールオプションについてのより詳しい情報が得られます。
 </p>

 <p class="para">
  最新バージョンの Apache HTTP Server を
  <a href="http://httpd.apache.org/" class="link external">&raquo;&nbsp;Apache ダウンロードサイト</a>
  からダウンロードし、上述のいずれかのバージョンの PHP を用意してください。
  この手引きでは Apache 2.x で PHP を動作させるための
  基本的な部分しかカバーしていません。さらに詳しい情報については、<a href="http://httpd.apache.org/docs/current/" class="link external">&raquo;&nbsp;Apache ドキュメンテーション</a> を参照ください。
  情報が古く不正確になってしまうため、以下では詳細なバージョン番号は
  記述されていません。&#039;NN&#039; という文字列をご使用のバージョンに適宜置き換えて
  ください。
 </p>

 <p class="para">
  現在、Apache 2.x には 2.4 と 2.2 の二種類があります。
  どちらを選ぶにしてもそれなりの理由があるでしょうが、
  2.4 が現在の最新版です。もし選択の余地があるのなら
  2.4 を使うことを推奨します。しかし、この例では 2.4 と 2.2
  のどちらでも使えるようにしています。
  Apache httpd 2.2 は正式には廃止予定で、新しい開発やパッチが
  発行されないことに注意してください。
 </p>

 <ol type="1">
  <li class="listitem">
   <p class="para">
    Apache HTTP server を上のサイトから取得して展開します。
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
tar -xzf httpd-2.x.NN.tar.gz
</pre></div>
    </div>
   </div>
  </li>

  <li class="listitem">
   <p class="para">
    同じく、PHP のソースを取得して展開します。
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
tar -xzf php-NN.tar.gz
</pre></div>
    </div>
   </div>
  </li>
   
  <li class="listitem">
   <p class="para">
    Apache をビルドしてインストールします。Apache のビルドに関する詳細は
    Apache のドキュメントを参照ください。
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cd httpd-2_x_NN
./configure --enable-so
make
make install
</pre></div>
    </div>
   </div>
  </li>

  <li class="listitem">
   <p class="para">
    以上で Apache 2.x.NN が、モジュールの動的ロードとデフォルトの
    MPM（マルチプロセッシングモジュール）である prefork が有効になった
    状態で、/usr/local/apache2 にインストールされます。
    インストールが正常か調べるには、以下のようにして Apache サーバーを立ち上げます。

    <div class="informalexample">
     <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl start
</pre></div>
     </div>
    </div>

    サーバーを止めて、PHP 用の設定を進めましょう。

    <div class="informalexample">
     <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl stop
</pre></div>
     </div>
    </div>
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    PHP の configure を行います。ここでは、様々なオプションを指定し、
    特定の拡張モジュールを有効にするといったカスタマイズを行います。
    指定可能なオプションの一覧は、<strong class="command">./configure --help</strong> を実行すると得られます。
    以下に、Apache 2 と MySQL のサポートを有効にする、簡単な設定例を示します。
   </p>

   <p class="para">
    上で説明したように Apache をソースからビルドした場合は、
    <strong class="command">apxs</strong> のパスも下の例のとおりになっているでしょう。しかし、
    もし別の方法で Apache をインストールした場合は <strong class="command">apxs</strong>
    のパスを適切に変更しなければなりません。
    ディストリビューションによっては、<strong class="command">apxs</strong> の名前が
    <strong class="command">apxs2</strong> と変更されていることもあるので注意しましょう。
   </p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-pdo-mysql
make
make install
</pre></div>
    </div>
   </div>

   <p class="para">
    configure オプションを変更して再インストールする場合は、
    <strong class="command">configure</strong>, <strong class="command">make</strong>,
    <strong class="command">make install</strong> の手順を繰り返さなければなりません。
    共有モジュールとしてコンパイルされた PHP を有効にするには
    Apache を再起動するだけです。Apache の再コンパイルは不要です。
   </p>
         
   <p class="para">
    特に断りがない限り、<strong class="command">make install</strong> は、<a href="https://pear.php.net/" class="link external">&raquo;&nbsp;PEAR</a>, <a href="install.pecl.phpize.php" class="link">phpize</a> のような様々な 
    関連ツール、CLI 版 PHP などもインストールすることに注意してください。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    <var class="filename">php.ini</var> ファイルを設定する
   </p>
    
   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
cp php.ini-development /usr/local/lib/php.ini
</pre></div>
    </div>
   </div>

   <p class="para">
    PHP の実行時設定を変更するには、<code class="literal">.ini</code> ファイルを編集します。
    <var class="filename">php.ini</var> ファイルを他の場所に置きたい場合は、手順 5 で、
    オプション <code class="literal">--with-config-file-path=/some/path</code> を使用します。
   </p>
   
   <p class="para">
    <var class="filename">php.ini-development</var>ent ではなく、<var class="filename">php.ini-production</var> を使用する場合は、PHP の
    動作が変化しますので、ファイル中に記載されている変更点の一覧を確認する
    ようにしてください。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    <var class="filename">httpd.conf</var> を編集し、PHP の共有モジュールをロードするよう設定します。
    <code class="literal">LoadModule</code> 命令の右側に記述するパスは、システムの PHP 共有モジュール
    を指している必要があります。上記の <strong class="command">make install</strong> により既にこの設定は
    追加されている場合もありますが、確認が必要です。
   </p>

   <div class="informalexample">
    <p class="para">
     PHP 8 向けの設定は以下のようになります:
    </p>

    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">LoadModule php_module modules/libphp.so</pre>
</div>
    </div>

   </div>

   <div class="informalexample">
    <p class="para">
     PHP 7 向けの設定は以下のようになります:
    </p>

    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">LoadModule php7_module modules/libphp7.so</pre>
</div>
    </div>

   </div>
  </li>

  <li class="listitem">
   <p class="para">
    Apache が特定の拡張子のファイルを PHP としてパースするよう設定します。
    たとえば、Apache が拡張子 <code class="literal">.php</code> のファイルを PHP としてパースするようにします。
    単に Apache の <code class="literal">AddType</code> ディレクティブを使うだけではなく、
    悪意を持ってアップロード (あるいは作成) された <var class="filename">exploit.php.jpg</var>
    のようなファイルが PHP として実行されてしまわないようにしたいものです。
    この例では、PHP としてパースさせたい任意の拡張子を追加していくだけです。
    ためしに <code class="literal">.php</code> を追加してみましょう。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch \.php$&gt;
    SetHandler application/x-httpd-php
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="para">
    あるいは、拡張子 <code class="literal">.php</code>, <code class="literal">.php2</code>,
    <code class="literal">.php3</code>, <code class="literal">.php4</code>, <code class="literal">.php5</code>,
    <code class="literal">.php6</code>, <code class="literal">.phtml</code>
    のファイルだけを PHP として実行したい場合は、以下のようにします。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch &quot;\.ph(p[2-6]?|tml)$&quot;&gt;
    SetHandler application/x-httpd-php
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="para">
    拡張子 <code class="literal">.phps</code> のファイルを php ソースフィルタに処理させて
    構文ハイライトつきのソースコードとして表示させるには、このようにします。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">&lt;FilesMatch &quot;\.phps$&quot;&gt;
    SetHandler application/x-httpd-php-source
&lt;/FilesMatch&gt;</pre>
</div>
    </div>

   </div>

   <p class="para">
    <code class="literal">mod_rewrite</code> を使うと、<code class="literal">.php</code> ファイルに名前を変えたりコピーしたりしなくても
    任意の <code class="literal">.phps</code> ファイルを構文ハイライトつきのソースコードとして表示させることができます。
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="apache-confcode"><pre class="apache-confcode">RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]</pre>
</div>
    </div>

   </div>

   <p class="para">
    php ソースフィルタは、実運用環境では無効にしておかなければなりません。
    有効にしてしまうと、ソースコードに埋め込まれた機密情報や重要情報が漏れてしまう危険があります。
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Apache サーバーを、通常の手順通り、起動させます。
   </p>
  
   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
/usr/local/apache2/bin/apachectl start
</pre></div>
    </div>
   </div>

   <p class="para">または、以下の手順でも起動できます。</p>

   <div class="informalexample">
    <div class="example-contents screen">
<div class="cdata"><pre>
service httpd restart
</pre></div>
    </div>
   </div>
  </li>
 </ol>

 <p class="para">
  上記の手順で、Apache2 ウェブサーバー上で
  <code class="literal">SAPI</code> モジュールとして PHP を動作させることができます。もちろん、Apache と PHP の双方とも、もっと多くの configure オプションを指定することが出来ます。
  詳しい情報を得るには、ソースツリーディレクトリで <strong class="command">./configure --help</strong> を実行してください。
 </p>

 <p class="para">
  マルチスレッド版の Apache をビルドするには、Apache のビルド時に標準の
  <var class="filename">prefork</var> MPM ではなく <var class="filename">worker</var> MPM を選択します。
  そのためには、先ほどの手順 3 のところで <strong class="command">./configure</strong> の引数に次のオプションを追加します。
 </p>

 <div class="informalexample">
  <div class="example-contents screen">
<div class="cdata"><pre>
--with-mpm=worker
</pre></div>
  </div>
 </div>

 <p class="para">
  そうすることで何がどのようになるのかをきちんと認識したうえで、これを行わなければなりません。
  詳細については Apacheドキュメントの
  <a href="http://httpd.apache.org/docs/current/mpm.html" class="link external">&raquo;&nbsp;マルチプロセッシングモジュール (MPM)</a>
  を参照ください。
 </p>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
  <a href="faq.installation.php#faq.installation.apache.multiviews" class="link">Apache MultiViews FAQ</a> では、PHP でマルチビューを使う方法について解説しています。
  </p>
 </p></blockquote>

 <blockquote class="note"><p><strong class="note">注意</strong>: 
  <p class="para">
   マルチスレッド版の Apache をビルドするには、ターゲットシステムがスレッドに対応していなければなりません。
   その場合は、PHP についても Zend Thread Safety (ZTS) でビルドしなければなりません。
   この構成では使用できない拡張モジュールもあります。推奨される方法は、Apache をデフォルトの
   <var class="filename">prefork</var> MPM モジュールでビルドすることです。
  </p>
 </p></blockquote>
</div><?php manual_footer($setup); ?>