要解决Linux中PHP-FPM慢的问题,可以从多个方面进行优化。以下是一些常见的优化方法:
static、dynamic、ondemand)。pm.max_children):根据服务器内存和CPU资源合理设置,避免设置过大导致内存耗尽。pm.start_servers):建议设置为可用CPU核心数的4倍。pm.min_spare_servers 和 pm.max_spare_servers):根据服务器流量模式调整,以平衡资源利用和响应速度。pm.max_requests):设置一个合理的请求数,确保子进程定期重启,释放可能的内存泄漏。sudo apt-get install php-opcache,然后在 php.ini 文件中启用OPcache。zend_extension = opcache.so
opcache.enable = 1
opcache.memory_consumption = 64
opcache.interned_strings_buffer = 8
opcache.max_accelerated_files = 40000
opcache.revalidate_freq = 2
opcache.fast_shutdown = 1
Nginx配置:
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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Apache配置:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
htop、vmstat、iostat 等定期监控服务器的性能指标。taskset 命令将PHP-FPM进程绑定到特定的CPU核心上。通过上述方法,可以有效解决PHP-FPM在Linux上的性能瓶颈问题,提高服务器的整体性能。在实际操作中,需要根据具体的服务器环境和应用程序需求来定制配置。