在 CentOS 上开启 PHP 的 gzip 压缩,可以通过修改 Apache 或 Nginx 服务器的配置文件来实现。以下是针对 Apache 和 Nginx 的具体步骤:
Apache
-
安装 Apache(如果尚未安装):
sudo yum install httpd -
启动 Apache:
sudo systemctl start httpd -
启用 mod_deflate 模块:
sudo yum install mod_deflate sudo systemctl enable mod_deflate -
编辑 Apache 配置文件:
打开/etc/httpd/conf/httpd.conf或/etc/httpd/conf.d/目录下的相关配置文件(例如default.conf):sudo vi /etc/httpd/conf/httpd.conf -
添加或修改以下配置:
在文件中找到部分,并确保以下配置存在:AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json -
重启 Apache:
sudo systemctl restart httpd
Nginx
-
安装 Nginx(如果尚未安装):
sudo yum install nginx -
启动 Nginx:
sudo systemctl start nginx -
编辑 Nginx 配置文件:
打开/etc/nginx/nginx.conf或/etc/nginx/conf.d/目录下的相关配置文件(例如default.conf):sudo vi /etc/nginx/nginx.conf -
添加或修改以下配置:
在server块中添加以下配置:server { ... gzip on; gzip_types text/html text/plain text/xml text/css application/javascript application/json; ... } -
重启 Nginx:
sudo systemctl restart nginx
验证
你可以通过访问你的网站并使用浏览器的开发者工具(通常按 F12 打开),查看响应头中的 Content-Encoding 字段来验证 gzip 是否已启用。如果显示 gzip,则表示 gzip 压缩已成功开启。
通过以上步骤,你应该能够在 CentOS 上成功开启 PHP 的 gzip 压缩。