一、安裝fastcgi子產品
1、yum install php-fpm,php-mysql,php-mbstring,php-gd,php-xml
php-fpm:PHP FastCGI Process Manager,基于fastcgi協定的php程序管理器。nginx可通過該程式與php完成動态資料互動.

php-fpm的工作方式類似于httpd的prefork模型
/etc/php-fpm.d/www.conf檔案為php-fpm的主配置檔案,主要配置項有:
listen = 127.0.0.1:9000 ##監聽位址
listen.allowed_clients = 127.0.0.1 ###允許互動的用戶端ip
user = apache
group = apache
pm = dynamic ###使用動态方法
pm.max_children = 50 ###一次請求允許的最多資源數
pm.start_servers = 5 ###剛開始時啟動的程序
pm.min_spare_servers = 5 ###最少空閑程序數
pm.max_spare_servers = 35 ###最大空閑程序數
2、nginx的fastcgi子產品指令:
fastcgi_pass 127.0.0.1:9000; ###是fpm服務監聽的位址和端口
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; ####指明目前fpm服務的root路徑.如,請求
http://localhost/index.php就會對應這個選項中定義的/usr/local/nginx/html/index.php檔案路徑
include fastcgi_params;####表示還包括fastcgi_params檔案中定義的參數
3.啟動php-fpm.service進行測試:
systemctl start php-fpm.service
在fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;定義的路徑下建立一個php測試頁info.php檔案,内容如下,表示輸出php的相關資訊:
證明測試生效.
二、fastcgi緩存相關配置:
1、fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fcgicache:10m;####指明fastcgi的緩存檔案路徑,levels表示nginx的緩存機制建立一級目錄與二級目錄,keys_zone指明緩存鍵的區域名稱及有效性時間為10分鐘;
2、fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;#####定義各http響應狀态碼及其緩存有效時間
3、 fastcgi_cache_key $request_uri;###表示使用http請求的uri緩存後作為緩存鍵
4、fastcgi_cache fcgicache;####指明使用的fastcgi緩存的空間名
要使用緩存時,至少要配置好三個參數:
fastcgi_cache
fastcgi_cache_key
fastcgi_cache_valid
配置好fastcgi緩存後,可使用壓力測試指令對緩存進行測試:
ab -c 10 -n 100
http:/ip/info.php:
測試可得出緩存能加速一定的響應時間。
nginx通過fastcgi子產品與後端的php伺服器(php-fpm.service)進行互動,完成動态資料處理後将動态資料與靜态資料一起響應給用戶端.