• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP-FPM实现性能优化_PHP

php 搞代码 3年前 (2022-01-26) 16次浏览 已收录 0个评论

简介:

PHP-FPM 是一个 PHP FastCGI 管理器,一般 Nginx 上面跑 PHP 程序都会将 PHP 程序丢给 PHP-FPM 来解析。好了,就这样!

PHP 5.4 开始集成了 PHP-FPM ,也就是说编译 PHP 时,只要 –enable-fpm 就装好了 PHP-FPM 。

一、安装 PHP-FPM

shell > ./configure --prefix=/usr/local/php<div style="color:transparent">!本文来源gaodai.ma#com搞#代!码(网</div><em>搞gaodaima代码</em> \--with-config-file-path=/usr/local/php --with-mysql=/usr/local/mysql/ \--with-mysqli=/usr/local/mysql/bin/mysql_config --with-gd --with-xsl --with-bz2 \--with-zlib --with-curl --with-pear --without-iconv --with-mcrypt \--with-gettext --with-openssl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir \--with-libdir=lib64 --enable-ftp --enable-fpm --enable-opcache --enable-exif --enable-soap --enable-bcmath --enable-calendar \--enable-sockets --enable-mbstring --enable-gd-native-ttf --disable-rpath --disable-debug

## 看到上面这堆参数了没有,这是在编译 PHP ,其中有一个参数是 –enable-fpm 没错,这就是启用 PHP-FPM 扩展。

shell > make; make install

二、配置 PHP-FPM

shell > cp /usr/local/src/php-5.6.17/php.ini-production /usr/local/php/php.ini # 这是 PHP 的配置文件shell > cp /usr/local/src/php-5.6.17/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 这是 PHP-FPM 的启动脚本shell > cd /usr/local/php/etc/shell > cp php-fpm.conf.default php-fpm.conf # 复制一份配置文件shell > vim php-fpm.conf[global]pid = run/php-fpm.pid # PIDrlimit_files = 65535 # 打开文件数限制[www] # 进程池user = nginx # 以 nginx 身份运行group = nginxlisten = 127.0.0.1:9000 # 监听本机的 9000 端口;listen = /dev/shm/php-cgi.sock; # 监听 UNIX SOCKET ,并把 SOCKET 放在了内存空间中,速度更快 ( Nginx 也要相应修改 )!;listen.backlog = 10240 # UNIX SOCKET 的方式高并发下有点不稳定,该参数用来缓解 ( SOCKET 等待队列长度 );listen.owner = nginx # UNIX SOCKET 的权限;listen.group = nginx;listen.mode = 0660pm = dynamic # 创建进程的方式,动态创建pm.max_children = 32 # 最大进程数 ( 不能只看内存来创建,要看具体使用率,有时内存足够,进程数大多时,导致 CPU 频繁上下文切换,负载会很高 )pm.start_servers = 5 # 初始进程数pm.min_spare_servers = 5 # 最小空闲进程数pm.max_spare_servers = 10 # 最大空闲进程数pm.status_path = /php_status # PHP-FPM 状态监控 ( Nginx 要设置访问权限 )shell > service php-fpm start

三、监控 PHP-FPM

shell > vim /usr/local/nginx/conf/nginx.conflocation ~ /php_status { # 创建一个单独的 server 或直接在 server {} 中加入配置  access_log off;  allow 127.0.0.1;  allow 36.110.41.194; # 做好权限  deny all;  fastcgi_pass 127.0.0.1:9000; # 如果是 UNIX SOCKET 的方式,要类似这样写: fastcgi_pass unix:/dev/shm/php-cgi.sock;  fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;  include fastcgi_params;}shell > kill -HUP `cat /usr/local/nginx/logs/nginx.pid`shell > curl http://127.0.0.1/php_status # 访问该路径得到如下数据pool: www               # 进程池名称process manager: dynamic        # 进程管理方式start time: 22/Jan/2016:15:49:00 +0800 # 启动时间start since: 375            # 运行时长accepted conn: 7            # 当前进程池接受的请求数listen queue: 0            # 请求等待队列,如果不为 0 ,意味着 FPM 进程不足,需要增加max listen queue: 0          # 最大等待队列数量listen queue len: 1024         # SOCKET 等待队列长度idle processes: 4           # 空闲进程数active processes: 1          # 活跃的进程数total processes: 5           # 总进程数max active processes: 1        # 最大活跃进程数max children reached: 0        # 达到最大进程数的次数,如果不为 0 ,意味着最大进程数不足,需要增加slow requests: 0            # 慢请求数量,需要设置 slow logshell > curl http://127.0.0.1/php_status # 这里有多种参数供选择,例如: http://127.0.0.1/php_status?html 、?json 、?xml 、?full

# 我想,用 python 脚本用做个监控,?json 格式是最好不过了吧!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP-FPM实现性能优化_PHP

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址