I have a Fedora 25 VM running in Hyper-V. This VM is used for a PHP app running on Apache. It is up-to-date and running stock everything.

A simple php script to connect to a remote website fails when running in Apache but works fine from the command line:

$socket = stream_socket_client($host, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, stream_context_create());

if (!$socket) {
    print ("Unable to connect to ${host}: ${errno} ${errstr}\n");
} else {
    print ("Connected to ${host} successfully \n");

The error is

php_network_getaddresses: getaddrinfo failed: Name or service not known

However, the VM has network access and everything works fine from the php command line. This suggests it's an Apache issue. Also: if I restart Apache, the problem goes away.

Thus my question:

How can I force Apache to resolve names properly after a reboot? If I need to reload Apache after every network change, that's acceptable for this installation (it's a dev workstation): how do I trigger that automatically so that it works on boot? Is there a systemd method for doing this?


You don't need to trigger a re-load or a re-start. You simply need to ensure that Apache is not started until /etc/resolv.conf has been written by whatever is doing that at bootstrap. The advice from the PHP bug tracker is to use a systemd override file to add an After= extension naming the relevant service(s) into httpd.service:

cd /etc/systemd/system/
install -m 0755 -d httpd.d
echo >> httpd.d/after.conf "[Unit]"
echo >> httpd.d/after.conf "After=network.service systemd-networkd.service network-online.target"

Further reading

  • I had seen this bug report and tried to set the correct "After=" override but it didn't work. I tried again after seeing your answer which had "[Unit]" in the override file - this fixed the issue. I edited the file using systemctl edit httpd instead of creating it manually. Oct 25 '17 at 15:38

Put an Apache reload script in /etc/NetworkManager/dispatcher.d folder. Source: Run daemon on startup in Fedora 13 after wireless interface is up

OR trigger Apache config reload from Upstart conf.

I mean somthing like

start on net-device-up - but this could be Ubuntu specific.

  • Fedora 25 is a systemd operating system, and the question is tagged systemd.
    – JdeBP
    Oct 25 '17 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.