Nginx 黑魔法:使用 NGX-PHP 模块低成本实现高性能应用

2023-05-2314:03:56服务器及运维Comments887 views字数 1825阅读模式

提到 “NGX 和 PHP”,使用过 Nginx 和 PHP 的同学第一反映可能是 Nginx + PHP-FPM 这种架构。不过,这篇文章中,我们要提到的技术架构更简单高效一些:直接使用 Nginx 和三方模块(NGX-PHP),调用 PHP Embedded 库,来实现原本需要跨进程实现的功能,从而明显提升应用性能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

之所以能够这样玩,需要感谢下面两个项目的相关实现:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

  • PHP 提供了一种有趣的调用方式:让其他的程序能够通过支持 C Bindings 的符号绑定的方式来调用它的核心引擎,Zend。这种接口调用方式,被称作 PHP SAPI 或者 PHP-Embeded,项目地址:https://github.com/php/php-src/tree/master/sapi/embed
  • 2016 年,有一位来自搜狐的工程师 rryqszq4,开始在 GitHub 上尝试开源一个项目,把 “Nginx” 和 “PHP-Embeded Library” 桥接到一起,这个项目经过多年发展,陆续支持了 PHP5、PHP7,以及最新的 PHP8。项目地址:https://github.com/rryqszq4/ngx-php

在 Techem Power 的测试中,自 2020 年开始,“NGX-PHP” 这个技术选型出现之后,便取得了不错的成绩,比如:2020 年的Round 19,以及 2022 年的Round 21文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

Nginx 黑魔法:使用 NGX-PHP 模块低成本实现高性能应用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

2020 年和 2022 年的两轮框架评分测试

在最近的 2022 年测试中,框架开销非常低,位于排行榜第五和第六名。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

Nginx 黑魔法:使用 NGX-PHP 模块低成本实现高性能应用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

2022 年测试中,框架开销排行

如果用我们熟悉的 Node.js + MongoDB 作为基准,那么这套方案开销比它少 300%:跑的快,吃草少。换个角度来看,这个方案非常贴合 “Nginx” 和 “PHP” 的特性:快糙猛。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

好了,关于这个项目的概况就介绍到这里,我们先来使用 Docker 快速、实际的感受下它的性能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

快速体验

执行下面的命令:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

docker run --rm -it -v `pwd`/data:/usr/share/nginx/html/data:rw -p 8090:80 soulteary/ngx-php:8-microblog

当 Docker 镜像下载完毕之后,我们将看到一个和普通 Nginx 镜像启动无异的日志输出:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/10/05 10:25:39 [notice] 1#1: using the "epoll" event method
2022/10/05 10:25:39 [notice] 1#1: nginx/1.23.1
2022/10/05 10:25:39 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219) 
2022/10/05 10:25:39 [notice] 1#1: OS: Linux 5.10.76-linuxkit
...

打开浏览器,输入 http://localhost:8090 ,就能够看到效果啦。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/41740.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/41740.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定