apache預設是用自帶的mod_php模組執行php,現在我們介紹使用fastcgi來執行php指令碼。先說下fastcgi的優點:
Fastcgi的優點:

  • 從穩定性上看, fastcgi是以獨立的程序池執行來cgi,單獨一個程序死掉,系統可以很輕易的丟棄,然後重新分 配新的程序來執行邏輯.
  • · 從安全性上看,Fastcgi支援分散式運算. fastcgi和宿主的server完全獨立, fastcgi怎麼down也不會把server搞垮.
  • · 從效能上看, fastcgi把動態邏輯的處理從server中分離出來, 大負荷的IO處理還是留給宿主server, 這樣宿主server可以一心一意作IO,對於一個普通的動態網頁來說, 邏輯處理可能只有一小部分, 大量的圖片等靜態
  • IO處理完全不需要邏輯程式的參與.
  • · 從擴充套件性上講, fastcgi是一箇中立的技術標準, 完全可以支援任何語言寫的處理程式 (php,java,perl,ruby,c++,python...)
  • · 適用作業系統,可在任何平臺上http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz使用

安裝apache

  1. wget http://apache.ziply.com//httpd/httpd-2.2.21.tar.gz
  2. tar xzf httpd-2.2.21.tar.gz
  3. cd httpd-2.2.21
  4. ./configure --prefix=/usr/local/apache
  5. make && make install

安裝fastcgi

  1. wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
  2. tar xzf mod_fastcgi-2.4.6.tar.gz
  3. cd mod_fastcgi-2.4.6
  4. cp Makefile.AP2 Makefile
  5. make top_dir=/usr/local/apache
  6. make top_dir=/usr/local/apache install

完成之後編輯httpd.conf配置檔案,加入fastcgi模組裝載程式碼:

  1. LoadModule fastcgi_module modules/mod_fastcgi.so

安裝php5.2

  1. wget http://us2.php.net/get/php-5.2.17.tar.gz/from/am.php.net/mirror
  2. tar xzf php-5.2.17.tar.gz
  3. cd php-5.2.17
  4. ./configure --prefix=/usr/local/php --enable-fastcgi --disable-cli
  5. make && make install

配置apache支援php

編輯httpd.conf檔案,加入如下程式碼:

  1. ### fastcgi ###
  2. ScriptAlias /fcgi-bin/ "/usr/local/php/bin/"
  3. AddHandler php-fastcgi .php
  4. Action php-fastcgi /fcgi-bin/php-cgi
  5. AddType application/x-httpd-php .php
  6. <IfModule mod_fcgid.c>
  7. AddHandler fcgid-script. .php .fcgi   ### 暫時只配置支援.php
  8. IdleTimeout 300
  9. ProcessLifeTime 1800
  10. MaxProcessCount 100
  11. DefaultMinClassProcessCount 3
  12. DefaultMaxClassProcessCount 8
  13. IPCConnectTimeout 15
  14. IPCCommTimeout 300
  15. MaxRequestsPerProcess 100
  16. </IfModule>
  17. ### fastcgi ###

建立虛擬主機可以這樣配置:

  1. <VirtualHost *:80>
  2. DocumentRoot /usr/local/apache/htdocs
  3. ServerName localhost
  4. Options +ExecCGI
  5. AddHandler fastcgi-script .fcgi
  6. AddType application/x-httpd-php .php
  7. Action application/x-httpd-php /fcgi-bin/php-cgi
  8. <Directory /usr/local/apache/htdocs>
  9. Options Indexes ExecCGI
  10. Order allow,deny
  11. allow from all
  12. </Directory>
  13. </VirtualHost>

詳細的fastcgi指令配置請看:http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html