php-fpm安装以及基本知识

上一篇 / 下一篇  2015-10-14 14:15:05 / 个人分类:测试技能

参考http://www.111cn.net/sys/nginx/80388.htm

PHP-FPM 

php-fpm是一个php FastCGI的管理器。首先先了解一下CGI与fastCGI

CGI基本概念

官方解释:(common GateWay interface)公共网关接口,是外部应用程序与web服务器之间的接口标准,是在cgi程序和web服务器之间传递信息的规程

也就是说cgi是为了保证web server传递过来的数据是标准格式的。举个例子,web server是内容的发布者,如nginx,如果请求index.html,那么nginx回去文件系统找到

这个文件,发送个浏览器,分发的是静态数据。如果请求的index.php,根据配置文件,nginx知道这个不是静态文件,需要去找php解释器来处理,那么就会把这个请求简单

处理下交给php解释器,nginx会传哪些数据给php解释器呢?这个就是CGI来规定的。到这里,我们清楚CGI是神马了。

 

当nginx收到index.php请求后,会启动CGI程序。这里就是php解释器。接下来php解释器会解析php.ini文件,初始化执行环境,然后处理请求,再以规定cgi规定格式返回

处理后的结果,退出进程。nginx再把结果返回给浏览器。

 

FastCGI基本概念

那fastcgi又是什么呢?fastcgi是用来提高cgi程序的性能的

提 高性能,说明cgi程序存在性能问题,问题在哪?  ‘’php解释器会解析php.ini文件,初始化执行环境‘’ 就是这里了。标准的cgi对每个请求都会执行这些步骤(每执行一个请求,启动一次进程,是非常消耗资源的)所以处理请求的时间就会长,这肯定是不合理的。 那么fastcgi就是优化这个问题的,它是怎么做的呢?首先fastcgi会启动一个master,解析配置文件,初始化执行环境,然后再启动多个 worker,当请求过来是,master会传递给一个worker,然后立即接受下一个请求,,这样就避免重复启动和初始化工作了,提高效率。当 worker空闲太多,会停掉一些worker,也可以预先启动一些worker等待请求。这就是fastcgi对进程的管理

 

php-fpm是fastcgi进程的管理,用来管理fastcgi进程的

php的解释器是php-cgi,php-cgi只是个cgi程序,他自己本身只能解析请求,返回结果,不会进行进程管理,所以需要一些调度php-cgi进程的程序。php-fpm就是调度解释器的东东。

 

php-fpm与nginx配置与使用

一、php-fpm 安装

1、使用源码安装

安装前准备:

centos下执行

yum -y install gcc automake autoconf libtool make

yum -y install gcc gcc-c++ glibc

yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-devel

 

新版php-fpm安装(推荐安装方式)

wgethttp://cn2.php.net/distributions/php-5.4.7.tar.gz
tar zvxf php-5.4.7.tar.gz
cd php-5.4.7
./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir

make all install
旧版手动打补丁php-fpm安装(旧版程序已经没有了,大家新版的吧,这里做个展示)
wgethttp://cn2.php.net/get/php-5.2.17.tar.gz
wgethttp://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc\
-with-mysql=/usr/local/mysql\
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring\
-with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml\
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets  -with-bz2 -with-curl -with-curlwrappers\
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv-enable-bcmath\
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo\
-with-pdo-mysql
make all install

编译安装需要几分钟

以上两种方式都可以安装php-fpm,安装后内容放在/usr/local/php目录下

 

下面是对php-fpm运行用户进行设置

cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf
修改
user = www-data
group = www-data

如果www-data用户不存在,那么先添加www-data用户
groupadd www-data
useradd -g www-data www-data

 

二、编译安装nginx

然后按照http://www.nginx.cn/install安装nginx

 

三、修改nginx配置文件以支持php-fpm

nginx安装完成后,修改nginx配置文件为,nginx.conf

其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误

/etc/nginx/nginx.conf 

server {
    listen 8080;   //服务端端口
    server_name    localhost;
    root /letv/yanmin/www;  //php文件的地址
    location / {
            index  index.html index.htm index.php;
            if(!-e $request_filename)
            {
                rewrite ^/(.*) /index-dev.php last;
            }
    }
   location ~ \.php$ {
       fastcgi_pass  127.0.0.1:9000;    //该端口在/etc/php-fpm.d/www.conf   中listen = 127.0.0.1:9000端口号
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
       client_max_body_size 40m;
  }
#配置更新完reload下
./nginx -s reload

四、创建测试php文件与html文件

 

1、创建html文件

在/letv/yanmin/www下添加html文件

[root@beta.internal.beta.14www]# cat index.html
<h1>hello world!</h1>
 
#通过http协议访问该html文件
[root@beta.internal.beta.14www]# curl -i'http://localhost:8080/index.html'
HTTP/1.1200OK
Server: nginx
Date: Tue,13Oct201509:55:38GMT
Content-Type: text/html
Content-Length:22
Last-Modified: Tue,13Oct201509:36:10GMT
Connection: keep-alive
Accept-Ranges: bytes
 
<h1>hello world!</h1>

 2、创建php文件

在/usr/local/nginx/html下创建index.php文件,输入如下内容

<?php
    echo phpinfo();
?>

[root@beta.internal.beta.14www]# cat index.php
<?php
phpinfo();
?>

在本地配置hosts

#127.0.0.1      localhost
10.154.156.14         localhost

本地使用浏览器访问 http://localhost:8080/index.php。显示如图:

五、php-fpm 5.4.7 如何关闭 重启?

php 5.4.7 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

php-fpm 关闭:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重启:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

查看php-fpm进程数:

psaux | grep -c php-fpm

8.命令行下执行php,提示找不到命令

-bash: /usr/bin/php: No such file or directory

vi /etc/profile

在文件底部增加一行配置
export PATH=/usr/local/php/bin:$PATH

保存退出

source /etc/profile


TAG: 知识

 

评分:0

我来说两句

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 93286
  • 日志数: 31
  • 建立时间: 2015-05-22
  • 更新时间: 2016-05-17

RSS订阅

Open Toolbar