10

This question already has an answer here:

I have Apache 2.4 running on Mac OS X. apachectl configtest give me: Syntax OK.

I have two virtual hosts set up, one called localhost, one called test.dev.

<VirtualHost *:80>
    DocumentRoot "/Users/psychomachine/Development/_localhost"
    ServerName localhost
    ServerAlias www.localhost
    <Directory />
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/psychomachine/Development/test"
    ServerName test.dev
    ServerAlias www.test.dev
    <Directory />
        Require all granted
    </Directory>
</VirtualHost>

localhost just works:

↪ curl -I -L localhost                                                                                                                                                              15:51:08
HTTP/1.1 200 OK
Date: Tue, 08 Dec 2015 14:51:17 GMT
Server: Apache/2.4.16 (Unix)
Last-Modified: Tue, 08 Dec 2015 08:52:04 GMT
ETag: "c-5265f1673f500"
Accept-Ranges: bytes
Content-Length: 12
Content-Type: text/html

whereas test.dev doesn't:

↪ curl -I -L test.dev                                                                                                                                                               15:51:01
curl: (7) Failed to connect to test.dev port 80: Connection refused

My hosts file has an entry for test.dev, which is why I can ping test.dev and hear back from 127.0.0.1.

↪ ping test.dev                                                                                                                                                                     15:53:39
PING test.dev (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.069 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.096 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.111 ms
^C
--- test.dev ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.069/0.092/0.111/0.017 ms

I've spent hours looking at this, but I still don't understand how it is possible to ping test.dev but still get connection refused. Apache is listening on port 80, and I am not getting a page forbidden kind of error. There is nothing in Apache logs for test.dev, because the request for test.dev never makes it to Apache.

I am missing a crucial element of the puzzle — and I hope somebody will be able to point me in the right direction.

Many thanks in advance.

Edit: As I said, Apache is listening on 80:

sudo lsof -i ':80'                                                                                                                                                                16:54:46
COMMAND  PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
httpd   2464   root    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)
httpd   2466 daemon    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)
httpd   2467 daemon    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)
httpd   2468 daemon    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)
httpd   2469 daemon    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)
httpd   2470 daemon    5u  IPv6 0x8883a2a43af0ca7f      0t0  TCP *:http (LISTEN)

Additional info

scutil -r test.dev                                                    08:25:59
Reachable 

telnet test.dev                                                       08:26:17
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

telnet test.dev 80                                                    08:26:43
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host

127.0.0.1 test.dev is in /etc/hosts and Listen *:80 is in apache's conf.

marked as duplicate by Iain, Michael Hampton Dec 24 '15 at 14:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from stackoverflow.com Dec 24 '15 at 9:09

This question came from our site for professional and enthusiast programmers.

  • Please ping localhost and post the output. It should obviously output IP 127.0.0.1, but just to be sure. – Inigo Flores Dec 11 '15 at 9:42
  • It does. PING localhost (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.076 ms 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.105 ms ^C --- localhost ping statistics --- 2 packets transmitted, 2 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.076/0.090/0.105/0.014 ms – Tench Dec 11 '15 at 13:10
  • Are you getting any warnings when restarting Apache through the CLI? Have you checked the logs for any suspicious warnings? – Inigo Flores Dec 11 '15 at 13:54
4

Check you httpd.conf to see if you have the following:

Listen 127.0.0.1:80

You have to replace it with:

Listen *:80

By doing this, Apache will bind to all of the computer's interfaces, not just the loopback interface.

Don't forget to restart Apache after editing httpd.conf.

  • 1
    thanks, but that's exactly it — I do have *:80 in httpd.conf. – Tench Dec 11 '15 at 5:58
4

Even though Apache was listening to port 80, this port was refusing connection. In the end this is what worked for me:

I made sure port 80 was open for TCP on all interfaces, which on OSX you do by adding

pass in proto tcp from any to any port 80

to /etc/pf.conf. Reloading pfctl didn't quite do the trick, but a reboot did. Now all my virtual hosts are accessible. And the world is in order again.

1

Try to connect test.dev with another available port fot http or https communication. OR stop the previous PID with port ':80' and then run. Check the process ID with -

netstat -ltnp | grep ':80'

kill -9

  • thanks, but killing processes or changing the port doesn't make a difference. – Tench Dec 8 '15 at 15:59
  • may be you cannot access two different processes with same port. – Mohit Rohilla Dec 8 '15 at 16:20
  • try with another and tell me what excatly you want to do – Mohit Rohilla Dec 8 '15 at 16:21
  • ok. i had apache listen to :80 only. after adding listen 8080 in httpd.conf (so that apache listens to both 80 and 8080) and changing vhosts.conf for test.dev to listen to 8080, i could access localhost on 80 and test.dev on 8080. but my goal is to access both virtual hosts on 80. – Tench Dec 8 '15 at 17:20

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