Conexão PHP com o PostgreSQL (e o MySQL) no CentOS 7

Após a instalação do PHP, PostgreSQL e MySQL no CentOS 7, ao tentar fazer um simples teste de conexão do PHP com o PostgreSQL, estava recebendo a seguinte mensagem de erro:

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host “localhost” (127.0.0.1) and accepting TCP/IP connections on port 5432?

O código de teste foi o seguinte:

<?php

 $casa = "localhost";
 $banco = "nome_do_banco";
 $porta = 5432;
 $usuario = "nome_do_usuario";
 $senha = "senha_do_usuario";

 $conexao = pg_connect("host=$casa dbname=$banco port=$porta user=$usuario password=$senha");

 if (! $conexao ) {
 print "<h3>Não foi possível conectar ao PostgreSQL.</h3><br />";
 pg_errormessage();
 } else {
 pg_close($conexao);
 print "<h3>Conexão OK.</h3>";
 }

?>

Eu já tinha ajustado as configurações do pg_hba.conf e postgresql.conf e, mesmo assim, o PHP não conseguia estabelecer uma conexão com o PostgreSQL. Por segurança, chequei as regras do firewall e estavam todas corretas também.

Aí me lembrei: o SELinux! Será que o SELinux estaria bloqueando por padrão as conexões ao banco de dados? Bem, o comando abaixo (como root) retorna as configurações do SELinux para os servidores web:

# getsebool -a | grep httpd
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off

Pronto, identificamos o problema! A propriedade httpd_can_network_connect_db está em “off” (e a httpd_can_network_connect também) e o SELinux está impedindo a conexão do PHP com o PostgreSQL. Para permitir que o SELinux deixe o PHP se conectar ao PostgreSQL (e ao MySQL), faça o seguinte (como root):

setsebool -P httpd_can_network_connect_db on
setsebool -P httpd_can_network_connect on

Agora o Apache/PHP já consegue se conectar aos bancos de dados!

351 thoughts on “Conexão PHP com o PostgreSQL (e o MySQL) no CentOS 7”

  1. Pingback: adam eve deal
  2. Pingback: adam and eve code
  3. Pingback: Brazilian Hair
  4. Pingback: porn movie
  5. Pingback: porn movie
  6. Pingback: hacking services
  7. Pingback: prediksi bola
  8. Pingback: vibrator
  9. Pingback: Lovehoney
  10. Pingback: the bullet toy
  11. Pingback: kona coffee beans
  12. Pingback: polidomes
  13. Pingback: Email Archiving
  14. Pingback: vibrator
  15. Pingback: best kona coffee
  16. Pingback: lion coffee
  17. Pingback: kegel sex exercise
  18. Pingback: realistic dildo
  19. Pingback: oral sex kit
  20. Pingback: mini hitachi wand
  21. Pingback: mini vibrator sex
  22. Pingback: development
  23. Pingback: adidas官網
  24. Pingback: vibe vibrator
  25. Pingback: Xvideo
  26. Pingback: loja de bebe
  27. Pingback: stockings
  28. Pingback: icicles toys
  29. Pingback: sex toy review
  30. Pingback: Strapons
  31. Pingback: new cryptocurrency
  32. Pingback: kona coffee
  33. Pingback: MSP airport Limo
  34. Pingback: cock ring
  35. Pingback: seo marketing
  36. Pingback: grip tape
  37. Pingback: adult sex toys
  38. Pingback: bullet vibrators
  39. Pingback: top best sex toys
  40. Pingback: Sripatum
  41. Pingback: g spot sex toy
  42. Pingback: sex lube for women
  43. Pingback: RMUTT
  44. Pingback: bondage sex
  45. Pingback: nipple vacuum
  46. Pingback: senegal qatar
  47. Pingback: how to make money
  48. Pingback: Ripoff Report
  49. Pingback: SEO 2018
  50. Pingback: bead vibrator
  51. Pingback: Emperor 7
  52. Pingback: auto injuries
  53. Pingback: really big dildo
  54. Pingback: pc app
  55. Pingback: anal sex vibrator
  56. Pingback: buy and sell
  57. Pingback: sex movie
  58. Pingback: leather strap on
  59. Pingback: mumbai police
  60. Pingback: best vibrator
  61. Pingback: Best Anal Plug
  62. Pingback: clit tickler
  63. Pingback: best kona coffee
  64. Pingback: wand massagers
  65. Pingback: Orlando SEO agency
  66. Pingback: Cracked phone
  67. Pingback: Top dildos
  68. Pingback: vibrating ring use
  69. Pingback: anal vibrator
  70. Pingback: clitoral vibrator
  71. Pingback: Page
  72. Pingback: rhalyns
  73. Pingback: lube for anal sex
  74. Pingback: Penis Extension
  75. Pingback: Less Of Tour
  76. Pingback: water damage
  77. Pingback: vibe rabbit
  78. Pingback: خرید هاست
  79. Pingback: 海美迪
  80. Pingback: 美容外科
  81. Pingback: 100% kona coffee
  82. Pingback: womens clothes
  83. Pingback: his explanation
  84. Pingback: Homepage
  85. Pingback: forex promotions
  86. Pingback: Hariyanvi Song
  87. Pingback: 90's hits
  88. Pingback: 90's R&B
  89. Pingback: Buy Jackets Online
  90. Pingback: خرید vpn
  91. Pingback: Kona Co
  92. Pingback: 100% K coffee
  93. Pingback: vegas packages
  94. Pingback: خدمات سئو
  95. Pingback: best coffee beans
  96. Pingback: خرید vpn
  97. Pingback: خرید vpn
  98. Pingback: 100 kona coffee
  99. Pingback: china logistics
  100. Pingback: coffee machines
  101. Pingback: خرید vpn
  102. Pingback: خرید کریو
  103. Pingback: here
  104. Pingback: خرید vpn
  105. Pingback: persian rugs
  106. Pingback: خرید vpn
  107. Pingback: tam
  108. Pingback: خرید vpn
  109. Pingback: خرید vpn
  110. Pingback: Rbxtrading
  111. Pingback: amazon all offers
  112. Pingback: möbler
  113. Pingback: termeh
  114. Pingback: free games to play
  115. Pingback: گوگل adwords
  116. Pingback: Belize inn
  117. Pingback: گوگل adwords
  118. Pingback: گوگل adwords
  119. Pingback: 脱毛
  120. Pingback: vpn
  121. Pingback: گوگل adwords
  122. Pingback: ile maurice design
  123. Pingback: gymshark
  124. Pingback: گوگل adwords
  125. Pingback: novin marketing
  126. Pingback: persian tar
  127. Pingback: resume
  128. Pingback: گوگل adwords
  129. Pingback: خرید vpn
  130. Pingback: سیستم cctv
  131. Pingback: خرید vpn
  132. Pingback: خرید vpn
  133. Pingback: سیستم cctv
  134. Pingback: خرید vpn
  135. Pingback: سیستم cctv
  136. Pingback: zand game
  137. Pingback: خرید vpn
  138. Pingback: سیستم cctv
  139. Pingback: خرید طلا
  140. Pingback: Global Geeks
  141. Pingback: خرید طلا
  142. Pingback: سیستم cctv
  143. Pingback: خرید vpn
  144. Pingback: 100 kona coffee
  145. Pingback: خرید vpn
  146. Pingback: gratis inserieren
  147. Pingback: nipple play
  148. Pingback: سیستم cctv
  149. Pingback: خرید vpn
  150. Pingback: hp printer drivers
  151. Pingback: kinky sex toy

Leave a Reply