在Linux系统中,PHP-FPM(FastCGI Process Manager)和Apache可以通过多种方式协同工作,以提供高效的PHP处理能力。以下是两种常见的配置方法:
方法一:使用mod_proxy_fcgi模块
-
安装Apache和PHP-FPM: 确保你已经安装了Apache和PHP-FPM。如果没有安装,可以使用包管理器进行安装。
sudo apt-get update sudo apt-get install apache2 php-fpm -
启用必要的Apache模块: 启用
proxy和proxy_fcgi模块。sudo a2enmod proxy sudo a2enmod proxy_fcgi -
配置Apache虚拟主机: 编辑Apache的虚拟主机配置文件(通常位于
/etc/apache2/sites-available/目录下),添加以下内容::80 > ServerAdmin webmaster@localhost DocumentRoot /var/www/htmlOptions Indexes FollowSymLinks AllowOverride All Require all granted # PHP-FPM configuration SetHandler "proxy:fcgi://localhost:9000" ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined 这里的关键是
SetHandler "proxy:fcgi://localhost:9000",它将PHP文件的请求转发到运行在本地端口9000上的PHP-FPM进程。 -
重启Apache: 使配置生效。
sudo systemctl restart apache2
方法二:使用mod_fastcgi模块
-
安装Apache和PHP-FPM: 同样,确保你已经安装了Apache和PHP-FPM。
-
启用必要的Apache模块: 启用
fastcgi和proxy_fcgi模块。sudo a2enmod fastcgi sudo a2enmod proxy_fcgi -
配置Apache虚拟主机: 编辑Apache的虚拟主机配置文件,添加以下内容:
:80 > ServerAdmin webmaster@localhost DocumentRoot /var/www/htmlOptions Indexes FollowSymLinks AllowOverride All Require all granted # PHP-FPM configuration SetHandler fastcgi-script FastCgiExternalServer /var/run/php/php7.4-fpm.sock -socket /var/run/php/php7.4-fpm.sock -pass-header Authorization ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined 这里的关键是
SetHandler fastcgi-script和FastCgiExternalServer,它们将PHP文件的请求转发到PHP-FPM的Unix套接字。 -
重启Apache: 使配置生效。
sudo systemctl restart apache2
总结
这两种方法都可以实现PHP-FPM和Apache的协同工作,选择哪种方法取决于你的具体需求和环境。使用mod_proxy_fcgi模块通常更灵活,支持TCP和Unix套接字,并且可以更好地处理负载均衡和高并发请求。而使用mod_fastcgi模块则更简单直接,适用于简单的配置场景。
以上就是关于“Linux中php-fpm与Apache如何协同工作”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm