83

When writing web apps in Python, it's brain dead easy to run a development server. Django and Google App Engine (GAE) both ship with simple servers.

The main feature I'm looking for is no configuration. I want something like the GAE dev server where you just pass the directory of the app as a parameter when the server is started.

Is there a reason that this is more difficult with PHP?

mguassa
  • 4,051
  • 2
  • 14
  • 19
mdashx
  • 1,335
  • 2
  • 10
  • 12

8 Answers8

221

Actually PHP 5.4.0 recently presented its built-in web server:

cd ~/public_html
php -S localhost:8000
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
HBv6
  • 3,487
  • 4
  • 30
  • 43
  • 9
    Well, at the time of my answer this didn't exist, but its clearly the better way of solving the problem. – therufa Mar 26 '15 at 11:22
  • 8
    This should be the new accepted answer. – Alphaaa May 22 '15 at 23:13
  • 1
    Totally agree with Alphaa. This is a hell of a lot simpler than installing LAMP / XAMPP. – IcyFlame Aug 07 '15 at 15:54
  • 1
    `-t` to set the document root (relative path) if you want to run this from your project's cwd. For example, if you had a build process that output the public browser code to a `./dist/` folder: `php -S localhost:8000 -t dist` – iono Oct 18 '18 at 05:58
21

If it's a feasible option on whatever platform you're using, PHP 5.4 has a Django-like development server.

You may also want the snippet I use to detect the development server and replicate the two most common uses of mod_rewrite. (Hiding index.php in URLs and passing static files through to be served)

(Yes, there is a built-in command-line switch for that, but it doesn't quite match the mod_rewrite snippet most people use and caused spurious 404 errors for me because of that mismatch.)

ssokolow
  • 14,938
  • 7
  • 52
  • 57
14

Use XAMPP: http://www.apachefriends.org/en/xampp.html Its easy to install and config.

EDIT:
since PHP 5.4 (which was released 2 years after this answer) there's a built-in web-server. Today this is inaccurate, please use the method provided by k0pernikus in his answer.

therufa
  • 2,050
  • 2
  • 25
  • 39
  • 2
    I'd be happier if I didn't need to run Apache, but this looks like the way to do it. Currently, I use Nginx / MySQL, but I only touch it every few months, so I don't feel like screwing around with configuring anything. – mdashx Jun 09 '10 at 21:29
  • 2
    this should not be the accepted answer. php -S is what he wanted and you suggest installing a full featured webserver suite with database and all. – sgohl Aug 23 '17 at 08:30
  • 1
    @roothahn at the time of the answer the latest php version was 5.3.3. That version did not have a built-in web server. I agree with you today, but the dev server did arrive only 2 years after this answer. I have clearly stated this almost 3 years ago in the comments of the highest ranking answer. – therufa Jul 20 '18 at 07:39
8

Try using batch file

  1. Open notepad
  2. php -S localhost:8000
  3. save file as .bat extension => server.bat
  4. now click on server.bat, your server is ready on http://localhost:8000.

DEP

if you got error php not recognize any internal or external command then goto environment variable and edit path to php.exe

"C:\wamp\bin\php\php5.4.3"
Nishchit
  • 18,284
  • 12
  • 54
  • 81
7

Update: I've developed a docker based version that I have started using instead of easy_php_dev: localdev

easy_php_dev makes multi-site php development very easy and fast. Only limitation is it is designed only for OSX. Disclosure: I'm the author.

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
5

When I need a quick php web server for local testing I use QuickPHP

Keeper
  • 3,516
  • 1
  • 18
  • 29
5

We have developed a very small development server which has a similar behavior as django development server.

The software is called sng and work with php and nginx. It is made in javascript and nodejs, you can install it very easily with npm :

npm install sng
fe_lix_
  • 938
  • 11
  • 18
2

AppServ has always setup everything you need nicely!

fire
  • 21,383
  • 17
  • 79
  • 114