close
Selinux 下的php-mysql 操作

Selinux 下的php-mysql 操作

今天有朋友問我他的伺服器問題,一個很簡單的 mysql_connect 函式,在權限和安裝都正確的情況下,竟然連不到 mysql 伺服器

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on '192.168.0.4' (13) in /var/www/html/www/cpjhnews.php on line 13

我花費了三個小時查找原因,由於系統是linux centos5.3 新安裝的,程式在其他server 執行沒有什麼問題,但是一到這台伺服器就出狀況了。

初步猜測可能是 mysql 資料庫的權限問題,可是找不到設定錯誤的地方,另外用 console 是可以正常連線的,這就讓我納悶很久。於是我懷疑是 php、httpd、或是php.ini、httpd.conf 等東西沒有裝好?

所以花了三個小時重裝重設....問題依舊。

在經過一天的沉殿後,我一直在想這個問題到底在哪?第二天,我重拾這個問題,重新撰寫了php的程式問題依舊。不過再思考一下,突然靈光一閃:會不會是 selinux 的問題,所以在 runtime 把他關掉:
# setenforce 0

再測試網頁,正常~~~ 果然是 Selinux 的問題,本來怎麼都沒想到?害我研究好久!

難道又要我用外面淺又膚淺又賣得吱吱叫的電腦書面對 Selinux 的那一千零一招:關掉?

如果是這樣,我還買你這本書幹嘛!

好的,那不關掉怎麼做,知道是 Selinux 的問題後,我又將 selinux 給打開了
# setenforce 1  (或 setenforce Enforcing)

取得 selinux 和httpd有關的參數
# getsebool -a | grep 'httpd'
allow_httpd_anon_write --> off
allow_httpd_bugzilla_script_anon_write --> off
allow_httpd_cvs_script_anon_write --> off
allow_httpd_mod_auth_pam --> off
allow_httpd_nagios_script_anon_write --> off
allow_httpd_prewikka_script_anon_write --> off
allow_httpd_squid_script_anon_write --> off
allow_httpd_sys_script_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_network_connect --> off
httpd_can_network_connect_db --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> on
httpd_disable_trans --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> on
httpd_rotatelogs_disable_trans --> off
httpd_ssi_exec --> off
httpd_suexec_disable_trans --> off
httpd_tty_comm --> on
httpd_unified --> on
httpd_use_cifs --> off
httpd_use_nfs --> off

設定 httpd 可以使用網路連接db :
$ setsebool httpd_can_network_connect_db 1

OK

直接設定就生效,不必重新啟動

[和Selinux有關的參考資料]

關於SELINUX的資料很少,尤其是中文的,不過這個人很厲害: Candyz’s Mac, Linux and Free/Open Source Software Blog
http://blog.candyz.org/20060506/1161
http://blog.candyz.org/20060507/1165
http://blog.candyz.org/20060507/1164

http://blog.candyz.org/20060509/1168

完整連結 http://note.tc.edu.tw/427.html

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()