近日配置内网网站的 HTTPS 时,通过网上的免费证书服务下载了内网域名的证书文件。
令人疑惑的是,配置完成后,网站依旧无法运行在 HTTPS 下。
查看 Nginx 的错误文件,可以看到 Nginx 报告没有权限读取文件。
cat /var/log/nginx/error.log
2022/08/12 22:19:27 [emerg] 1407#0: cannot load certificate "/root/.acme.sh/example.com/example.com.cer": BIO_new_file() failed (SSL: error:0200100D:system library:fopen:Permission denied:fopen('/root/.acme.sh/example.com/example.com.cer','r') error:2006D002:BIO routines:BIO_new_file:system lib)
但是 ls -l
查看文件权限时,发现文件确实配置了644权限。
这种奇奇怪怪的权限问题,很明显是 SELinux 搞的鬼。
SELinux,全称 Security-Enhanced Linux,为 Linux 提供了更高级的安全控制能力。当进程访问文件的时候,Linux 除了传统的文件权限校验之外,还会进一步检查文件和进程的 Security Context。
首先,通过 sestatus -v
,查看到 SELinux 运行在 enforcing 模式(既权限校验失败,会直接拒绝进程访问)。
[root@home nginx]# sestatus -v
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
在”正经“的服务器上,你需要老老实实修正文件或者进程的 Security Context。不过如果是在安全性不太重要的场景,可以把 SELinux 调整到 permissive 模式。
setenforce 0
permissive 模式下,SELinux 会做安全校验,但是不会拒绝访问操作。违规的访问会被写入到 audit 日志。