Skip to content
Advertisement

PDO connection works from command line, but not through Apache?

I have a very simple test script:

JavaScript

When I execute this script from the command line, it works perfectly:

JavaScript

But when I access the exact same script through my web browser, it says:

JavaScript

I’ve tried var_dump on the error, and the message is: “SQLSTATE[HY000] [2003] Can’t connect to MySQL server on ‘db.example.edu’ (13)”.

This is puzzling. It’s the exact same script on the exact same server — why does it work when I execute it from the command line, but fail when Apache executes it?

Advertisement

Answer

If this is a Red Hat-derived distribution (RHEL, CentOS, Fedora, ScientificLinux) running SELinux (or any non Red Hat derivative using SELinux), the default policy setting at time of this writing is to prohibit Apache from making external connections to other servers or databases. As root, you must enable the following two SELinux booleans. Use the -P option to persist the change across a reboot.

JavaScript

Note that httpd_can_network_connect may not be necessary. Try it first turning on only httpd_can_network_connect_db.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement