All this time I used to use Apache with prefork method (apache2-mpm-prefork) combined with PHP5 module (libapache2-mod-php5). Until the day my web server choking out of resources because lots of users accessing. Apache with PHP module consumes a lot of memory because each request is handled by one process.
When I was googling, I found that Apache2 using worker is more efficient and less resource consuming because one process can handle many request (by using multithreading).
But heck, when I tried to install apache2-mpm-worker, my Ubuntu start whining about wanting to uninstall my libapache2-mod-php5. I found out that PHP5 module is not compatible with multithreading version of Apache. It only want the prefork version.
I was googling again, and I find that with worker version of Apache, PHP must be used in CGI form. Dang! I have bad experience with GCI version of PHP. But things have changed, now there is FCGI that much better handling CGI applications. Also by using this version of PHP, Apache will be less loaded because it will only handles static elements (html pages, images, css, etc). All PHP requests will be handled by PHP itself.
Well let just start the installation process.
Apache2 and mod_fcgid Installation
First we install Apache2 and FCGI module:
$ sudo apt-get install apache2-mpm-worker libapache2-mod-fcgid
And then we turn on the FCGI module:
$ sudo a2enmod fcgid
PHP5 Installation
Then we install PHP5 packages. These are what I used to have in my web servers. You can add or remove the packages you want:
$ sudo apt-get install php5-cgi php5-curl php5-gd php5-ldap php5-mysql php5-mysqli php5-sqlite php5-xsl
Apache Configuration
Now you edit the file /etc/apache2/sites-available/default, and then put these text below on the part after "NameVirtualHost" and before <Virtualhost>:
<Directory /var/www>
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
</Directory>
And then put ExecCGI to each <Directory>.
Example:
Options ExecCGI Indexes
Now you can just restart your new Apache:
$ sudo /etc/init.d/apache2 force-reload
You can test it out by using phpinfo(). Enjoy your new and cool Apache+PHP!
Bookmark/Search this post with:
Comments
Thanks for the info. Just
Thanks for the info. Just what I needed to get apache set up the way I wanted it.
BTW...I had to move FCGIWrapper /usr/lib/cgi-bin/php5 .php inside my <virtualhost><directory>.
Mark
Hey your Captcha is
Hey your Captcha is challenging. Distinguishing between upper/lower case on some letters.
I am getting lots and lots
I am getting lots and lots of 503 errors after doing this. When a few clients are hitting the machine at the same time - it doesn't take long for "503"s to be the only response. Has anyone seen this with this setup?
Thanks for the information
Thanks for the information but
Apache gives this error on reload.
************************************
FCGIWrapper not allowed here
failed!
************************************
i put
AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php
as mentioned above.
@bcg: In file
@bcg:
In file /etc/apache2/mods-available/fcgid.conf, you may add the following lines:
IPCCommTimeout 3600@Ravi: Sorry those lines
@Ravi:
Sorry those lines aren't complete. I have updated my blog.
Hi Ivan - I had a similar
Hi Ivan -
I had a similar issue with apache and php, sure with I had seen your post before I spent days trying to find a solution!
@cell phone: so datahawk.com
@cell phone: so datahawk.com is now using fcgid? thanks for coming into my blog.
"But heck, when I tried to
"But heck, when I tried to install apache2-mpm-worker, my Ubuntu start whining about wanting to uninstall my libapache2-mod-php5. I found out that PHP5 module is not compatible with multithreading version of Apache. It only want the prefork version."
This is inaccurate. The _Ubuntu_ build of mod_php is not built with thread safety. PHP5 is perfectly capable of running with Apache 2 in worker. Frankly, IMO, if you are running a web server that you rely on, you should be building your own binaries or using a distro like Gentoo that allows you to build them based on their USE flags.
@Brian: thanks for your
@Brian: thanks for your information. but i'm lazy and i hate compiling stuff. :D
Mr. Moon maybe wants to
Mr. Moon maybe wants to discuss with this guy:
http://neosmart.net/blog/2008/dont-believe-the-lies-php-isnt-thread-safe...
Ivan, you are a hero. I've
Ivan, you are a hero. I've been scouring the Internet, following endless complicated fcgi tutorials, and yours is the first that's actually worked. Thanks!
Now if I can just get it working with suexec I'm all sorted.
Works like a charm. Thanks
Works like a charm. Thanks Ivan
Dear Ivan, I was wondering a
Dear Ivan,
I was wondering a few weeks ago about this solution and "we" thought lets give it a try. I tried your way to run it but it wasn't so much faster. So i thought lets try with self-built php 5.2.6 and with some other tricks. So at the end, i just increased our webserver`s performance up to 40-50% faster successfully and its thread-safe.
Anyhow.. two things to mention:
1. Thanks a lot! You made me think about it!
2. If you are interested in my results i can let you know about what and how i did (because of corporal privacy reasons i cant tell you everything :))
Pingback
[...] involved uninstalling PHP. To use the worker MPM I would have to run PHP via FastCGI. I found these instructions on how to set that [...]
Thank you for this. I'm
Thank you for this. I'm having a problem that maybe you may have some insight on: PHP processes are staying alive too long (it seems) and I have the restart the server to clear them out.
* Ubuntu 8.04 (amd 64)
* Drupal 5.10
* PHP 5.2.4
* Problem mainly seen when running simpletests
The simpletests can run once, but then I get an error that it can't find a class (DrupalUnitTests) and I have to restart the server.
Thanks!
=Fen
@dark_jmg Hi mate, Can you
@dark_jmg
Hi mate,
Can you please provide more info about your results?
my mail is: 6aotka4a {at} gmail.com
Regards,
Ross
Pingback
[...] 于是只能选择使用 cgi 的方式来安装。参考这篇文章:Installing Apache2 and PHP5 using mod_fcgid [...]
Pingback
[...] @ 9:00 pm I came across an excellent article on how to improve php5 performance on Apache2: Installing Apache2 and PHP5 using mod_fcgid. There is a similar article Apache2/workerMPM/FastCGI/PHP5. These articles show how to run php5 [...]
Pingback
[...] on Jan.10, 2009 I came across an excellent article on how to improve php5 performance on Apache2: Installing Apache2 and PHP5 using mod_fcgid. There is a similar article Apache2/workerMPM/FastCGI/PHP5. These articles show how to run php5 [...]
Pingback
[...] — kkruecke @ 9:00 pm Tags: Apache2, apache2-mpm-worker, configure, mod_fcgid, php, php-cgi Installing Apache2 and PHP5 using mod_fcgid and Apache2/workerMPM/FastCGI/PHP5 shows how to run php5 with apache2-mpm-worker using the FastCGI [...]
Pingback
[...] http://ivan.gudangbaca.com/installing_apache2_and_php5_using_mod_fcgid Linux, PHP Apache, Linux, PHP [...]
Post new comment