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
- wget http://apache.ziply.com//httpd/httpd-2.2.21.tar.gz
- tar xzf httpd-2.2.21.tar.gz
- cd httpd-2.2.21
- ./configure --prefix=/usr/local/apache
- make && make install
安裝fastcgi
- wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz
- tar xzf mod_fastcgi-2.4.6.tar.gz
- cd mod_fastcgi-2.4.6
- cp Makefile.AP2 Makefile
- make top_dir=/usr/local/apache
- make top_dir=/usr/local/apache install
完成之後編輯httpd.conf配置檔案,加入fastcgi模組裝載程式碼:
- LoadModule fastcgi_module modules/mod_fastcgi.so
安裝php5.2
- wget http://us2.php.net/get/php-5.2.17.tar.gz/from/am.php.net/mirror
- tar xzf php-5.2.17.tar.gz
- cd php-5.2.17
- ./configure --prefix=/usr/local/php --enable-fastcgi --disable-cli
- make && make install
配置apache支援php
編輯httpd.conf檔案,加入如下程式碼:
- ### fastcgi ###
- ScriptAlias /fcgi-bin/ "/usr/local/php/bin/"
- AddHandler php-fastcgi .php
- Action php-fastcgi /fcgi-bin/php-cgi
- AddType application/x-httpd-php .php
- <IfModule mod_fcgid.c>
- AddHandler fcgid-script. .php .fcgi ### 暫時只配置支援.php
- IdleTimeout 300
- ProcessLifeTime 1800
- MaxProcessCount 100
- DefaultMinClassProcessCount 3
- DefaultMaxClassProcessCount 8
- IPCConnectTimeout 15
- IPCCommTimeout 300
- MaxRequestsPerProcess 100
- </IfModule>
- ### fastcgi ###
建立虛擬主機可以這樣配置:
- <VirtualHost *:80>
- DocumentRoot /usr/local/apache/htdocs
- ServerName localhost
- Options +ExecCGI
- AddHandler fastcgi-script .fcgi
- AddType application/x-httpd-php .php
- Action application/x-httpd-php /fcgi-bin/php-cgi
- <Directory /usr/local/apache/htdocs>
- Options Indexes ExecCGI
- Order allow,deny
- allow from all
- </Directory>
- </VirtualHost>
詳細的fastcgi指令配置請看:http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html