利用Ubuntu PHP-FPM提高网站速度可以通过以下几个步骤来实现:
sudo apt update
sudo apt install php-fpm php-mysql
/etc/php/{version}/fpm/php.ini,根据需要进行自定义配置。例如:pm.max_children = 50 # 提高进程池的大小
pm.start_servers = 20 # 启动时初始的进程数量
pm.min_spare_servers = 10 # 最小空闲进程数量
pm.max_spare_servers = 30 # 最大空闲进程数量
pm.max_requests = 500 # 单个进程处理的最大请求数量
/etc/nginx/sites-available/{your-site},添加以下内容:server {
listen 80;
server_name your-domain.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; # 根据你的 PHP 版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后,重新加载Nginx配置:sudo nginx -t
sudo systemctl reload nginx
mod_proxy_fcgi 和 mod_fastcgi 模块,并在站点配置文件中添加以下内容:<VirtualHost *:80>
ServerName your-domain.com
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:fcgi://unix:/run/php/php7.4-fpm.sock" # 根据你的 PHP 版本调整
</FilesMatch>
</VirtualHost>
然后,重新加载 Apache 配置:sudo systemctl reload apache2
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 0
opcache.fast_shutdown = 1
然后重启PHP-FPM服务。ps aux | grep php-fpm | wc -l
pm.max_children、pm.start_servers、pm.min_spare_servers 和 pm.max_spare_servers 等参数。sudo apt-get install varnish
配置Varnish:
编辑Varnish的配置文件(通常位于 /etc/varnish/default.vcl),设置缓存大小、后端服务器地址等。
启动Varnish:sudo systemctl start varnish
sudo systemctl enable varnish
配置Web服务器将Web服务器的监听端口更改为Varnish的监听端口。通过以上步骤,可以显著提高Ubuntu上使用PHP-FPM的网站速度和性能。记得在修改任何配置之前备份现有文件,并在生产环境中进行充分的测试。