在Ubuntu上优化PHP-FPM(FastCGI Process Manager)的启动时间可以通过多种方法来实现。以下是一些常见的优化步骤:
编辑PHP-FPM的配置文件 /etc/php/7.x/fpm/pool.d/www.conf
(请根据你的PHP版本调整路径)。
pm = dynamic
:使用动态进程管理模式。pm.max_children = 50
:设置最大子进程数。pm.start_servers = 5
:启动时的服务器进程数。pm.min_spare_servers = 5
:最小空闲服务器进程数。pm.max_spare_servers = 35
:最大空闲服务器进程数。request_terminate_timeout = 0
:禁用请求超时,适用于长时间运行的脚本。确保你的Web服务器(如Nginx或Apache)配置了与PHP-FPM的持久连接。
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.x-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
OPcache可以显著提高PHP脚本的执行速度。
sudo apt-get install php7.x-opcache
编辑 /etc/php/7.x/cli/php.ini
和 /etc/php/7.x/fpm/php.ini
文件,添加或修改以下配置:
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
启用慢日志可以帮助你识别和优化慢查询。
编辑 /etc/php/7.x/fpm/pool.d/www.conf
文件,添加或修改以下配置:
request_slowlog_timeout = 10s
slowlog = /var/log/php-fpm/www-slow.log
如果你使用的是Systemd,可以通过调整服务配置来优化PHP-FPM的启动时间。
sudo nano /etc/systemd/system/php7.x-fpm.service
在 [Service]
部分添加以下参数:
ExecStartPre=/bin/sleep 5
sudo systemctl daemon-reload
sudo systemctl restart php7.x-fpm
使用工具如 htop
、top
、systemd-cgtop
等监控系统资源使用情况,分析PHP-FPM的性能瓶颈。
通过以上步骤,你可以有效地优化Ubuntu上PHP-FPM的启动时间。根据你的具体需求和环境,可能需要进一步调整配置。