Apache2/workerMPM/FastCGI/PHP5

Usually when you are trying to do something on linux and have no ideea you usually google it. I was doing it myself recently when i had to put up a new web server with apache 2 and i just decided to change from the standard mod php to fastcgi spawned php and from regular prefork MPM to the worker one.
Well trust me i’ve been crawling all the net for several days and i found so many advices and tips and mini howtos, that in the end all the info got so mixed in my head i was unable to figure it out.
So i started out step-by-step and reading all the readme files and docs of apache and php alike as well as fcgi specs.
I’m publishing this just for the reason of being usefull to other persons like me, rather than just have “YET ANOTHER” howto alongside miriads of others.

But let’s just start

You may or may not know that PHP is considered not to be thread-safe therefore not suitable to be run in multi-threaded envs like apache with worker-mpm. Well i’ve decided to test on my own and see if those statements are true and if i manage to get to see the “PHP has encountered an access violation at memory_address” error. I wouldnt like to dissapoint people at neosmart.net[http://neosmart.net/blog/2008/dont-believe-the-lies-php-isnt-thread-safe-yet/] and i wouldnt want by comments to earn a collective cockpunch but my experience with fcgi’d PHP5 under apache+worker=mpm is the best possible one.

I’m not covering all the apache2 initiald eploy, and even if you didnt installed any of htem yet the command i’ll issue will check dependencies and ask you to install all the other pre-requisites. Pay attention to the fact that when you are trying to install the apache2-mpm-worker package your Debian will want to uninstall the libapache2-mod-php5 one. That’s because the PHP5 module is not compatible with the multithreaded version of apache. It only owrk with standard prefork MPM.

So we are goign to use worker but there’s no more php5 module. Worry not as we will run it as cgi within fastcgi thus freeing apache itself that will only serve static content as images, html files, css etc, while all dynamic php content will be served by php itself.

gw1-livent:~# apt-get install apache2-mpm-worker libapache2-mod-fcgid
Reading package lists… Done
Building dependency tree… Done
The following extra packages will be installed:
apache2.2-common
The following packages will be REMOVED:
apache2-mpm-prefork libapache2-mod-php5
The following NEW packages will be installed:
apache2-mpm-worker libapache2-mod-fcgid
The following packages will be upgraded:
apache2.2-common
1 upgraded, 2 newly installed, 2 to remove and 75 not upgraded.
Need to get 1424kB of archives.
After unpacking 5349kB disk space will be freed.
Do you want to continue [Y/n]?

Just pressing enter will continue with the installation of the necessary packages.
The fastcgi module will be enabled at install time. You should see something like

Setting up libapache2-mod-fcgid (1.10-2) …
Module fcgid installed; run /etc/init.d/apache2 force-reload to enable.

However, if you dont you will need to enable it manual using:

gw1-livent:~# a2enmod fcgid

And you should see the same message “Module fcgid installed; run /etc/init.d/apache2 force-reload to enable.”
Make sure to edit you /etc/apache/mods-enabled/fcgid.conf so it looks like:

<IfModule mod_fcgid.c>
AddHandler fcgid-script .fcgi
SocketPath /var/lib/apache2/fcgid/sock
IPCConnectTimeout 20
</IfModule>

Now that we are done with the fastcgi part we need to reinstall PHP5 which was previously uninstalled.

gw1-livent:~# apt-get install php5-cgi php5-curl php5-gd php5-ldap php5-mysql php5-sqlite php5-xsl

(Feel free to browse the php5 packages and modify the line above to suit your php5 extensions requirements)

In order to make use of the freshly installed php under fastcgi we will need to enable fastcgi/php support per virtualhost basis.
So assuming you have the domain.com this is what the virtualhost configuration should look like:
(You may have other config lines and stuff , i am just giving you an example)

<VirtualHost www.domain.com:80>
ServerName www.domain.com
ServerAlias domain.net
ServerAdmin admin@domain.com
DocumentRoot /var/www/dn

<Directory /var/www/domain>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/domain-error.log
CustomLog /var/log/apache2/domain-access.log combined
LogLevel warn
ServerSignature On
</VirtualHost>

All the trick is done by these lines:

AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
Options ExecCGI

Now you should just restart apache and test out your new domain by using

<?php phpinfo(); ?>

inside a newly created index.php file. (or you can name whatever you want)

I hope this does help you folks
Over and out

Andy




This entry was posted on Thursday, September 4th, 2008 and is filed under Featured, Linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

11 Responses to “Apache2/workerMPM/FastCGI/PHP5”

  1. kkruecke

    Thanks for this really helpful article.

  2. kkruecke

    Should each virtual host (in order to have php5 support) have the same entries as your example configuration? I just want to be sure about this.

  3. Andy

    You are welcome, and yes each virtual host conf needs to have those lines.

    Andy

  4. Vortex

    Thanks! This article was very helpful! Works fine and helped me a lot!

    Thanks again!

  5. Neo

    First of all, appriciate your helpful article.
    in this case, can Zend Optimizer be used with these combination?
    i’ve googled a lot of time to find out the answer but no luck.
    would you please guide me?
    Thanks in advance.

  6. Andy

    Zend Optimizer should be easily implemented since you add the load lines into php.ini. And as far as php.ini is concerned if you add the ZO into the php-cgi’s php.ini it should all be fine.

    Andy

  7. dader

    I was exactly in your case, and your writing perfectly match the point ! thanks a lot

  8. mt

    In some cases, an “apt-get install php5″ will not work, because the package system will always try to remove the worker and reinstall automatically the prefork.

    That can be easily avoided by telling the apt or whatever you use explicit the packages you want.

    “apt-get -u install apache2-mpm-worker libapache2-mod-fcgid ph5″ will ensure that apt understood that you want php5 AND the worker/fcgid stuff.

    That works too when you want for example install mysql with postfix. On most system, mysql will try to install exim4 as default. If you use apt-get install mysql postfix, apt will install it right.

Trackbacks

  1. Improving php performance with apache2-mpm-worker + mod_fcgid « PHP Hints
  2. Improving php performance with apache2-mpm-worker + mod_fcgid
  3. Improve php and apache2 performance with apache2-mpm-worker + mod_fcgid + php5-cgi « PHP Hints

Leave a Reply