CentOS 6 安裝 Varnish 4.0 後服務無法啟動,經排查為 SELinux 策略未適配新版本導致 VCL 編譯暫存檔寫入被拒。本文提供完整除錯流程與修正方案。

rpm --nosignature -i https://repo.varnish-cache.org/redhat/varnish-4.0.el6.rpm
yum install varnish
service varnish start
# Starting Varnish Cache: [FAILED]
/etc/sysconfig/varnish,在 DAEMON_OPTS 末尾加 -d 啟用除錯模式。/etc/init.d/varnish,移除 start() 中的 > /dev/null 2>&1 以顯示錯誤輸出。Failed to change owner on ./vcl.xxx.c: Operation not permitted
Running VCC-compiler failed, exited with 2
VCL compilation failed
修改 DAEMON_OPTS 將 -u varnish -g varnish 改為 -u root -g root,移除 -d 後可啟動。但以 root 執行 Varnish 風險極高。
參考 Red Hat Bugzilla #1125165:SELinux 策略阻止 varnishd_t 域建立/寫入 VCL 編譯暫存檔。
setenforce 0

vi /etc/selinux/config
# SELINUX=disabled
reboot
# 1. 產生 audit.log 供分析
ausearch -c 'varnishd' --raw | audit2allow -M my-varnish
# 2. 安裝模組
semodule -i my-varnish.pp
此法保持 SELinux 啟用並精準放行所需權限,最符合安全最佳實務。
套用方案後 service varnish start 應顯示 [OK],netstat -ntlp | grep varnish 確認監聽埠正常。