Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find available port for PHP server

Tags:

php

PHP 5.4 comes with a built-in server for development purposes. This is the kind of thing I've been waiting for months, because up until now I've had to sort of hack together a PHP script that listens for incoming connections and handles them (because I don't want to go to the trouble and overhead of installing an actual server).

The main thing left for me to worry about is: how can I have a port assigned?

In my PHP script, I used to do this:

socket_bind($sock,"localhost",0) or die("Could not bind socket");
socket_getsockname($sock,$ip,$port);

$port would then be the port number assigned by the OS based on what is available.

I was just wondering if any such feature existed in PHP's built-in server and, if so, what the command line should be to access it.

like image 234
Niet the Dark Absol Avatar asked Mar 26 '12 14:03

Niet the Dark Absol


1 Answers

From the PHP docs:

Example #1 Starting the web server

 $ cd ~/public_html
 $ php -S localhost:8000

There you have it - the server is running on port 8000.

like image 149
yrosen Avatar answered Sep 20 '22 15:09

yrosen