PHP-FPM(FastCGI Process Manager)和Apache是两种不同的Web服务器软件,它们可以通过多种方式协同工作以提供高效的PHP处理能力。以下是几种常见的配置方法:
方法一:使用Apache的mod_proxy_fcgi模块
-
安装PHP-FPM: 确保你的系统上已经安装了PHP-FPM。你可以使用包管理器来安装,例如在Ubuntu上:
sudo apt-get install php-fpm -
配置PHP-FPM: 编辑PHP-FPM的配置文件(通常是
/etc/php/7.x/fpm/pool.d/www.conf),确保监听地址和端口设置正确。例如:listen = /run/php/php7.x-fpm.sock -
启用Apache的mod_proxy和mod_proxy_fcgi模块:
sudo a2enmod proxy sudo a2enmod proxy_fcgi -
配置Apache虚拟主机: 编辑你的Apache虚拟主机配置文件(通常在
/etc/apache2/sites-available/目录下),添加以下内容::80 > ServerName example.com DocumentRoot /var/www/htmlOptions Indexes FollowSymLinks AllowOverride All Require all granted # Proxy PHP requests to PHP-FPM SetHandler "proxy:fcgi://unix:/run/php/php7.x-fpm.sock|fcgi://localhost:9000" -
重启Apache:
sudo systemctl restart apache2
方法二:使用Apache的mod_fastcgi模块
-
安装PHP-FPM: 确保你的系统上已经安装了PHP-FPM。
-
配置PHP-FPM: 编辑PHP-FPM的配置文件,确保监听地址和端口设置正确。
-
启用Apache的mod_fastcgi模块:
sudo a2enmod fastcgi -
配置Apache虚拟主机: 编辑你的Apache虚拟主机配置文件,添加以下内容:
:80 > ServerName example.com DocumentRoot /var/www/htmlOptions Indexes FollowSymLinks AllowOverride All Require all granted # Proxy PHP requests to PHP-FPM AddHandler fastcgi-script .php Action fastcgi-script /fastcgi-php Alias /fastcgi-php /usr/lib/cgi-bin/fastcgi-php FastCgiExternalServer /usr/lib/cgi-bin/fastcgi-php -socket /run/php/php7.x-fpm.sock -pass-header Authorization SetHandler fastcgi-script -
重启Apache:
sudo systemctl restart apache2
方法三:使用Nginx作为反向代理
虽然这不是直接使用Apache,但Nginx作为反向代理可以更高效地处理PHP请求,并将它们转发给PHP-FPM。
-
安装Nginx和PHP-FPM:
sudo apt-get install nginx php-fpm -
配置PHP-FPM: 编辑PHP-FPM的配置文件,确保监听地址和端口设置正确。
-
配置Nginx: 编辑Nginx的配置文件(通常是
/etc/nginx/sites-available/default),添加以下内容:server { listen 80; server_name example.com; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.x-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } -
重启Nginx:
sudo systemctl restart nginx
通过以上方法,你可以选择最适合你需求的方式来配置PHP-FPM和Apache的协同工作。
以上就是关于“php-fpm与Apache如何协同工作”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm