Linux php FastCGI

php FastCGI

FastCGI

FastCGI:快速通用网关接口(Fast Common Gateway Interface/FastCGI)是一种让交互程序与Web服务器通信的协议。
FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。
FastCGI具有稳定,安全,高性能,方便扩展等优点。
利用nginx、spawn-fcgi、fcgi就可搭建一个高并发高性能的FastCGI框架。本文将浅析框架的搭建过程。

FastCGI的重要特点总结:

FastCGI是HTTP服务器和动态脚本语言间通信的接口或者工具
FastCGI优点是把动态语言解析和HTTP服务器分离开来
nginx apache以及多数动态语言都支持FastCGI
FastCGI接口方式采用C/S架构,客户端(http服务器),服务国端(动态语言解析服务 器)
PHP动态语言服务可以启动多个FastCGI的守护进程,例如:php-fpm
http服务器通过(例如:nginx FastCGI_PASS)FastCGI客户端和动态语言FastCGI服务 端通信,例如:php-fpm

FastCGI的运行原理:

Nginx不支持对外部程序的直接调用或者解析,所有的外部程序(包括PHP)必须通过FastCGI接口来调用。FastCGI接口在Linux下是socket(这个socket可以是文件socket,也可以是ip socket)。为了调用CGI程序,还需要一个FastCGI的wrapper(wrapper可以理解为用于启动另一个程序的程序),这个wrapper绑定在某个固定socket上,如端口或者文件socket。当Nginx将CGI请求发送给这个socket的时候,通过FastCGI接口,wrapper接收到请求,然后派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本并读取返回数据;接着,wrapper再将返回的数据通过FastCGI接口,沿着固定的socket传递给Nginx;最后,Nginx将返回的数据发送给客户端。这就是Nginx+FastCGI的整个运作过程

Php安装

Php源码安装

 安装依赖

先安装扩展源 yum install epel-release -y
yum install openssl-devel zlib-devel libjpeg-devel freetype-devel gd-devel libcurl-devel libxml2 libxml2-devel mcrypt libmcrypt-devel mhash-devel libxslt-devel libxslt –y

 安装libiconv-devel

wget -c http://ftp.gnu.org/gnu/libiconv/libiconv-1.14.tar.gz;tar xf libiconv-1.14.tar.gz &&\
cd libiconv-1.14;./configure --prefix=/usr/local/libiconv&&make&&make install

 安装libmcrypt库

wget -c ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxvf libmcrypt-2.5.7.tar.gz;cd libmcrypt-2.5.7/;./configure &&make && make install &&/sbin/ldconfig

编译ltdl

wget -c ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxvf libmcrypt-2.5.7.tar.gz;cd libmcrypt-2.5.7/libltdl/;./configure --enable-ltdl-install &&make && make install

修改so文件引用位置

echo "/usr/local/lib">>/etc/ld.so.conf
执行:ldconfig

wget -O /etc/yum.repo.d/epel.epo http://

 安装mhash加密扩展库

wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
./configure;make;make install

yum install mhash mhash-devel -y

rm -f /usr/local/libmcrypt.*
rm -f /usr/local/libmhash*

 安装php 5.3.28

wget http://mirrors.sohu.com/php/php-5.3.28.tar.bz2 ;tar jxf  php-5.3.28.tar.bz2 
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache/bin/apxs --with-mysql=/usr/local/mysql/ &&make &&make install

 Php 5.3 配置参数

./configure --prefix=/usr/local/php \
-with-mysql=mysqlnd \
-with-pdo-mysql=mysqlnd \
-with-iconv-dir=/usr/local/libiconv \
-with-freetype-dir \
-with-jpeg-dir \
-with-png-dir \
-with-zlib \
-with-libxml-dir=/usr/ \
-enable-xml \
-enable-rpath \
-enable-safe-mode \
-enable-bcmath \
-enable-shmop \
-enable-sysvsem \
-enable-inline-optimization \
-with-curl \
-with-curlwrappers \
-enable-mbregex \
-enable-fpm \
enable-mbstring \
-with-mcrypt \
-with-gd \
-enable-gd-native-ttf \
-with-openssl \
-with-mhash \
-enable-pcntl \
-enable-sockets \
-with-xmlrpc \
-enable-zip \
-enable-soap \
-enable-short-tags \
-enable-zend-multibyte \
-enable-static \
-with-xsl \
-with-fpm-user=nginx \
-with-fpm-group=nginx \
-enable-cgi \
-enable-ftp \
-with-ncurses

 Php5.5参数:

./configure --prefix=/usr/local/php \
-with-mysql=mysqlnd \
-with-pdo-mysql=mysqlnd \
-with-iconv-dir \
-with-freetype-dir \
-with-jpeg-dir \
-with-png-dir \
-with-zlib \
-with-libxml-dir=/usr/ \
-enable-xml \
-enable-rpath \
-enable-bcmath \
-enable-shmop \
-enable-sysvsem \
-enable-inline-optimization \
-with-curl \
-enable-mbregex \
-enable-fpm \
-enable-mbstring \
-with-mcrypt \
-with-gd \
-enable-gd-native-ttf \
-with-openssl \
-with-mhash \
-enable-pcntl \
-enable-sockets \
-with-xmlrpc \
-enable-soap \
-enable-short-tags \
-enable-static \
-with-xsl \
-with-fpm-user=nginx \
-with-fpm-group=nginx \
-enable-ftp \
-enable-cgi \
-enable-opcache=no
&&make &&make install

Php yum安装

yum list installed | grep php 

先删除已有的php版本 ,执行下面的命令删除php

yum remove php-common

然后像安装那样问你是否继续的,输入yes即可

添加 yum 源

CentOS 6.x 的源

rpm -Uvh http://download.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

CentOS 7.x 的源

    rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 
    rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

安装 PHP

yum install --enablerepo=remi,remi-php56 php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysql php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common

注:安装5.6版本为remi-php56,安装5.5版本为remi-php55

Php服务与端口

启动: /usr/local/php/sbin/php-fpm

端口: 127.0.0.1:9000

.

Php配置文件

------- php-fpm.conf 主进程配置文件 ----------------------------------------------------------------------------

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

pid = /var/logs/php/php-fpm.pid
error_log=/var/logs/php-fpm.log
log_level=error
文件描述符大小
rlimit_file=32768
events.mechanism=epoll
listen.owner=nginx
listen.group=nginx
子进程数量
pm.max_children=1024
pm.start_servers=16
空闲进程
pa.min_spare_servers=5
pa.max_spare_servers=20
空闲时间
pm.process_idle_timeout=15s
每个子进程最大请求
pm.max_requests=2048;
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
slowlog=/var/log/php/$spool.log.slow
request_slowlog_timeout=10
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f www@my.domain.com

#优化PHP服务器

eAccelerator、APC、Xcache、ZEND属于一个免费的开源php加速、优化、编译和动态缓存项目,原理和apc类似,都是通过缓存php编译后的opcode代码来提高php脚本的执行性能。
目前最新的版本下载地址:
https://codeload.github.com/eaccelerator/eaccelerator/legacy.tar.gz/master 
tar -xzf master ;cd 
eaccelerator-eaccelerator-42067ac/;phpize ;
./configure --enable-eaccelerator=shared --with-php-config=/usr/bin/php-config
make && make install
然后在/etc/php.ini末尾加入如下代码:
extension="/usr/lib64/php/modules/eaccelerator.so"

eaccelerator.shm_size="64"
指定 eAccelerator 能够使用的共享内存数量,单位:MB。
"0" 代表操作系统默认,默认值为 "0",一般设置为64或128

eaccelerator.cache_dir="/data/eaccelerator"
用户磁盘缓存的目录。eAccelerator 在该目录中存储预编译代码、session 数据、内容等。相同
的数据也可以存储于共享内存中(以获得更快的存取速度)。默认值为 "/tmp/eaccelerator"。

eaccelerator.enable="1"
开启或关闭 eAccelerator。"1" 为开启,"0" 为关闭。默认值为 "1"。

eaccelerator.optimizer="1"
开启或关闭内部优化器,可以提升代码执行速度。"1" 为开启,"0" 为关闭。默认值为 "1"。

eaccelerator.check_mtime="1"
开启或关闭 PHP 文件改动检查。"1" 为开启,"0" 为关闭。如果您想要在修改后重新编译 
PHP程序则需要设置为 "1"。默认值为 "1"。

eaccelerator.debug="0"
开启或关闭调试日志记录。"1" 为开启,"0" 为关闭。默认值为 "0"。

eaccelerator.filter=""
判断哪些 PHP 文件必须缓存。您可以指定缓存和不缓存的文件类型(如 "*.php *.phtml"等),如果参数以 "!" 开头,则匹配这些参数的文件被忽略缓存。默认值为 "",即,所有 PHP 文件都将被缓存。

eaccelerator.shm_max="0"
当使用 " eaccelerator_put() " 函数时禁止其向共享内存中存储过大的文件。该参数指定允许存储的最大值,单位:字节 (10240, 10K, 1M)。"0" 为不限制。默认值为 "0"。

eaccelerator.shm_ttl="0"
当 eAccelerator 获取新脚本的共享内存大小失败时,它将从共享内存中删除所有在最后 "shm_ttl" 秒内无法存取的脚本缓存。默认值为 "0",即:不从共享内春中删除任何缓存文件。

eaccelerator.shm_only="0"
允许或禁止将已编译脚本缓存在磁盘上。该选项对 session 数据和内容缓存无效。默认值为 "0",即:使用磁盘和共享内存进行缓存。

eaccelerator.compress="1"
允许或禁止压缩内容缓存。默认值为 "1",即:允许压缩。

eaccelerator.compress_level="9"
指定内容缓存的压缩等级。默认值为 "9",为最高等级。

php升级

下载源:
分以下步骤進行
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm
设置源:
vim /etc/yum.repos.d/remi.repo
進去把所有的enabled参数改成1
执行命令: yum –enablerepo=remi update php* mysql*  
安装php 
yum -y update php*

Php故障处理

7.1 error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

解决方法:yum install libxslt-devel

7.2 error while loading shared libraries: xxx.so.0:cannot open shared object file: No such file or directory
原因:
找不到libmysqlclient.so.18
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/

解决方法:
php-5.3.28]# touch ext/phar/phar.phar

一般而言,有很多so檔會在/usr/local/lib這個目錄下,所以在/etc/ld.so.conf中加入/usr/local/lib這一行,可以解決此問題。

將 /etc/ld.so.conf存檔後,還要執行「/sbin/ldconfig –v」來更新一下才會生效

方法2:  echo "/usr/local/lib">>/etc/ld.so.conf
            执行:ldconfig

7.3 /usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [sapi/fpm/php-fpm] 错误 1

解决方法:安装libmcrypt 实现加密功能的库。如果已经yum安装,在此包中有ltdl的安装目录,进入该目录编译
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/libltdl/
./configure --enable-ltdl-install
make
make install

7.4 /data/tools/php-5.3.28/sapi/cli/php: error while loading shared libraries: libltdl.so.3: cannot open shared object file: No such file or directory

make[1]: *** [install-pear-installer] 错误 127
make: *** [install-pear] 错误 2

ln -s  /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3

echo "/usr/local/lib">>/etc/ld.so.conf
            执行:ldconfig

转载于:https://blog.51cto.com/zhongliang/2152907

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值