4

I want to create VirtualHosts on Mac OS 10.7 and therefore I edited the /etc/apache2/httpd.conf. I uncommented the line "Include /private/etc/apache2/extra/httpd-vhosts.conf" to include the virtual hosts. In the file /private/etc/apache2/extra/httpd-vhosts.conf I wrote the following:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "/var/www"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/someFolder"
    ServerName myApplication.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/someOhterFolder"
    ServerName myApplication2.dev
</VirtualHost>

There were two example virtual hosts before which I deleted. In my /etc/hosts file I added the following:

127.0.0.1 myApplication.dev
127.0.0.1 myApplication2.dev

I restarted my Apache and typed myApplication.dev and myApplication2.dev in the browser but I get an error "server not found" and it makes www.myApplication.dev in the browser (the same for myApplication2.dev).

Did I forget something to configure? I activated PHP in httpd.conf, mysql is installed also, but that has nothing to do with virtual hosts, I think. Thanks for your help!

skaffman
  • 398,947
  • 96
  • 818
  • 769
tester
  • 3,977
  • 5
  • 39
  • 59

4 Answers4

8

apachectl has an option -S to check vhost.conf file syntax. You can find these lines in vhosts.conf file.

> # You may use the command line option '-S' to verify your virtual host
> # configuration.

So, when you run

sh-3.2# apachectl -S

if you get Syntax OK result it means that there is no problem in your vhosts.conf file.

httpd: VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
         port 80 namevhost zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
         port 80 namevhost yy.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:35)
Syntax OK

If conf file has any problem it will tell you error line(s) like

sh-3.2# apachectl -S
Syntax error on line 33 of /private/etc/apache2/extra/httpd-vhosts.conf:
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" clause (see docs)

make sure that your vhosts.conf file has true configuration.

cyb0k
  • 2,478
  • 23
  • 19
  • Thank you! This helped my fix an issue. Strangely there was no mention of a syntax error in the apache error log, but running the syntax check highlighted some hidden characters that must have come in when I pasted a config someone sent my over iChat. – Paul Maunders Jun 26 '14 at 09:13
  • Despite how easy this is, it solved my issue. I just had a syntax error and "apachectl restart" apparently doesn't say anything about it. – roma Sep 26 '17 at 11:24
6

I had the exact same problem using OS X Lion. I fixed it by adding "::1 myhost.dev" to /etc/hosts:

127.0.0.1 myhost.dev
::1 myhost.dev

Incidentally, the ::1 also fixes a bug that makes page loading very slow on virtual hosts served from the Mac.

orourkedd
  • 6,201
  • 5
  • 43
  • 66
2

Are you using an HTTP proxy? If so, make an exception for myApplication.dev and myApplication2.dev.

What I meant was that the problem "server mot found" means that your browser cannot find the ip adresses of the hosts "myapplication.dev". This may be because you're using an http proxy, possibly one configured by your hosting company. In any case, you don't even reach the server, so you never get to try the virtual host configuration at all.

To just try the virtual host configuration, you can use telnet in a Terminal window and talk HTTP directly to the server, like this:

yourmacbox:~ yourname$ telnet 127.0.0.1 80

You should see the following text:

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Then you type

GET / HTTP/1.0
Host: myApplication.dev

Now, hopefully you should see some response from your web server. This shows that once you can connect to it, the virtual hosts things works.

Jenny D
  • 1,225
  • 9
  • 20
  • 1
    I just have a normal MacBook Pro with the standard configuration from installation. So there are no HTTP proxies configured. Or do you mean something else? – tester May 07 '12 at 18:05
  • I am having this same problem. I was able to use this `Host:` trick with `telnet` to get the expected response. Though I had to type Control-D to finish the request. I am able to `ping` the host, so the entry in `/etc/hosts` is correct, but I still can’t access it in a browser. I don’t think there’s a proxy running, is there any way I could detect that from the command line? – Buck Doyle May 10 '12 at 12:52
  • The proxy doesn't have to be running on your own system - it may be that your internet provider has a setup that makes your web browser use the provider's proxy. You can check that in the Preferences for your browser, usually somewhere in the Network settings. – Jenny D May 10 '12 at 12:56
  • Hmm. It’s happening even when I try it with `lynx`, which I’d be surprised to learn paid attention to the OS proxy settings. But thanks for the `telnet` trick, it at least let me confirm the application is working, even if I can’t access it as I need to. Isn’t it strange that I can`t do `telnet [hostname] 80` though? It’s as if Apache isn’t listening on *, but it seems to be in the configuration… – Buck Doyle May 13 '12 at 01:07
  • If you can't do telnet "hostname", then it's your DNS setup that isn't working, since none of your applications can find the hostname. (Yes, lynx does understand http proxies - if you've one assigned from your ISP, it will end up in your environment variables and lynx will use it.) – Jenny D May 13 '12 at 06:47
2

I had the same problem, and noticed that the ServerRoot "/usr" was set as shown and incorrectly after the 10.7 upgrade. The httpd.conf file was still under /etc/apache2, but this setting in it was pointing to the wrong place. Once I had fixed that by changing to ServerRoot "/etc/apache2", all my previous virtual host configuration got picked up properly.

I also had to re-enable virtual hosts by uncommenting line 477 as mentioned here http://brettterpstra.com/fixing-virtual-hosts-and-web-sharing-in-mountain-lion/ That didn't quite kick in until I had fixed the path issue above.

Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47