在 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 mod_deflate
編輯 Apache 配置文件:
打開 /etc/httpd/conf/httpd.conf 或 /etc/httpd/conf.d/ 目錄下的相關配置文件(例如 default.conf):
sudo vi /etc/httpd/conf/httpd.conf
添加或修改以下配置:
在文件中找到 <IfModule mod_deflate.c> 部分,并確保以下配置存在:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
</IfModule>
重啟 Apache:
sudo systemctl restart httpd
安裝 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 壓縮。