Why can’t the PHP engine handle HTTP communication?

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?

3

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.

1

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.

3

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.

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật