Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How apache runs PHP for pages [closed]

Tags:

php

apache

Imagine there is a PHP page on http://server/page.php.

Client(s) send 100 requests from browser to the server for that page simultaneously.

Does the server run 100 separate processes of php.exe simultaneously?

Does it re-interpret the page.php 100 times?

like image 489
Meysam Hit Avatar asked Jun 01 '13 15:06

Meysam Hit


2 Answers

Apache2 has multiple different mode to work.

In "prefork" (the most commonly used) mode, Apache will create process for every request, each process will run a own php.exe. Config file will assign a maximum number of connections (MaxClients in httpd.conf), Apache will only create MaxClients. This is to prevent memory exhaustion. More requests are queued, waiting for the previous request to complete.

If you do not install opcode cache extensions like APC, XCache, eAccelerator, php.exe will re-interpret the page.php 100 times.

like image 145
jysperm Avatar answered Oct 06 '22 23:10

jysperm


It depends.

There are different ways of setting things up, and things can get quite complex.

The short answer is 'more or less'. A number of apache processes will be spawned, the PHP code will be parsed and run.

If you want to avoid the parsing overhead use an opcode cache. APC (Alternative PHP Cache) is a very popular one. This has a number of neat features which are worth digging into, but without any config other than installing it it will ensure that each php page is only parsed into opcode once.

To change how many apache services are spawned, most likely you'll be using MPM Prefork. This lets you decide if how you want Apache to deal with multiple users.

For general advice, in my experience (small sites, not a huge amount of traffic), installing APC is worth doing, for everything else the defaults are not too bad.

like image 35
Rich Bradshaw Avatar answered Oct 06 '22 22:10

Rich Bradshaw