在 CentOS 上开启 PHP 的 gzip 压缩,可以通过修改 Apache 或 Nginx 服务器的配置文件来实现。以下是针对 Apache 和 Nginx 的具体步骤:
安装 Apache(如果尚未安装):
sudo yum install httpd
启动 Apache 服务:
sudo systemctl start httpd
启用 mod_deflate 模块:
sudo yum install mod_deflate
sudo systemctl enable httpd
sudo systemctl start httpd
编辑 Apache 配置文件:
打开 /etc/httpd/conf/httpd.conf
文件,或者找到并编辑包含 LoadModule deflate_module modules/mod_deflate.so
的行,确保它没有被注释掉。
添加或修改 gzip 压缩配置: 在配置文件中添加或修改以下内容:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-woff
AddOutputFilterByType DEFLATE application/x-font-woff2
</IfModule>
重启 Apache 服务:
sudo systemctl restart httpd
安装 Nginx(如果尚未安装):
sudo yum install nginx
启动 Nginx 服务:
sudo systemctl start nginx
编辑 Nginx 配置文件:
打开 /etc/nginx/nginx.conf
文件,或者找到并编辑包含 http { ... }
块的文件。
添加或修改 gzip 压缩配置:
在 http
块中添加或修改以下内容:
http {
...
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
application/javascript
application/json
application/x-javascript
text/css
text/javascript
text/plain
text/xml;
...
}
重启 Nginx 服务:
sudo systemctl restart nginx
完成以上步骤后,你的 CentOS 服务器上的 PHP 应用程序将会启用 gzip 压缩,从而提高页面加载速度和减少带宽消耗。