PHP-FPM(FastCGI Process Manager)和Apache是两种不同的Web服务器软件,它们可以通过多种方式协同工作以提供高效的PHP处理能力。以下是几种常见的配置方法:
安装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/
目录下),添加以下内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# Proxy PHP requests to PHP-FPM
<FilesMatch \.php$>
SetHandler "proxy:fcgi://unix:/run/php/php7.x-fpm.sock|fcgi://localhost:9000"
</FilesMatch>
</VirtualHost>
重启Apache:
sudo systemctl restart apache2
安装PHP-FPM: 确保你的系统上已经安装了PHP-FPM。
配置PHP-FPM: 编辑PHP-FPM的配置文件,确保监听地址和端口设置正确。
启用Apache的mod_fastcgi模块:
sudo a2enmod fastcgi
配置Apache虚拟主机: 编辑你的Apache虚拟主机配置文件,添加以下内容:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
# 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
<FilesMatch \.php$>
SetHandler fastcgi-script
</FilesMatch>
</VirtualHost>
重启Apache:
sudo systemctl restart apache2
虽然这不是直接使用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的协同工作。