Getting php mysqli_connect to work under httpd

Documenting this because I know I’m going to forget it.

I was playing around with some older PHP code in preparation for a new project and tried setting it up on Fedora 20.

Part of the code makes a connection to a MariaDB database and I kept getting a HTTP 500 error when I ran the code. If I ran it from the command line, it worked without a problem.

The error I was seeing was:

PHP Fatal error:  Uncaught mysqli_sql_exception: Permission denied in /var/www/html/w/php/_database.php:5

It turns out that I’d forgotten selinux kicks in and prevents httpd from making outbound network connections. It’s actually logged in /var/log/messages:

Mar 20 17:26:48 vmfedora setroubleshoot[314730]: SELinux is preventing php-fpm from name_connect access on the tcp_socket port 3306.

This can be remedied, if you still want selinux enabled, by doing the following:

setsebool -P httpd_can_network_connect 1

I restarted httpd for good measure and now it’s working.

Leave a Reply

Your email address will not be published. Required fields are marked *