在 CentOS 下,要为 PHP 启用 gzip 压缩,你需要按照以下步骤操作:
确保已经安装了 Apache 或 Nginx 服务器。CentOS 默认使用 Apache,如果你使用的是 Nginx,请跳到步骤 3。
安装 mod_deflate 模块(如果尚未安装):
sudo yum install mod_deflate
sudo systemctl enable httpd
sudo systemctl start httpd
sudo systemctl status httpd
/etc/httpd/conf/httpd.conf
或 /etc/apache2/apache2.conf
(取决于你的 CentOS 版本):sudo vi /etc/httpd/conf/httpd.conf
<IfModule mod_deflate.c>
部分,如果没有找到,请在文件的适当位置添加。然后,确保以下代码已添加到配置文件中:AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/x-javascript
保存并关闭配置文件。
重启 Apache 服务器以使更改生效:
sudo systemctl restart httpd
现在,你的 CentOS 服务器上的 PHP 应用程序应该已经启用了 gzip 压缩。你可以通过访问 http://your_server_ip_or_domain
并查看响应头来验证是否已启用 gzip 压缩。在浏览器中,你可以使用开发者工具(通常通过按 F12 打开)来查看响应头。在 Network 选项卡中,找到一个资源(如 .html 或 .js 文件),然后查看 Headers 部分。如果已启用 gzip 压缩,你应该会看到 Content-Encoding: gzip
响应头。