Apache-mpm-worker e Php5

Da PiemonteWireless.

Apache MPM Worker (the thread way) e PHP5

Recentente mi sono posto la domanda di cosa fossero tutte quei possibili pacchetti di installazione di Apache2 che si trovano su una Ubuntu.

# apt-cache search apache2|grep apache2-mpm
apache2-mpm-event - Event driven model for Apache HTTPD 2.1
apache2-mpm-perchild - Transitional package - please remove
apache2-mpm-prefork - Traditional model for Apache HTTPD 2.1
apache2-mpm-worker - High speed threaded model for Apache HTTPD 2.1
apache2-mpm-itk - multiuser MPM for Apache 2.2

Ho cosi' scoperto che sono delle diverse compilazioni di Apache. In particolare 2 sono quelle su cui conviene soffermarsi:

apache2-mpm-prefork - Traditional model for Apache HTTPD 2.1
apache2-mpm-worker - High speed threaded model for Apache HTTPD 2.1

L'ultima Ubuntu (7.04 Feisty) da come default la worker.

Un breve promemoria sulla differenza tra un'applicazione basata sul modello dei processi e sul modello dei thread: in Linux (ma anche in tutti gli altri OS) un processo e' un'applicazione in esecuzione; (se ricordate Platone, l'idea e' l'applicazione mentre la realta' e' il processo);

Da wikipedia:

  • Una prima differenza fra thread e processi consiste nel modo con cui essi condividono le risorse. Mentre i processi sono di solito fra loro indipendenti, utilizzando diverse aree di memoria ed interagendo soltanto mediante appositi meccanismi di comunicazione messi a disposizione dal sistema, al contrario i thread tipicamente condividono le medesime informazioni di stato, la memoria ed altre risorse di sistema.
  • L'altra differenza sostanziale è insita nel meccanismo di attivazione: la creazione di un nuovo processo è sempre onerosa per il sistema, in quanto devono essere allocate le risorse necessarie alla sua esecuzione (allocazione di memoria, riferimenti alle periferiche, e così via, operazioni tipicamente onerose); il thread invece è parte del processo, e quindi una sua nuova attivazione viene effettuata in tempi ridottissimi a costi minimi.


Quindi nel caso di apache la differenza e' fondamentale (sopratutto in caso di numerose richieste): ad ogni richiesta apache2-mpm-prefork forca un nuovo processo, allocandogli memoria, cpu e quant'altro richiede, mentre apache-mpm-worker semplicemente avvia un nuovo thread, che condividera' gran parte delle risorse col processo padre.

A questo punto sembra di gran lunga preferibile la versione worker, ma come al solito c'e' il rovescio della medaglia, che in questo caso si chiama PHP.

Qui sotto riporto la FAQ di php che spiega il rovescio della medaglia, ma vi anticipo e riassumo il mio punto di vista:

Se usiamo un threads server, e uno dei thread fa casino per qualsivoglia ragione, cio' manda in bomba tutto il processo, poiche' ne condivide gran parte delle risorse... ed essendo PHP un linguaggio che ti lascia fare (se non che ti facilita) ogni tipo di errore, il problema e' alquanto serio.

A mio avviso, se non si ha bisogno di PHP, il worker e' da preferirsi; in caso contrario bisogna stare attenti a pesare fra velocita' di esecuzione e possibilita' di crash (sopratutto su macchine di produzione).

Per chi volesse comunque usare la worker con PHP, qui sotto riporto un'articolo per risolvere i conflitti di dipendenze che APT riporta in questo caso.


Running multi-threaded Apache with PHP on Ubuntu

(estratto da: http://eightpence.com/running-multi-threaded-apache-with-php-on-ubuntu/)

If you try to install apache2-mpm-worker or apache2-mpm-perchild on Ubuntu, it will not let you do so alongside apache2-mod-php5.

The story is that many modules php might potentially load are not thread safe, and so running it in a threaded Apache is “not recommended”. However, with threaded Apache2, you get better performance and you might be able to wing running your PHP app without any problems. Mix these two at your own risk.

PHP has to have experimental multi-threading compiled in to run with multithreaded Apache, which is why you can’t just install the apache-worker and mod-php packages together. You’ll have to build it yourself, but this guide will show you how to compile php from source in Ubuntu.

# apt-get install apache2-mpm-worker

(or apt-get install apache2-mpm-perchild, the other multi-threaded Apache implementation).

I needed these to get Apache running on my production server, so you may need them too:

apt-get install build-essential flex libxml2-dev

If you want to compile PHP with a MySQL driver, you’ll need these development bits:

apt-get install libmysqlclient14-dev

Download the latest PHP sources (I’m using 5.1.6). Direct link to the latest version at the time of writing:

wget http://us3.php.net/get/php-5.1.6.tar.bz2/from/this/mirror
tar -xvjf php-5.1.6.tar.bz2
cd php-5.1.6

You must compile PHP with the location of your apache2 binary. I’m compiling in mysql support. You must also use the “–enable-maintainer-zts” switch to compile in threading support, or you will get “Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP. Pre-configuration failed!” when you try and load mod-php into Apache.

./configure --with-apxs2=/usr/bin/apxs2 --with-mysql --enable-maintainer-zts --prefix=/usr
make
sudo make install

You can then set the php module to load, and restart Apache

a2enmod php5
/etc/init.d/apache2 restart

And that should be it. I already had a working PHP/Apache setup on my system before I did this (although PHP was removed automatically when I installed apache2-mpm-worker), so I might be missing a step that you need from a fresh Ubuntu install (let me know and I’ll add it).

No hint of instability. apache2-mpm-worker is running this very blog (Wordpress) as I write.


Why shouldn't I use Apache2 with a threaded MPM in a production environment?

(estratto da: http://www.php.net/manual/en/faq.installation.php#faq.installation.apache2)

PHP is glue. It is the glue used to build cool web applications by sticking dozens of 3rd-party libraries together and making it all appear as one coherent entity through an intuitive and easy to learn language interface. The flexibility and power of PHP relies on the stability and robustness of the underlying platform. It needs a working OS, a working web server and working 3rd-party libraries to glue together. When any of these stop working PHP needs ways to identify the problems and fix them quickly. When you make the underlying framework more complex by not having completely separate execution threads, completely separate memory segments and a strong sandbox for each request to play in, feet of clay are introduced into PHP's system.

If you feel you have to use a threaded MPM, look at a FastCGI configuration where PHP is running in its own memory space.

And finally, this warning against using a threaded MPM is not as strong for Windows systems because most libraries on that platform tend to be threadsafe.


Name (required):

Website:

Comment:

Discussione:Apache-mpm-worker e Php5

34 Rating: 2.1/5 (11 votes cast)

Strumenti personali