29

Yes, there's a built-in web server in the upcoming release of PHP 5.4 which you can try out in their release candidates (I found about this just recently too!)

http://php.net/manual/en/features.commandline.webserver.php

What I need help figuring out is, is there any way to make it run on domain names other than localhost (it's running fine on localhost, port 80)? Even 127.0.0.1 doesn't work. I've put in dummy hostnames in my hosts file to point to 127.0.0.1 and they don't work too. I understand that it's just a release candidate, but I would like to know whether anyone else has already come up with a solution for this issue so that I can test my app with the actual domain name pointing to 127.0.0.1 in my hosts file. OS is Windows 7 Professional SP1.

Things I've already tried: 1. Googling (duh) 2. Looking through php.ini for options 3. Trying out 127.0.0.1, my LAN IP, my WAN IP with port 80 forwarded and NAT loopback issue fixed (router running DD-WRT)

hakre
  • 193,403
  • 52
  • 435
  • 836
Shanshui
  • 684
  • 1
  • 5
  • 12
  • If it's a *web server* than you're better off at SuperUser or ServerFault, this website would be geared towards *actual programming with PHP*. – animuson Dec 04 '11 at 17:42
  • I'm not exactly sure whether it belongs, since this is something built into PHP and probably got something to do with the configuration of PHP itself. Thanks for the suggestion anyway! – Shanshui Dec 04 '11 at 17:47
  • I gave it some thought, and I think you're right. I just flagged my own question to have it moved to ServerFault. Hopefully a moderator would come over soon. – Shanshui Dec 04 '11 at 17:52
  • 5
    It does not belong on SF, since this isn't a real webserver. It's intended as development tool only. Why would you want to run it on other domains than localhost? Also what exactly have you tried? `php -S www.example.localnet:9000` runs perfectly fine. It ignores the actual `Host:` request header even. Which version did you try? (Beta versions might be unsuitable for bug complaints.) – mario Dec 04 '11 at 17:53
  • Mario, I think you just solved my issue. I created a shortcut for the PHP executable and forgot that I've been running php -S localhost:80. Can you write your comment as an answer so that I can mark it as the correct answer? (Just to add, it's PHP 5.4 RC2) – Shanshui Dec 04 '11 at 17:57
  • 8
    Mods need to chill out on the closes… This is a perfectly legitimate question. – Alex Gray Jan 08 '12 at 17:41

1 Answers1

75

I did these tests on a Windows XP system, but should work the same on Linux as well by modifying the commands.

Run your PHP test server like this:

C:/php/php.exe -S 0.0.0.0:80
or
/usr/bin/php -S 0.0.0.0:80

0.0.0.0 will bind to all available IP addresses on the system.

On another machine on the network, I configured the hosts file to point to the internal IP of the system running PHP using a custom domain. This is not 127.0.0.1 as that refers to the local host, in my case I pointed my main PC to 192.168.88.247 which was the XP machine running PHP. Note the firewall should be disabled or set to allow traffic on port 80 on the machine running php.

I configured my router to port forward traffic from external port 80 to 192.168.88.247:80. Then using a hosts file on a PC from an external network, I configured the fake domain to point to my WAN IP. I was able to access the PHP web server externally.

That said, it is just a server for testing, so there may be unknown security risks opening it up to the outside world.

starball
  • 20,030
  • 7
  • 43
  • 238
drew010
  • 68,777
  • 11
  • 134
  • 162
  • 1
    Thanksfor the 0.0.0.0 tip! too... ;-) – ZEE Oct 14 '14 at 16:30
  • how to make it accept connections only from pre-specified hosts? – Ahmad Hajjar Nov 11 '15 at 09:11
  • 1
    @AhmadHajjar If you want the PHP web server to only accept connections from certain hosts you will need to configure a firewall on the local computer (or router if serving externally using NAT) since the built-in server doesn't have any type of access control. While not as safe, I suppose you could configure the router script to look at the remote address and compare it against a list of allowed hosts and terminate the request if it is outside the list. – drew010 Nov 11 '15 at 17:14
  • love it, this way it is possible to use it inside docker – FantomX1 Sep 15 '18 at 16:44
  • Had a similar problem with JavaFX. It would say "connection" refused despite using `http://localhost` in the URL. I'm not sure why the connection would be refused by JavaFX when it's at `localhost`, but `0.0.0.0` fixed it, thanks! – tresf Oct 06 '19 at 05:47