第2章 PHP(FastCGI)
一、什麼是FastCGI
FastCGI是一個可伸縮、高速在HTTP server和動態腳本語言之間的一個通信接口.大多數的HTTP server都支持FastCGI,比如:Nginx Aapache lighttpd等..FastCGI被很多語言所支持。其中就有PHP.
二、Nginx+FastCGI運行原理
(1)Nginx不支持對外程序的直接調用或者解析,所有的外包程序(包括PHP)都必須通過FastCGI來調用
(2)FastCGI接口在Linux下面可以(socket文件的方式存在,那麼可以IP),
(3)爲了調用CGI程序,還需要一個FastCGI的wrapper,當Nginx發送CGI的請求給這個socket的時候。通過FastCGI接口wrapper接受到的請求,然後派生出一個新的線程,這個線程調用外部程序或者腳本讀卻返回數據.
最後wrapper在將返回的數據通過FastCGI接口,沿着固定的socket傳遞給Nginx,最後返回給數據發送的客戶端.
Nginx-->發送一個CGI請求-->FastCGI(warpper)接收到這個請求--->派生出一個新的線程--->調用外部程序或者腳本返回數據--->wrapper將數據沿着固定的socket傳遞給Nginx-->由Nginx在把這個數據返回給用戶.
三、PHP與PHP-FPM的安裝與優化
PHP-FPM也是第三方的FastCGI的進程管理,它作爲PHP補丁一起開發,編寫的時候跟着一起編譯到PHP內核當中,PHP-FPM在處理高併發的方面非常的優秀.它的一個有點呢:就是把動態語言和HTTP server分離開來(動靜分離),Http server主要處理靜態請求,PHP-FPM處理動態請求。
所有呢 PHP/PHP-FPM 和Nginx經常組合到一塊安裝到一臺機器上,以滿足業務需求.
四、首先安裝Mysql
(1)首先安裝Mysql數據庫,PHP在編譯的時候需要mysql的一個配置
這樣PHP遠程鏈接Mysql纔有用
# cd /data/soft/
# tar xf mysql-5.1.49.tar.gz -C tmp/
# cd tmp/mysql-5.1.49/
#CONFOPTS=" \
--with-charset=utf8 \
--with-plugins=partition,federated,innobase,myisam \
--enable-static \
--enable-assembler \
--enable-thread-safe-client \
--with-client-ldflags=-all-static-ltinfo \
--with-mysqld-ldflags=-all-static-ltinfo \
--with-big-tables \
--with-mysqld-user=mysql \
--without-debug \
--without-ndb-debug \
--localstatedir=/usr/local/services/mysql-5.1.49/var \
--prefix=/usr/local/services/mysql-5.1.49 \
"
#./configure $CONFOPTS >/dev/null
# make >/dev/null && make install >/dev/null
五、安裝PHP的依賴庫
①libxml2-2.7.7.tar.gz
# cd /data/soft/
#tar xf libxml2-2.7.7.tar.gz –C tmp/
# cd tmp/libxml2-2.7.7/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
②curl-7.21.4.tar.gz
# cd /data/soft/
# tar xf curl-7.21.4.tar.gz -C tmp/
# cd tmp/curl-7.21.4/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
③jpegsrc.v8b.tar.gz
# cd /data/soft/
#tar xf jpegsrc.v8b.tar.gz –C tmp/
#cd tmp/jpeg-8b/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
④libpng-1.4.3.tar.gz
# cd /data/soft/
# tar xf libpng-1.4.3.tar.gz -C tmp/
# cd tmp/libpng-1.4.3/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
⑤freetype-2.4.1.tar.gz
# cd /data/soft/
# tar xf freetype-2.4.1.tar.gz -C tmp/
# cd tmp/freetype-2.4.1/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
⑥libevent-2.0.10-stable.tar.gz
# cd /data/soft/
# tar xf libevent-2.0.10-stable.tar.gz –C tmp/
# cd tmp/libevent-2.0.10-stable/
#./configure --prefix=/usr/local/services --disable-debug-mode >/dev/null
#make >/dev/null && make install >/dev/null
⑦re2c-0.13.5.tar.gz
# cd /data/soft/
# tar xf re2c-0.13.5.tar.gz -C tmp/
# cd tmp/re2c-0.13.5/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
⑧libmcrypt-2.5.8.tar.gz
# cd /data/soft/
# tar xf libmcrypt-2.5.8.tar.bz2 -C tmp/
# cd tmp/libmcrypt-2.5.8/
#./configure --prefix=/usr/local/services >/dev/null
#make >/dev/null && make install >/dev/null
# cd libltdl/
# ./configure --prefix=/usr/local/services --enable-ltdl-install >/dev/null
#make >/dev/null && make install >/dev/null
六、安裝PHP的依賴庫
wget http://php.net/distributions/php-5.3.13.tar.gz
php-5.3.13.tar.gz
# cd /data/soft/
# tar xf php-5.3.13.tar.gz -C tmp/
#cd tmp/php-5.3.13/
#CONFOPTS="
--enable-zend-multibyte \
--enable-mbstring \
--enable-sockets \
--enable-pdo \
--enable-zip \
--enable-fpm \
--with-gd \
--with-fpm-user=user_00 \
--with-fpm-group=user_00 \
--with-zlib \
--with-config-file-path=/usr/local/services/php-5.3.13/etc \
--with-libxml-dir=/usr/local/services \
--with-curl=/usr/local/services \
--with-png-dir=/usr/local/services \
--with-jpeg-dir=/usr/local/services \
--with-freetype-dir=/usr/local/services \
--with-mysql=/usr/local/services/mysql \
--with-pdo-mysql=/usr/local/services/mysql \
--with-mysqli=/usr/local/services/mysql/bin/mysql_config \
--prefix=/usr/local/services/php-5.3.13 \
"
# ./configure $CONFOPTS
# make >/dev/null && make install >/dev/null
編譯錯誤解決:
/var/lib/mysql/mysql.sock
configure: error: Cannot find libmysqlclient under /usr.
Note that the MySQL client library is not bundled anymore!
解決方法:
cp -rp /usr/lib64/mysql/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
七、安裝PHP的擴展模塊
①eaccelerator-0.9.6.1.tar.bz2
# cd /data/soft/
#tar xf eaccelerator-0.9.6.1.tar.bz2 -C tmp/
#cd tmp/eaccelerator-0.9.6.1/
#/usr/local/services/php-5.3.13/bin/phpize
#./configure --prefix=/usr/local/services/eaccelerator-0.9.6.1 --enable-eaccelerator --with-php-config=/usr/local/services/php-5.3.13/bin/php-config > /dev/null
#make >/dev/null && make install >/dev/null
#mkdir /tmp/eaccelerator
#chmod 777 /tmp/eaccelerator
②memcached-1.4.13.tar.gz
(服務器端要前安裝,下面的編譯擴展模塊要用到)
# cd /data/soft/
#tar xf memcached-1.4.13.tar.gz -C tmp/
# cd tmp/memcached-1.4.13/
#./configure --enable-64bit --with-libevent=/usr/local/services --prefix=/usr/local/services/memcached-1.4.13 >/dev/null
# make >/dev/null && make install >/dev/null
③libmemcached-0.48.tar.gz
# cd /data/soft/
#tar xf libmemcached-0.48.tar.gz -C tmp/
#cd tmp/libmemcached-0.48/
#CONFOPTS="
--disable-libinnodb
--without-libinnodb-prefix
--with-libevent-prefix=/usr/local/services
--with-memcached=/usr/local/services/memcached-1.4.13/bin/memcached
--prefix=/usr/local/services
"
#./configure $CONFOPTS >/dev/null
#make >/dev/null && make install >/dev/null
④igbinary-1.0.2.tgz
# cd /data/soft/
# tar xf igbinary-1.0.2.tar.gz -C tmp/
#cd tmp/igbinary-1.0.2/
#/usr/local/services/php-5.3.13/bin/phpize
#./configure --enable-igbinary --with-php-config=/usr/local/services/php-5.3.13/bin/php-config >/dev/null
#make >/dev/null && make install >/dev/null
⑤memcache-3.0.5.tgz
# cd /data/soft/
# tar xf memcache-3.0.5.tgz -C tmp/
#cd tmp/memcache-3.0.5/
#/usr/local/services/php-5.3.13/bin/phpize
#CONFOPTS=" \
--enable-memcache \
--with-php-config=/usr/local/services/php-5.3.13/bin/php-config \
"
#./configure $CONFOPTS >/dev/null
#make >/dev/null && make install >/dev/null
⑥memcached-1.0.2.tgz(注意安裝的順序,igbinary-1.1.1.tgz是依賴庫)
# cd /data/soft/
# tar xf memcached-1.0.2.tgz -C tmp/
# cd tmp/memcached-1.0.2/
#/usr/local/services/php-5.3.13/bin/phpize
#CONFOPTS=" \
--enable-memcached \
--enable-memcached-igbinary \
--enable-memcached-json \
--with-libmemcached-dir=/usr/local/services \
--with-php-config=/usr/local/services/php-5.3.13/bin/php-config \
--prefix=/usr/local/services \
"
#./configure $CONFOPTS >/dev/null
#make >/dev/null && make install >/dev/null
⑦owlient-phpredis-2.1.1-1-g90ecd17.tar.gz
# cd /data/soft/
#tar xf owlient-phpredis-2.1.1-1-g90ecd17.tar.gz -C tmp/
# cd tmp/owlient-phpredis-90ecd17/
#/usr/local/services/php-5.3.13/bin/phpize
#./configure --with-php-config=/usr/local/services/php-5.3.13/bin/php-config >/dev/null
#make >/dev/null && make install >/dev/null
九、拷貝配置文件:
# cd /usr/local/services/php-5.3.13/etc
# cp php-fpm.conf.default php-fpm.conf
# cp /soft/php/php-5.3.13/php.ini-production php.ini
7.8、PHP配置文件優化與調整
1.在php-fpm.conf 裏面調整.
;listen = 127.0.0.1:9000
listen = /tmp/php-cgi.tuge.sock #以socke的方式訪問.注視掉.ip端口的方式.
; Default Value: log/php-fpm.log
error_log = /data/php_log/tuge.php.error #根據不同的項目名.定義不同的.sock 和日誌.
# 調整進程數量
pm.max_children:靜態方式下開啓的php-fpm進程數量。
pm.start_servers:動態方式下的起始php-fpm進程數量。
pm.min_spare_servers:動態方式下的最小php-fpm進程數量。
pm.max_spare_servers:動態方式下的最大php-fpm進程數量。
2.在php.ini 加入擴展模塊.
在尾部添加:
[eaccelerator]
zend_extension="/usr/local/services/php-5.3.20/lib/php/extensions/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
擴展模塊增加
extension_dir = "/usr/local/services/php-5.3.13/lib/php/extensions"
extension = memcached.so
extension = redis.so
extension = memcache.so
extension = igbinary.so
3、移動擴展模塊位置
# cd /usr/local/services/php-5.3.13/lib/php/extensions/no-debug-non-zts-20090626/
# mv /usr/local/services/php-5.3.13/lib/php/extensions/no-debug-non-zts-20090626/* /usr/local/services/php-5.3.13/lib/php/extensions
十步:啓動PHP-FPM
修改Nginx的虛擬主機的sock位置
fastcgi_pass unix:/tmp/php-cgi.gongda.sock;
啓動PHP
# cd /usr/local/services/php-5.3.13/sbin
# ./php-fpm
PHP的CGI配置
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
nginx和tomcat實現負載均衡
酒徒词客
2020-07-03 00:44:32
LVSj實現負載均衡
8章-LVS負載均衡 一、爲什麼要使用這個負載均衡技術? 考慮一個問題: 比如某公司有一臺服務器目前支撐了2000左右的用戶,但是隨着用戶的業務
酒徒词客
2020-07-03 00:44:32
網絡存儲ISCSI配置
IP網絡存儲ISCSI IP網絡存儲ISCSI 一、 ISCSI概述 1.1、 介紹 本章主要介紹了基於IP SAN 的網絡存儲ISCSI
酒徒词客
2020-07-03 00:44:32
Xtrabackup數據庫備份工具
酒徒词客
2020-02-20 17:18:49
rysnc和intotify實現數據同步
酒徒词客
2020-02-20 17:18:48
Memcached的配置
酒徒词客
2020-02-20 17:18:47
nginx和tomcat實現負載均衡
酒徒词客
2020-07-03 00:44:32
LVSj實現負載均衡
8章-LVS負載均衡 一、爲什麼要使用這個負載均衡技術? 考慮一個問題: 比如某公司有一臺服務器目前支撐了2000左右的用戶,但是隨着用戶的業務
酒徒词客
2020-07-03 00:44:32
網絡存儲ISCSI配置
IP網絡存儲ISCSI IP網絡存儲ISCSI 一、 ISCSI概述 1.1、 介紹 本章主要介紹了基於IP SAN 的網絡存儲ISCSI
酒徒词客
2020-07-03 00:44:32