2017年5月18日 星期四

PHP PDO無法存取PostgreSQL在CentOS的問題

作業環境如下-

   CentOS Linux 7.3
   Apache/2.4.6 (CentOS)
   PostgreSQL 9.2.18

一支簡單的PHP程式,連線本機的PostgreSQL資料庫發生錯誤

   ....could not connect to server: Permission denied.... 


Google查到可能是SELinux保護功能阻擋了連線

確認SELinux狀態:

指令: 
   /usr/sbin/sestatus

   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
   Policy MLS status:            enabled
   Policy deny_unknown status:   allowed
   Max kernel policy version:    28



確實SELinux是作用中,不太懂,只好查查設定值:

指令: 
   /usr/sbin/getsebool -a | grep httpd

   httpd_can_network_connect --> off


果然! 預設不允許httpd主動發起連線



修改設定:

指令: 
/usr/sbin/setsebool -P httpd_can_network_connect on


等了大約30秒,shell prompt終於回來了(上面getsebool指令可確認結果)


再測試PHP網頁,成功連線讀取PostgreSQL資料!

ciao~




  
   

沒有留言:

張貼留言