PHP-FPM(FastCGI Process Manager)和Nginx的高效配合可以显著提高PHP应用程序的性能。以下是一些关键步骤和最佳实践,以确保两者之间的顺畅通信:
首先,确保你已经安装了PHP-FPM。以下是一个基本的安装示例:
sudo apt-get update sudo apt-get install php-fpm 编辑PHP-FPM配置文件(通常位于 /etc/php/7.x/fpm/pool.d/www.conf),确保以下设置正确:
; 监听的地址和端口 listen = /run/php/php7.x-fpm.sock ; 监听的地址和端口(如果使用TCP) ; listen = 127.0.0.1:9000 ; 用户和组 user = www-data group = www-data ; PM模式和进程管理 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 编辑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; # 使用Unix套接字 # fastcgi_pass 127.0.0.1:9000; # 或者使用TCP fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 启动PHP-FPM和Nginx服务:
sudo systemctl start php7.x-fpm sudo systemctl start nginx 确保服务正常运行:
sudo systemctl status php7.x-fpm sudo systemctl status nginx pm.max_children 和其他相关参数。gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; listen 443 ssl http2; systemd-cgtop 或其他监控工具来监控PHP-FPM的性能。通过以上步骤,你可以确保PHP-FPM和Nginx之间的高效配合,从而提升PHP应用程序的性能和稳定性。