Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The PHP engine interprets the PHP code and is perfectly capable of working with sockets, forming HTTP packets, etc. Why then do we need a separate Apache server, when the http serving duties could be embedded into the php application along with the page creation code?

share|improve this question
    
...to handle communication for example. –  Paul Hiemstra Mar 13 '13 at 21:08
3  
You'll need to do a bit of research by yourself on how HTTP and HTTP servers work. –  Yannis Rizos Mar 13 '13 at 21:11
1  
add comment

4 Answers

In addition to Yannis' answer, I want to add a few points.

The PHP engine can handle sockets so you could write a basic web server. You can also write a web server in Visual Basic 6 with the Winsock control - but it's not the right tool for the job.

Apache is a hugely powerful web server. It makes no sense to reinvent the wheel, particularly for a non-commercial project with limited scope, when the PHP team can leverage the testing, research and development that went into building Apache.

The other thing you definitely need to consider is that PHP doesn't support threads. There are workarounds (using shell_exec, for example), but you would be unable to write a multithreading server.

share|improve this answer
1  
PHP doesn't support threads That's true, but it might not be in the near future: php.net/manual/en/intro.pthreads.php –  Yannis Rizos Mar 13 '13 at 23:04
add comment

As of PHP 5.4 we don't need a separate web server, the CLI SAPI provides one build in. It's a fairly new addition to the language, and I wouldn't use it on production:

This web server is designed for developmental purposes only, and should not be used in production.

The build in web server might not be ready for production (yet?), but it does prove that PHP is more than capable of working without a third party web server. As for building a web server in PHP, that's also possible and a good example is nanoweb. The project appears to be dead, but you can still download its source and fool around with it.

share|improve this answer
1  
Of course you can write a web server in PHP. There's even one in PostScript. That doesn't mean it's a good idea to do so, and I think exploring the benefits of, say, Apache over what I assume to be the idea of a rather simplistic idea of a web server is probably the whole point of the question. –  Christopher Creutzig Mar 13 '13 at 22:17
    
@ChristopherCreutzig I was interrupted before I had the chance to expand my answer to rant about how Apache has been around for nearly 2 decades, don't reinvent the wheel, etc. Fortunately, Sam took the opportunity and posted his answer. –  Yannis Rizos Mar 13 '13 at 23:01
    
I'd also like to add that PHP isn't particularly well suited for long-running tasks, such as a web server. The PHP position is, and always has been that PHP is designed for short lived scripts. –  Craige Mar 14 '13 at 3:04
add comment

Not just PHP but any language runtime used for web applications development. Actually all of them, including PHP as mentioned by Yannis Rizos, can handle http communication themselves and it is used for testing purposes. But it's not used for production. The reasons are:

Configurability

The dedicated web servers support virtual hosts, URL rewriting, authentication, SSL and similar things that would have to be implemented in each an every web application runtime.

Having separate front-end web server and back-end application engine allows mixing and matching features provided by different implementations of each.

Uniform interface

A single computer only has one port 80, so only one process can be listening on it. But typical public web server runs multiple different webs and web applications often implemented in different languages. The only way to get them all run together on the same host is to have the web server in front of them that dispatches the request appropriately.

Reliability

Bugs in the web application can crash the engine or at least make the engine unresponsive. The web server can shield other visitors from such problems by multiplexing the requests over multiple instances of the back-end and restarting those instances when they exhibit problems. Apache normally preventively restarts it's workers after 100 or so requests. Different front-ends can do things differently, but all are able to restart misbehaving back-end.

share|improve this answer
add comment

It would be hard to have multiple requests running at same time.

However PHP 5.5 will have coroutines which should make writing simple servers much more sensible. (if coroutines in PHP are same as coroutines in other systems).

There are quite a few pure Python web servers, and they work great.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.