A Built-in Web Server For PHP
wiki.php.net
wiki.php.net
Any idiot can write a snarky comment about PHP. Very few get to write code that has anywhere near the impact it had.
The problem this creates is that once a developer has it running on their own machine inside Tomcat (or now this PHP built-in webserver) the app is probably coded in such a way that it will ONLY run inside this server, and will never scale. What's more, they often turn it into a production application with real customers depending on it, and blame you if you can't make it scale to handle hundreds or thousands of users.
This is the problem with using "built in" webservers. It encourages developer laziness, and encourages people to kick bad code over the fence to the operations team who has to make it reliable and run in production. Never mind that a built in PHP webserver will never scale to meet the needs of more than a few users.
The problem is, web developers already have a lot on their plate.
LAMP. The operating system, the web server, the database, and the scripting language. Plus HTML, Javascript, and CSS. And all the frameworks that "simplify" making these work together nicely. Then they have to figure out how to deploy on the test and prod servers. Oh, and they better back everything up. And some redundancy would be good.
That's on top of the existing requirements:
- VCS - Bug tracking - Documentation - Project management (Scrum? PMI? Flying-by-the-seat-of-their-pants?) - UX / UI design - Unit tests
Not to mention having some domain knowledge. The above is fine for a to-do list, but they might also need to know how to solve "real" problems, not just do IT.
I used to think so too, but I changed my mind a few years ago. Have diversity in the environment in the phases leading up to staging/testing leads to many problems being uncovered early - hardcoded paths, platform assumptions, potential performance issues that only show under some circumstances, ...
I think it improves the code if various devs use different installation paths, DB's, development tools and even OS's. I've seen many deployment issues that would have trivially been detected if devs had different environments.
Isn't this exactly what developers are currently doing? A lot of projects are heavily relying on .htaccess files and having the webserver do redirects/pretty URLs for them. They're putting part of the application logic in the webserver...
And I think that lots of people (myself included) already see PHP as a pretty bloated system; throwing an entire web server into what is a scripting language won't do much to keep it lean.
Edit: instead of downvoting, can you explain how this isn't already a trivially solved problem and why including an entire custom web server with what is a scripting language isn't bloat? Installing Apache really is that easy, even in Windows, if we're just talking about a dev machine.
Being able to launch a quick server to do testing is absolutely fantastic. I use poole.py for my site (to create a static portfolio website) and I use its built in web server to view my changes before committing them and pushing them live.
If PHP development became as simple as launching a quick web server just like in Python or Ruby I might once again take an interest in it, at the moment I am just not too interested in working on it, as either I have to set up and run Apache with mod_php on my local machine or I need to set it up on a remote machine, and then files are no longer local, and testing becomes a deploy/test cycle instead, or I am editing files remotely over a possibly slow SSH session, or having to FTP them on save.
No, a built in web server in PHP that does the handling of requests would be absolutely fantastic. Having a simple web server doesn't have to add much bloat at all, it isn't bloated in Python or Ruby either. Look at publicfile [1] for an extremely small web server, look at the examples in libevent [2]. A web server built into PHP only has to do the bare minimum, serve requests. It doesn't need all of the functionality that Apache has. It isn't meant to be used in production.
[1]: http://cr.yp.to/publicfile.html
[2]: http://monkey.org/~provos/libevent/ see [3]
[3]: http://monkey.org/~provos/libevent/doxygen-2.0.1/ (Event Driven HTTP servers)
In fact that's part of the reason why PHP became so popular: 1) it's free, 2) it's easy to do basic html manipulation, and 3) it's easy to get a basic setup running.
Why bundle a custom web server with a scripting language when literally all you have to do is visit one more web site and download and run one more installer? I feel like I'm missing some huge key revelation here because I keep getting downvoted for these thoughts. Should PHP also bundle their own custom MySQL engine so I don't have to run 'sudo apt-get install mysql'? Or a copy of Tuxracer too so that I can play it when I need a coding break without having to run the installer?
Edit: in fact, at the risk of further downvotes, I would go so far as to say that if it really is that difficult for someone to do a basic Apache/mod_php install in their OS of choice, they should probably do more reading/tutorials before proceeding down the PHP rabbit hole any further. Figuring out how to install a dev instance of Apache isn't more difficult than reading the documentation for getting this new embedded PHP server running.
Don't worry man, I've got you covered. Key revelation coming up.
I suspect you're thinking of this in the context of getting a single PHP app running on your box. What if you have several? Or what if you want to have several branches of the same codebase checked out and running? It's doable with some VirtualHost kung fu, maybe a wildcard ServerAlias, but it's no longer as simple as 'apt-get apache'.
Much simpler to tell someone, "Do a 'php -S' in whatever project you're working on at the moment."
This is EXACTLY why it would be awesome to have a web server bundled with PHP.
Your scenario of running a simple test.php from the default DocumentRoot is a very contrived one, one that only the very first beginner will ever use, and even then only because he doesn't know how to configure Apache. Plus it assumes a lot of things: that it's on Linux, that it uses the distro-supplied packages, that they use a setup that is the very basic one, ... OK for beginners, but serious friction ('friction', not 'unsolvable problem' - that still doesn't make it unworthy of being addressed) for many experimentation scenarios.
Also running php -S in a shell is simple, run it, and minimise the window.
Your statements are only true for the simplest of cases.
I've been working with both LAMP and WAMP (Windows version) since 2003 - about 7 or 8 years now...
And even provide a pre-packaged WAMP type-distribution with a Pro edition and a Community Edition: http://www.devside.net/server/webdeveloper
I'll skip you the drama, but take my word for it, getting AMP to work well together (and working with AMP) is not a trivial task and requires literally years of experience. There are about 1000 ways you can screw it up. You might laugh at this statement with your "apt-get" or your Windows-based Apache setup.exe, but what happens afterwards? And what happens if there is a problem?
Having provided support for so many years, I can tell you it can be a nightmare for some people.
- All unnecessary steps to get a task done creates friction. Having to set up Apache for the 100th time is downright irritating. Eliminating friction = good.
- "And I think that lots of people (myself included) already see PHP as a pretty bloated system" Yes, well because "lots of people" think its true doesn't necessarily make it so. "Bloated" usually means to complainers "has functionality that I don't use", more often than not followed by a euphemism for "please take away functionality that others use so that I don't get confused by all the options".
- The practical merit of a programming language is not defined by its syntax, or even library. It's defined by those things plus its ecosystem plus its tools plus it community plus all things around it. Ease of use is one of those things too, and a simple way to run scripts is a great boon to this ease of use. Python has an interactive shell, should that not be part of the default package? Some languages come with a build-in debugger, should that not be in the default package? All these tools increase the practical use of the language - one can debate where the line should be drawn, but an argument along the line of 'it's not strictly a part of the language or core functionality so it shouldn't go in' does not make sense.
Also, just because a feature is offered 'as a gift' does not mean it should automatically be included in a OSS project. Thats how projects become bloated (this is not comment for or against the idea, just a general point).
Yes, I know you can set up Apache and mod_php or whatever, but starting a server from the command line is just awesomer. You'll see, guys!
And for those, who want greater flexibility, https://github.com/indeyets/appserver-in-php is a Ruby-Rack/Python-WSGI style solution which gives you possibility to initialise objects, open db-connections, pre-warm caches, etc. before serving requests. Provides HTTP, SCGI and Mongrel2 interfaces
http://bergie.iki.fi/blog/introducing_the_midgard_create_use...
for dumping arrays and things, i use an error_log_r() which just does print_r to the error log:
function error_log_r($obj) {
ob_start();
print_r($obj);
$lines = explode("\n", ob_get_clean());
foreach ($lines as $line)
error_log($line);
} $lines = explode("\n", print_r($obj,1));
Or better yet if we're playing golf: array_map('error_log', explode("\n", print_r($obj, 1)));thanks, i've updated my local implementation.
Also, this would make it easier to use Xdebug or something like that as it wouldn't require that to be installed in a possibly production system.
Also, most beginners using PHP start off with some shared hosting somewhere and get the directory include stuff wrong and now have live bad code on the web. At my university that was a big issue, but it is the only way these beginners had to test their website.
And a built in web server would only be good for the novice tinkerer. I don't see entrenched PHP shops switching, nor new applications being written specifically for the build in server when Apache w/mods, nginx or LigHTTPD offer better solutions (mature solutions) that are available now.
If it's kept as simple as the RFC suggests, it should be stable real soon and there are definitely scenarios where I'd choose this over launching Apache or even installing it, depending on the task and environment.
I don't say this because it's PHP particularly either. Nobody seems to be able to avoid really trivial and massive vulnerabilities in new web servers. I've lost track of the number of times I've seen it. I'd link to a search of osvdb.org for "directory traversal web server" but it seems to be down.
When using django, you just have to install django using something like pip. Then from your project folder you can launch a dev server without need to configure apache.
As Steve Jobs would say, 'it just works'. Projects like Drupal would benefit immensely from something like this.
"Hopefully this will end the long-standing practice of novice PHP developers writing code and pushing it to production to test it (or worse, editing code on the production server)."
I was guilty of this because I just wanted to start writing code, not shave yaks for an hour to get Apache and everything else installed.
Rails definitely got this right, I doubt it would be nearly as popular if it weren't so freakin easy to get started on the tutorials. Not that PHP has a popularity problem.
There is practically never a valid excuse for uploading testing code to production, unless the site is down and you already have everything under revision control.
I think it's good to encourage best practices even if everyone should know these things, especially for the newbies.
I also wrote an extension for the mongrel HTTP parser, but you could probably get away with a half-assed pure PHP HTTP parser.
http://github.com/dhotson/httpparser-php
It sounds crazy but it actually works pretty well. :-)
One of the strengths of PHP has always been for me that your development and production environments are pretty much the same, and deployment is just a matter of finding a way to copy the files over (and you can be as simple or clever about that part as you like).
Teaching a newbie how to run the PHP server is not that much less complicated than getting a default apache install running (especially with all the LAMP and WAMP installers out there to do it for you), and then you'd have to teach them about apache anyway before they pushed to prod.
1. Your dev box isn't supposed to be very similar to production, this is why you have staging servers. My dev boxes have weird tooling and debug tools. They also run all services on a single box rather than distributed. Don't pretend dev is at all like prod, this assumption will disappoint you.
2. App servers, which generally speak HTTP and use a standard interface (in ruby Rack, in Python WSGI) and speak HTTP to a load balancer (probably nginx). Are simple and just work. They're less pain than setting up apache. `cd myproject && rails server` is nice.
3. There are about 10 billion things you can get wrong with setting up a production server. If you can't figure out how to proxy requests to an app server you have no business setting up a server in the first place. You've probably already lost.
It basically sets up a separate instance that runs PHP and requests are shuttled to the backend. Unfortunately there is no way to talk to it directly like there would be with rails and django's built in Python servers.
2. For me, "apt-get install apache" once is much simpler and cleaner than running a dev server every single time, but I have been doing it this way for more than a decade, so perhaps you are right and I am just set in my ways.
3. If you can't set up a production environment you can't set up a production environment, and it doesn't matter which architecture is involved.
> ...LAMP and WAMP installers...
Contradiction there, unless you're suggesting that production systems typically use all-in-one LAMP/WAMP installers.
An interesting thought though.
Then there's the apc cache question, there would have to be some way of persisting/sharing it.
Full blown goofy, but a little fun to ponder.
Django has this built in, Rails too. PHP went a step further and put this in the core language and that makes sense because PHP in itself is meant for the web, and is not a framework over some other language.
At this point in the game, Apache-class servers are glorified proxies, and occasionally dole out static data when you're not using a CDN.
I have customized my web server so much, I would've had to wait for years to see the changes in Apache, nginx or lighty, if ever.
It seems like you didn't read the whole RFC. The configuration is actually very simple (just the command line options), and not meant to be a front-facing HTTP server.
use \My\Namespace;
$var = new Namespace\MyClass();
$var->Foo();
I have to remember and type the parent namespace for every imported class I want to use. In a big project this gets to be too much typing and too much for my brain to remember. Contrast with say, C#: using My.Namespace;
MyClass var = new MyClass();
var.Foo();
You import a namespace, and the compiler figures out what goes where without you having to remember anything.Plus the weird exceptions required for backwards compatibility, and the global namespace: I often forget where a root \ is required and where it isn't (not when declaring a namespace, but often--not always!--when referencing one). And of course there's the monstrous choice of namespace separator.
End of the world? Not really. Sucks? Yeah. And possibly a dealbreaker for me for larger projects with complicated namespaces and lots of classes.
I'd love for this to be just a misunderstanding on my part, because it would make my life a lot easier if PHP namespaces where more like C# ones!
And a good read on PHP namespaces in general: http://weierophinney.net/matthew/archives/254-Why-PHP-Namesp...
$var = new MyClass();
This brings it pretty much in line with your C# example. Have you read the PHP docs on namespaces? namespace My {
class MyClass { public function Foo() { echo 'Foo'; } }
}
namespace {
use My\MyClass;
$var = new MyClass();
$var->Foo();
}Django phrase it in their tutorial as:
"We've included this with Django so you can develop things rapidly, without having to deal with configuring a production server -- such as Apache -- until you're ready for production.
Now's a good time to note: DON'T use this server in anything resembling a production environment. It's intended only for use while developing. (We're in the business of making Web frameworks, not Web servers.)"
As long as the PHP documentation includes similar caveats and guidance, an HTTP server isn't a bad thing.
Don't see how that would be a bad thing. Something simpler and quicker than installing XAMPP or configuring the webserver that comes with your OS/distro.