0

i.e to replace Apache with a PHP application that sent back html files when http requests for .php files are sent?

How practical is this?

algorithmicCoder
  • 6,595
  • 20
  • 68
  • 117
  • 1
    first result in google: `Nanoweb is an HTTP server written in PHP` http://nanoweb.si.kz/ – OZ_ May 25 '11 at 22:26
  • 7
    `How practical is this?` - only for self-learning. – OZ_ May 25 '11 at 22:27
  • 2
    As OZ_ said, it's ok for learning, but for real world use it's not practical. There are many limitations to PHP, you can't really compare its abilities with languages such as C/C++ when it comes to creating a multithreaded application that has to handle lots of data and serve everything fast. – N.B. May 25 '11 at 22:31
  • Sending back HTML when HTTP requests for PHP files is exactly what the existing non-PHP webservers (e.g. Apache, lighthttpd, nginx, IIS, etc.) already do... – El Yobo May 25 '11 at 22:34
  • PHP is already a recursive acronym, don't make it worse! :) – Jason Palmer May 25 '11 at 22:45
  • @El Yobo Ofcourse, but we ask questions to get knowledge and information about the questions...not to be forced to get the answers to DIFFERENT questions..besides @N.B and @OZ_ servers are written in Python for example and it would be good to see some insight as to why Python is used in "practical" settings but PHP is seen as impractical... – algorithmicCoder May 25 '11 at 22:45
  • If you only wanted an exact answer to your two questions, then they are "It's possible. It's not practical.". However, I suspect that you actually want slightly more than that, hence my answer and all the other answers you've got. – El Yobo May 25 '11 at 23:21
  • @OZ_: I don't see why it can't be practical, this is one of the node.js strong suits. – Alix Axel May 26 '11 at 00:18
  • @El Yobo it's not practical because....etc and Python works because etc is a much better answer...p.s. a comment is not an answer – algorithmicCoder May 26 '11 at 00:39
  • But algorithmicCoder, you told me off for answering more than your question; and "because" was not part your question. – El Yobo May 26 '11 at 01:02
  • @Alix - node.js is [written in C/C++](http://www.lullabot.com/podcasts/drupal-voices-alex-barth-on-nodejs-and-express-versus-drupal). – El Yobo May 26 '11 at 01:03
  • @El Yobo: I know, but that doesn't change the facts: you can come up with a reliable web server in PHP that is both practical and efficient - nanoserv for instance is somewhat bloated and doesn't take advantage of stuff like libevent, nonetheless it can still perform better than Apache in certain scenarios. – Alix Axel May 26 '11 at 01:17
  • @Alix, the point is that node.js is an apples-to-oranges comparison; the OP is asking about writing a webserver in a high level dynamically typed language with generally poor memory management and node.js is *not* written in one of these, so saying that "this is one of the node.js strong suites" is wildly inaccurate. OZ_'s point , elaborated by N.B. is that PHP is bad for this and C/C++ is good for this. Node.js further confirms this, and refutes the very point you were trying to make with it. – El Yobo May 26 '11 at 01:48
  • @El Yobo: C/C++ is good for everything that involves performance, I'm not saying that a PHP HTTP server would beat nginx but I just don't agree with the unpractical statements as an absolute truth. A HTTP server in PHP that is able to serve hundreds or maybe thousands of requests per second is doable with a little effort. My point with node.js was not the underlying language, just that the language acts as the server - which, IMO, is very practical. – Alix Axel May 26 '11 at 02:28
  • 1
    Sorry Alix, I'm still not sure I'm getting your point. What do you mean that the language acts as the server? Node.js is a C/C++ server running javascript applications, which still seems unconnected to the question of a PHP server running PHP applications; perhaps you can explain further? – El Yobo May 26 '11 at 02:41
  • @El Yobo: I understand your point, I just don't agree that unpractical is the right label for every case. Nginx performs way better than Apache and still, Apache continues to be the most practical web server around. I believe that the built-in web server in PHP 5.4, the libevent extension that adds non-blocking IO to buffer streams are good efforts than can contribute towards a more robust PHP web server. If it can handle dozens or maybe hundreds of requests per second it will be practical for the vast majority of the small web sites in the web. – Alix Axel May 26 '11 at 15:17
  • @El Yobo: I'm not saying that this is the ideal solution, surely one would benefit from making use of a mature and well optimized web server coded in a more efficient programming language, but disregarding the possibility as being plainly unpractical without having the knowledge of the actual factors that could make it unpractical seems a bit drastic to me. Given that the OP has given us little information about those factors I still can't accept that PHP is unpractical for his usage as a definitive truth. – Alix Axel May 26 '11 at 15:24
  • Alix, again (if you look at my comment from yesterday below), you'll see that the PHP 5.4 webserver not written in PHP, it's *written in C/C++*. You even commented after mine down there, you mustn't have read it :). The point being that Node.js and the PHP 5.4 webserver are clearly not evidence of the usefulness of a high level language webserver *because they're not written in a high level language*. – El Yobo May 26 '11 at 22:21
  • @EL Yobo "No" or "Yes" is certainly not standard as a complete answer on S.O. Thankfully everyone does not answer questions like that. "Because"...XYZ or "e.g." XYZ ...is actually fairly standard and implied by every single question on S.O. The idea is that the person who asks a question DOES NOT KNOW and wants to LEARN....The happy ending to all this would be you writing a nice answer below about why PHP would be **worse** fundamentally than say Tornado which is written in a dynamically typed AND interpreted language...but powers Facebook's real time notifications... – algorithmicCoder May 27 '11 at 06:48
  • @algorithm; I know. That's why I gave you more than you asked for at the start, but then you started complaining about different answers, so I restricted myself to what you'd asked. – El Yobo May 27 '11 at 07:23

3 Answers3

4

It's already been done but if you want to know how practical it is, then i suggest you install and test with Apache bench to see the results:

http://nanoweb.si.kz/

Edit, A benchmark from the site:

Server Software:        aEGiS_nanoweb/2.0.1-dev
Server Hostname:        si.kz
Server Port:            80

Document Path:          /six.gif
Document Length:        28352 bytes

Concurrency Level:      20
Time taken for tests:   3.123 seconds
Complete requests:      500
Failed requests:        0
Broken pipe errors:     0
Keep-Alive requests:    497
Total transferred:      14496686 bytes
HTML transferred:       14337322 bytes
Requests per second:    160.10 [#/sec] (mean)
Time per request:       124.92 [ms] (mean)
Time per request:       6.25 [ms] (mean, across all concurrent requests)
Transfer rate:          4641.91 [Kbytes/sec] received

Connnection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0     0    1.9      0    13
Processing:    18   100  276.4     40  2739
Waiting:        1    97  276.9     39  2739
Total:         18   100  277.8     40  2750

Percentage of the requests served within a certain time (ms)
  50%     40
  66%     49
  75%     59
  80%     69
  90%    146
  95%    245
  98%    449
  99%   1915
 100%   2750 (last request)
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
2

Apart from Nanoweb, there is also a standard PEAR component to build standalone applications with a built-in webserver:
http://pear.php.net/package/HTTP_Server

Likewise the upcoming PHP 5.4 release is likely to include an internal mini webserver which facilitates simple file serving. https://wiki.php.net/rfc/builtinwebserver

php -S localhost:8000
mario
  • 144,265
  • 20
  • 237
  • 291
  • And it will come as no particular surprise to most people here that the build in webserver is [written in C, not PHP](https://gist.github.com/835698). That's a pretty nifty feature that they're adding though :) – El Yobo May 26 '11 at 01:50
0

Just FYI, PHP 5.4 just released with in-built webserver. Now you can run a local server with very simple commands like -

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

And you'll see the requests and responses like this -

PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011
Listening on localhost:8000
Document root is /home/me/public_html
Press Ctrl-C to quit.
[Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read
[Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read
[Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read
[Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read
[Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96