Why would apache refuse connection to localhost 127.0.0.1 on OSX? - macos

When trying to access sites on my localhost the connection is refused. Two days ago the set up was working without issues with multiple virtual hosts configured. I'm not aware of any changes that could have affected the set up. I spent all day yesterday trying to troubleshoot the issue but have been going around in circles.
OS: OSX 10.11.16
httpd -V returns this:
Server version: Apache/2.4.18 (Unix)
Server built: Feb 20 2016 20:03:19
Server's Module Magic Number: 20120211:52
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
httpd.conf is configured to allow virtual hosts and nothing has changed in httpd-vhosts.conf file.
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
...
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
apachectl configtest returns:
Syntax OK
I've tried running a port scan for 127.0.0.1 and http port 80 does not show. This and the connection being refused makes me think this is where the issue is but I don't know why. The OSX firewall is turned off. I've tried the solution posted here but it did not fix it.
My /etc/hosts file looks like this:
#
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
#
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 site.local
127.0.0.1 othersite.local
...
I can ping 127.0.0.1. I previously had homebrew installed to run different PHP versions but I've removed that to try and bring the system back to stock. I really don't know what to try next, any help would be really appreciated.

It happened to me while upgrading php. Below steps worked for me to bring me back on track.
Generally, mac creates a back-up before upgrading. Hence, we'll be using the pre-update version of httpd.conf
cd /etc/apache2/
sudo mv httpd.conf httpd.conf-afterupdate
sudo mv httpd.conf.pre-update httpd.conf
sudo apachectl configtest
sudo apachectl restart

You could check to ensure you have Listen: 80 in your /usr/local/etc/apache2/2.4/httpd.conf configuration file.

After several days of trying to debug this I resolved it by overwriting my httpd.conf file with an older one that was created when upgrading to osx elcapitan.
sudo cp httpd.conf~elcapitan httpd.conf
After doing this localhost was accessible again. I don't know what was wrong with my previous httpd.conf file. I'd been through it many times looking for issues and never found any. I even diffed the two files to try and see where the problem was and found no reason why it would fail in the way it was.
Once I had localhost responding again I went through the process of enabling the modules I require and reconfiguring my virtual hosts.
Again, I don't know what was wrong with the other httpd.conf file. Perhaps it was corrupted in some way. Regardless it was failing silently with apachectl configtest not reporting any problems.
So if others have a similar issue it may be worth reverting back to an older httpd.conf file. OSX usually creates a backup when upgrades are done.

In my case DocumentRoot in the httpd.conf file was wrong.
I figured it out after typing httpd in the terminal. Try the same maybe it can give you some hints to solve the problem.
➜ ~ httpd
AH00526: Syntax error on line 255 of /usr/local/etc/httpd/httpd.conf:
DocumentRoot '/Users/xx/Desktop/sites' is not a directory, or is not readable

Related

MacOS doesn't resolve localhost to 127.0.0.1 (ignores hosts / apache vhosts)

I ran into the problem, that localhost redirects to a specific domain instead of 127.0.0.1.
My configuration is a local apache/mysql/php installation on MacOS Mojave using vhosts for different projects that are configured in hosts to redirect properly.
When I enter 127.0.0.1 in any browser, I get to my overview page lying in the www root. The same should happen, when I enter localhost in the browser, but instead it redirects to a domain of a project I was working on in the past. It happens also without network connection, so the configuration is on this machine.
My /etc/hosts file:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost project1.local project2.local
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
The first line seems to be ignored and localhost doesn't resolve to 127.0.0.1.
I only have two vhost configurations plus default configuration with no wrong redirection and also the httpd.conf lookes fine. I searched the whole machine with grep -lr "redirectiondomain.com" * without results. I tried with apache switched off with the same result and looked up the MacOS network settings but I can't find anything related to the wrong redirection neither any other solution on stackoverflow or google that solves my issue. Has anybody a clue how the wrong redirection of localhost could be set somewhere else?
Edit: My hostname in MacOS is set. I tried localhost without port, with :80 and :8080

After upgrade to macOS Sierra Apache can't do start/stop/restart

After upgrade to macOS Sierra Apache can't do start/stop/restart.
Errors:
AH00557: httpd: apr_sockaddr_info_get() failed for MacBook-Pro-N.local
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
I tried reinstall apache24 using home-brew. But it didn't fix my problem.
Also I configured httpd.conf, uncommented ServerName localhost. But it didn't help anyway.
Please, help.
try adding
127.0.0.1 MacBook-Pro-N.local
to your /etc/hosts file
Another solution is to change ServerName to localhost.
So first you need to find the location of your httpd.conf Apache configuration file by:
apachectl -t -D DUMP_INCLUDES
then edit it and uncomment or change the line with ServerName (make sure it has the valid server name or it exists in your /etc/hosts file). E.g.
ServerName localhost

NGINX not serving from localhost but is serving from 127.0.0.1

I've set up a new macbook pro with El Capitan, I used homebrew to install everything. Also using brew services to start and stop launchctl.
I'm not sure what the problem is, I have it working on my iMac and did the same thing on the MBP, the config file is untouched, so I should be getting the default welcome screen on localhost, server_name is set to localhost
Any suggestions?
So the fix was adding [::]:8080 to the nginx.conf file
I used sudo lsof -i TCP -Pn | grep nginx and saw it
was listening for ipv6 like #ZYWJ was hinting at.
So your nginx.conf file should look something like this:
server {
listen [::]:8080;
server_name localhost;
}
I installed with homebrew
I hope this helps anyone with the same problem.

Mac localhost is not available after upgrade to OSX El Capitan 10.11.1

From httpd -v, I get the below:
Server version: Apache/2.4.16 (Unix)
Server built: Aug 22 2015 16:51:57
Also, etc/hosts is as follows:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
I start the server using sudo apachectl start and it returns no error. However, I am not able to connect to localhost on a web browser. Would you suggest a solution? Thanks for your time.
Please see if https://donatstudios.com/PHP-in-OS-X-Yosemite helps you re-configure the httpd.conf
If you don't need IPv6 there is an easy fix for this.
I was all of a sudden unable to access localhost:631 (CUPS).
I Changed IPv6 to link-local.
Problem solved.

DNSMasq Not Resolving Addresses on Mac Local Web Development Environment

I've setup my local web development environment according to -http://mallinson.ca/osx-web-development/. I have done this before, and I got on a new computer, and am trying to do it again. I'm running Apache 2.4.9 on Mac OS X Yosemite 10.10.2 and directing the DNS with dnsmasq.
When I try to go to home.dev, it received this error:
Error code: ERR_NAME_NOT_RESOLVED
When I run scutil --dns, it seems to resolve it correctly as:
resolver #3
domain : dev
nameserver[0] : 127.0.0.1
flags : Request A records, Request AAAA records
reach : Reachable,Local Address
However, when I:
ping -c 1 home.dev
I get
ping: cannot resolve home.dev: Unknown host
Here is my reference to it in vhosts:
<VirtualHost *:80>
VirtualDocumentRoot "/Volumes/Work/Home/www"
ServerName home.dev
UseCanonicalName Off
</Virtualhost>
I think it was a change from Maverick's to Yosemite. In Yosemite, I had to add "port 35353" to the /etc/resolver files as well as as the config file for DNSmasq. I know for a fact I didn't originally do that, and it originally worked, so there was a change somewhere in there that broke it. Possibly also from a newer version of DNSmasq.
To make this work, I had to add a 127.0.0.1 DNS entry in my Network Preferences.

Resources