RHEL SELinux阻止Apache访问PostgreSQL

我正在运行使用PostgreSQL的Django应用程序。 服务器正在使用SELinux运行RHEL 6.5。 我遇到了Django应用程序无法连接到数据库的问题,我想这是因为SELinux阻止了它。 这是我在Django中看到的错误:

could not connect to server: Permission denied Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

我怎样才能解决这个问题? 我遇到这个post ,但我不知道如何应用解决scheme( chcon -t postgresql_exec_t /path/to/pgbouncer )到我的问题。

谢谢!

[编辑]
当我尝试访问网站时,以下是/var/log/audit/audit.log样子:

 type=AVC msg=audit(1396289984.549:9245): avc: denied { write } for pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file type=SYSCALL msg=audit(1396289984.549:9245): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625273aa0 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null) type=AVC msg=audit(1396289984.756:9246): avc: denied { write } for pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file type=SYSCALL msg=audit(1396289984.756:9246): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe624d87890 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null) type=AVC msg=audit(1396289984.757:9247): avc: denied { write } for pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file type=SYSCALL msg=audit(1396289984.757:9247): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625342c20 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null) type=AVC msg=audit(1396289984.758:9248): avc: denied { write } for pid=16975 comm="httpd" name=".s.PGSQL.5432" dev=sda1 ino=2359354 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:tmp_t:s0 tclass=sock_file type=SYSCALL msg=audit(1396289984.758:9248): arch=c000003e syscall=42 success=no exit=-13 a0=10 a1=7fe625603ac0 a2=6e a3=0 items=0 ppid=16943 pid=16975 auid=22383 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1213 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null) 

[EDIT2]

这里有一些我已经启用的相关SELinux选项。

-bash-4.1$ sudo getsebool -a | grep httpd_can_network_connect_db httpd_can_network_connect_db --> on -bash-4.1$ sudo getsebool -a | grep allow_user_postgresql_connect allow_user_postgresql_connect --> on

对于未来的读者,对我来说,只要设置bool允许httpd进行数据库连接就足够了; 即:

 setsebool -P httpd_can_network_connect_db 1 

您可以通过以下方式检查/validation设置:

 getsebool httpd_can_network_connect_db 

这应该返回'… => on'

之后,如果你尾巴-f /var/log/audit/audit.log并重新尝试你的操作,它应该工作。

好的,在系统pipe理员的帮助下,问题现在已经解决了。 事实certificate,分配给/usr/pgsql-9.3/bin中的二进制文件的SELinux上下文是错误的。 所有解决这个问题的方法是chcon -t postgresql_exec_t /usr/pgsql-9.3/bin 。 要更改符号链接的上下文,只需添加-h