LAMP 之 php-fpm 相关介绍
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LAMP 之 php-fpm 相关介绍相关的知识,希望对你有一定的参考价值。
1 概述
php-FPM(FastCGI Process Manager:FastCGI进程管理器)是一个PHPFastCGI管理器,对于PHP 5.3.3之前的php来说,是一个补丁包 ,旨在将FastCGI进程管理整合进PHP包中。如果你使用的是PHP5.3.3之前的PHP的话,就必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。
使得php运行于fpm模式运行,使得php形成一个独立模块,而不仅仅是httpd下的一个模块,php可以监听自己的端口。php和http可以安装与两台机器上,但是一般情况下是将php和http安装在同一主机下,如果要分开安装,由于http服务器将php的文档都发给php-fpm这台机器处理,所以,此时加速的软件包xcache需要安装在php-fpm这台机器上。安装完成后,需要在对应http服务器上做相应的配置
注意,php-fpm(独立运行)和php(模块方式运行)是二选一,只能安装一个,
.CentOS 6,在PHP-5.3.2之前:默认不支持fpm机制;需要自行打补丁并编译安装,httpd-2.2:默认不支持fcgi协议,需要自行编译此模块,解决方案:编译安装httpd-2.4, php-5.3.3+
.CentOS 7:httpd-2.4:rpm包默认编译支持了fcgi模块php-fpm包:专用于将php运行于fpm模式
2 配置fastcgi
.php配置
.php.ini核心配置的详细说明:
http://php.net/manual/zh/ini.core.php
.Php.ini配置选项列表:
http://php.net/manual/zh/ini.list.php
.配置文件:/etc/php.ini,/etc/php.d/*.ini
.Module下,重启Httpd服务
.FastCGI模式下,重启php-fpm服务
.配置文件格式
.配置文件php-fpm.conf.default的格式是格式:
[foo]:Section Header Directive=value
.注释符:#纯粹的注释信息
; 用于注释可启动的指令
在较新的版本中,已经完全使用”;”进行注释
.fcgi服务配置文件:/etc/php-fpm.conf,/etc/php-fpm.d/*.conf
.连接池:
pm = static|dynamic static:固定数量的子进程;pm.max_children dynamic:子进程数量以动态模式管理 pm.max_children pm.start_servers pm.min_spare_servers pm.max_spare_servers pm.max_requests= 500 .确保运行php-fpm进程的用户对session目录有读写权限 mkdir /var/lib/php/session chown apache.apache /var/lib/php/session
配置httpd的配置文件
添加/etc/httpd/conf.d/fcgi.conf配置文件,内容如下:
vim /etc/httpd/conf.d/fcgi.conf .DirectoryIndexindex.php .ProxyRequestsOff .ProxyPassMatch ^/(.*\.php)$fcgi://127.0.0.1:9000/var/www/html/$1 #这里的$1是后项引用 #这里如果php-fcg的php是另一台,需要将127.0.0.1替换成对应机器的ip,如fpm模式的php的ip是172.18.50.73,这里就要将127.0.0.1替换成172.18.50.73.如果fpm安装在本机,就填127.0.0.1即可 #注意:在HTTPD服务器上必须启用proxy_fcgi_module模块,充当PHP客户端。
查看模块
httpd –M |grep fcgi cat /etc/httpd/conf.modules.d/00-proxy.conf
fcgi.conf配置文件中启用以下两行
LoadModule proxy_modulemodules/mod_proxy.so LoadModule proxy_fcgi_modulemodules/mod_proxy_fcgi.so
在fastcgi模式下,可以实现虚拟主机,相关配置如下,注意这个不是编译安装,是通过yum安装的配置如下。
vim /etc/httpd/conf.d/vhosts.conf DirectoryIndex index.php <VirtualHost *:80> ServerName www.b.net DocumentRoot /apps/vhosts/b.net ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/apps/vhosts/b.net/$1 <Directory"/apps/vhosts/b.net"> OptionsNone AllowOverride +None Requireall granted </Directory> </VirtualHost>
3 CentOS 7安装LAMP(PHP-FPM模式)
3.1 yum安装PHP-FPM
首先要卸载PHP
yum install php-fpm
.查看php-fpm所对应的配置文件
rpm -ql php-fpm /usr/lib/systemd/system/php-fpm.service /etc/logrotate.d/php-fpm /etc/php-fpm.conf /etc/php-fpm.d /etc/php-fpm.d/www.conf /etc/sysconfig/php-fpm /run/php-fpm
.PHP-FPM常见配置
daemonize= no //是否将程序运行在后台 listen = 127.0.0.1:9000 //FPM 监听地址,如果改成listen = 9000就会监听本机所有ip的9000端口 listen.backlog= -1 //等待队列的长度-1表示无限制 listen.allowed_clients= 127.0.0.1 //仅允许哪些主机访问,如果注释这个配置,那么所有的主机都将可以访问,这里127.0.0.1仅允许本机访问。 pm = dynamic //PM是动态运行还是静态运行 //static 固定数量的子进程,pm.max_childen //dynamic子进程数据以动态模式管理 pm.start_servers pm.min_spare_servers pm.max_spare_servers pm.max_requests= 500 php_value[session.save_handler] =files php_value[session.save_path] =/var/lib/php/session //设置session存放位置
.启动PHP-FPM
systemctl start php-fpm
3.2 编译安装PHP-FPM
install_php_fpm(){ echo "now install php-5.6.31.tar.bz2" echo "install libxml2-devel bzip2-devel libmcrypt-devel,please wait..." yum -y install openssl-devel libxml2-devel bzip2-devel &>/dev/null || { echo "something wrong when install openssl-devel libxml2-devel bzip2-devel,Please check local yum resource,script exist now";exit; } yum -y install libmcrypt-devel &>/dev/null || { echo "something wrong when install libmcrypt-devel,Please check epel yum resource,script exist now";exit; } get_package; echo "now unzip php-5.6.31.tar.bz2,wait a minite" tar xf /root/package/package."$time"/php-5.6.31.tar.bz2 -C /usr/local echo "now configure php" cd /usr/local/php-5.6.31 ./configure --prefix=/app/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-mcrypt --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php.d --with-bz2 &>/dev/null || { echo "something wrong when configure php,script will exist,please check";exit; } make &>/dev/null || { echo "something wrong when make php,script will exist,please check";exit; } make install &>/dev/null || { echo "something wrong when make install php,script will exist,please check";exit; } [ -e /etc/php ] || mkdir /etc/php/ cp /usr/local/php-5.6.31/php.ini-production /etc/php/php.ini cp /usr/local/php-5.6.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm chkconfig --add php-fpm cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf sed -i.bak ‘s/#LoadModule proxy_module modules\/mod_proxy.so/LoadModule proxy_module modules\/mod_proxy.so/g‘ /app/httpd24/conf/httpd.conf sed -i ‘s/#LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/LoadModule proxy_fcgi_module modules\/mod_proxy_fcgi.so/g‘ /app/httpd24/conf/httpd.conf #config httpd sed -i.bak ‘s/DirectoryIndex index.html/DirectoryIndex index.php index.html/g‘ /app/httpd24/conf/httpd.conf echo "AddType application/x-httpd-php .php" >> /app/httpd24/conf/httpd.conf echo "AddType application/x-httpd-php-source .phps" >> /app/httpd24/conf/httpd.conf echo "ProxyRequests Off" >>/app/httpd24/conf/httpd.conf #If your php_fpm is not install in the local host,you should replace 127.0.0.1 with php_fpm host ip. echo "ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/app/httpd24/htdocs/\$1" >>/app/httpd24/conf/httpd.conf cat >/app/httpd24/htdocs/index.php <<EOF <h1> <?php \$mysqli=new mysqli("$serverip","wpadmin","Pass123456"); if(mysqli_connect_errno()){ echo "it is falilure!"; \$mysqli=null; exit; } echo "nice job,it is connected..."; \$mysqli->close(); phpinfo(); ?> </h1> EOF echo "PATH=/app/php/bin:/app/httpd24/bin/:/usr/local/mysql/bin/:$PATH" >>/etc/profile.d/lamp.sh echo "php_fpm has been complete configure,now resatart php_fpm and httpd24" service php-fpm restart &>/dev/null && echo "php-fpm has been restart OK,you can test now..." || { echo "Something wrong when startphp-fpm,it will exit,please check";exit; } /app/httpd24/bin/apachectl restart &>/dev/null && echo "Httpd24 has been restart OK,you can test now..." || { echo "Something wrong when start httpd24,it will exit;please check";exit; }
本文出自 “阳光运维” 博客,请务必保留此出处http://ghbsunny.blog.51cto.com/7759574/1972149
以上是关于LAMP 之 php-fpm 相关介绍的主要内容,如果未能解决你的问题,请参考以下文章
LAMP架构之httpd+(php-fpm)+mariadb
LAMP架构之httpd+(php-fpm)+mariadb