FastCGI(PHP)
一、什么是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 1.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.13/lib/php/extensions/no-debug-non-zts-20090626/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 = memcached.so extension = redis.so extension = memcache.so extension = igbinary.so 十、启动PHP-FPM #cd ../sbin #./php-fpm 十一、Nginx配置文件调整 #vim /usr/local/services/nginx-0.8.55/conf/vhost/vhost.zhangyi.com #可以指定多个localtion进行不同的指令处理,这里是指定php的sock location ~ \.php$ { fastcgi_pass unix:/tmp/php-cgi.zhangyi.sock; #修改这里 fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param SERVER_NAME $http_host; fastcgi_ignore_client_abort on; } 十二、重启Nginux #cd /usr/local/services/nginx-0.8.55/sbin #./nginx -s reload 十三、测试 #cd /data/www #mv index.html index.php #vim index.php 在firefox浏览器上登录
网页标题:FastCGI(PHP)
文章转载:http://ybzwz.com/article/gicged.html