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