在 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 壓縮,從而提高頁面加載速度和減少帶寬消耗。