Update May 2013: We no longer recommend fcgid ever since Ubuntu Server 12.04 was released. This is because that version has PHP-FPM, which provides every benefit that fcgid has, with the added advantage of a shared opcode cache for all processes. We will be writing a full article on PHP-FPM with Drupal in the near future (soon to appear at High Performance Drupal with Apache MPM Worker Threaded Server and PHP-FPM).
Most sites that serve large Drupal instances use Apache with mod_php. Although this is often the easiest and fastest option, it may not be the ideal situation in some cases. This article explains fcgid, a way to run PHP other than mod_php, how to get it configured, and what are the advantages and disadvantages of it.
Apache's mod_php is the most widely used mode for PHP with Apache. mod_php itself is the entire PHP interpreter embedded in each Apache process that gets spawned. This provides performance and stability benefits, e.g.
- No need to call an external process (e.g. CGI).
- No need to communicate with another process via sockets (e.g. Fast CGI).
- The APC cache is shared by all Apache processes.
It also has some disadvantages
- The memory footprint per Apache process is large, specially when sites indulge in contributed modules.
- If Apache is serving static content, e.g. images and CSS files, it still has to spawn large processes because of the embedded PHP interpreter.
CGI (Common Gateway Interface) is the legacy way of runing applications on the web from the mid 1990s or so. It was too inefficient for anything but small sites. CGI spawns a new process for every incoming request to execute a PHP script, a very resource intensive and inefficient way of doing things. No wonder it faded away over time as web applications became more complex.
FastCGI was introduced to avoid some of the issues with running languages, including PHP, inside the Apache process, as well as avoiding the inefficiency of CGI.
A FastCGI application is executed outside of the web server (Apache or other wise), and waits for requests from the web server using a socket. The web server and the FastCGI application can even be on separate physical machines and communicate over the network.
Because the web server adn the application processes are separate better isolation is possible.
In reality, running PHP as mod_fastcgi with Apache has proved to be problematic. Mainly with stability. Even on Drupal.org we tried it for a while, but switched back to mod_php after some time.
mod_fcgid was introduced to be binary compatible with FastCGI, but with better control over spawning processes. The benefits of process isolation are still there.
The article assumes you are using Ubuntu/Debian. For Red Hat or Centos, use the equivalent packages and yum to achieve the same results.
First we install the required Apache components, with Apache threaded server (MPM Worker), to save memory.
aptitude install apache2-mpm-worker libapache2-mod-fcgid
Then we enable the Apache fcgid module.
And install PHP CGI, and the a few other PHP components, if they are not already on your system.
aptitude install php5-cgi php5-curl php5-gd php5-mysql
To configure fcgid, you have to do two things:
1. Create a new file in /etc/apache2/conf.d/php-fcgid.conf and put the following in it:
AddHandler fcgid-script .fcgi .php # Where to look for the php.ini file? DefaultInitEnv PHPRC "/etc/php5/cgi" # Maximum requests a process handles before it is terminated MaxRequestsPerProcess 1000 # Maximum number of PHP processes MaxProcessCount 10 # Number of seconds of idle time before a process is terminated IPCCommTimeout 240 IdleTimeout 240 #Or use this if you use the file above FCGIWrapper /usr/bin/php-cgi .php ServerLimit 500 StartServers 3 MinSpareThreads 3 MaxSpareThreads 10 ThreadsPerChild 10 MaxClients 300 MaxRequestsPerChild 1000
For a large site with a server with more memory and CPUs we can use this:
AddHandler fcgid-script .fcgi .php # Where to look for the php.ini file? DefaultInitEnv PHPRC "/etc/php5/cgi" # Where is the PHP executable FCGIWrapper /usr/bin/php-cgi .php # Maximum requests a process handles before it is terminated MaxRequestsPerProcess 1500 # Maximum number of PHP processes. MaxProcessCount 45 # Number of seconds of idle time before a process is terminated IPCCommTimeout 240 IdleTimeout 240# Large site ServerLimit 2048 ThreadLimit 100 StartServers 10 MinSpareThreads 30 MaxSpareThreads 100 ThreadsPerChild 64 MaxClients 2048 MaxRequestsPerChild 5000
2. Add ExecCGI to the Options line in the vhosts you want to use PHP and or fcgid on, for example.
Order Allow,Deny Allow From All Allow Override All Options MultiViews Indexes Includes FollowSymLinks ExecCGI
Now you should restart Apache, and fcgid should be active.
The first thing you observe is that the memory size of the fcgi process is considerably larger (40MB) than when PHP is running as a mod_php within Apache (31MB). However, when using fcgi, Apache (pre-fork) is only 3.3 to 2.4 MB per process so you can have more of them to serve static files. The savings are even more if you use the threaded server. For example, an Apache process on a large site would be 20 to 25 MB, and the total number needed for thousands of threads would be 10 to 15 processes total. You also normally have less CGI processes in total than Apache.
fcgi 15 processes * (40 MB - 21 MB shared) + 21 MB shared + (20 Apache procs * 6 MB) = 426 MB
mod_php 26 processes * (26 MB - 13 MB shared) + 13 MB shared = 351 MB
Apache threaded MPM Worker
fcgi 15 processes * (40 MB - 21 MB shared) + 21 MB shared + (10 Apache procs * 25 MB) = 290 MB
mod_php 26 processes * (26 MB - 13 MB shared) + 13 MB shared = 351 MB
The exact figures will vary for your site, depending on how you configured Apache and what modules you have enabled on your Drupal site.
Maximum number of processes
Setting the upper limit for the number php-cgi processes is possible but tricky. Normally MaxProcessCount should be the parameter to set to prevent creation of PHP processes above the number set for that parameter. I had to dig in the mod_fcgid source to find that parameter.
Make sure that you set this figure to a reasonable one. Setting it too low will cause an error to be logged in your web servers error log, for example:
[Sun Jul 25 17:06:23 2010] [notice] mod_fcgid: /home/example.com/www/index.php total process count 25 >= 25, skip the spawn request
On a 512MB VPS for example, a value of 7 to 10 seems to be adequate. For an 8GB dedicated server, a value of 45 or even higher will do. The exact figures will depend on how many modules you have, and how memory they consume.
Also, make sure that you do not set PHP_FCGI_CHILDREN at all. What happens is that fcgid uses it as a multiplier, not an upper maximum, and PHP will kept on breeding like rabbits, and the server will thrash and swap very quickly.
We conducted performance benchmarks comparing mod_php and mod_fcgid. The conclusion is that mod_php is a bit faster than fcgid, but not by much.
Our benchmarks how the following at different concurrency levels:
With 4 concurrent users fcgid Transactions: 826 hits Response time: 0.58 secs Transaction rate: 6.90 trans/sec Successful transactions: 826 Longest transaction: 2.13 Shortest transaction: 0.26 Apache mod_php Transactions: 853 hits Response time: 0.56 secs Transaction rate: 7.12 trans/sec Successful transactions: 853 Longest transaction: 0.95 Shortest transaction: 0.26 With 15 concurrent users: fcgid Transactions: 674 hits Elapsed time: 120.35 secs Response time: 2.65 secs Transaction rate: 5.60 trans/sec Concurrency: 14.83 Successful transactions: 674 Longest transaction: 7.28 Shortest transaction: 2.23 Apache mod_php Transactions: 748 hits Elapsed time: 120.42 secs Response time: 2.40 secs Transaction rate: 6.21 trans/sec Concurrency: 14.90 Successful transactions: 748 Longest transaction: 4.19 Shortest transaction: 1.80
Hunter Scott Newman confirmed the benchmarks independently, and sent us the following results. Note that he is using the threded Apache server with fcgid.
Apache Worker + fcgid 4 users Requests per second: 391.59 [#/sec] (mean) Time per request: 10.215 [ms] (mean) Time per request: 2.554 [ms] (mean, across all concurrent requests) 10 users Requests per second: 585.91 [#/sec] (mean) Time per request: 17.067 [ms] (mean) Time per request: 1.707 [ms] (mean, across all concurrent requests) 50 users Requests per second: 647.78 [#/sec] (mean) Time per request: 77.187 [ms] (mean) Time per request: 1.544 [ms] (mean, across all concurrent requests) Apache Prefork + mod_php 4 users Requests per second: 445.88 [#/sec] (mean) Time per request: 8.971 [ms] (mean) Time per request: 2.243 [ms] (mean, across all concurrent requests) 10 users Requests per second: 655.13 [#/sec] (mean) Time per request: 15.264 [ms] (mean) Time per request: 1.526 [ms] (mean, across all concurrent requests) 50 users Requests per second: 670.00 [#/sec] (mean) Time per request: 74.627 [ms] (mean) Time per request: 1.493 [ms] (mean, across all concurrent requests)
Benefits and advantages
There are several advantages for using fcgid. Performance is not one of them, but the penalty may be well worth it for the following advantages:
- Less memory consumption. Apache processes that serve static files are very small (3MB or less). So we can have more of those in memory.
- Less network connections active, since there are less PHP process
- Less database connections active, since there are less PHP processes. This is a very important point, since MySQL performance drops as the number of connections increase.
Case study: Large site with fcgid
In order to show what fcgid can help with, consider the following graphs. See the before and after comparison.
Memory usage by day, before and after.
The same was true over a week too.
And the number of MySQL threads went down dramatically, because only PHP processes will connect to it, and not every Apache process.
And also the number of network connections inside the server.
And just to make our lives easier, we wrote a monitoring script that will plot the number of php-cgi processes on a graph. We can know what is going on and what was going on too.
Caveats and precautions
Not everything is rosy though. There has to be some drawback.
APC still stores the op-code caches for Drupal and its modules. It has to do the parsing more often though, unlike on mod_php.
Because of that, you may on occasions see errors such as "Fatal error: Allowed memory size of nnnnnn bytes exhausted (tried to allocate nnnn bytes)". Reloading the page causes it to load normally.
Although we have not dug too deep into it, we have found is that APC is not shared among the processes. And because the php-cgi processes are killed and new ones spawned, the persistence of APC's user/data cache cannot be relied upon. This is not important for most users, but if you are using things like the Drupal cache router module or the performance logging module, this will impact you.
If pure speed is what you are after, then stay with mod_php.
However, for better resource usage and efficiency, consider moving to fcgid.
Here are some other links on fcgid. Note that some of the configurations mentioned recommend the use the PHP_FCGI_CHILDREN and other settings that proved disastrous. They also do not mention the MaxProcessCount parameter which we found from the source code.
- Typo3: Using PHP with mod_fcgid.
- How to setup Apache2 with mod_fcgid on PHP5 and Debian Etch.
- FastCGI with PHP opcode cache.
- Installing Apache 2 and PHP5 using mod_fcgid (currently offline, Google Cache here)
- Memory usage Apache + PHP as module versus FastCGI (on Windows), and comments on it.
Update: Article updated to use Apache threaded server (MPM Worker) instead of the pre-fork server. This provides lots of memory savings.
Chris Miller (not verified)
I think the advantages ofMon, 2013/08/05 - 02:09
I think the advantages of Apache's mod_php explains it's remarkable features. But the disadvantages significantly points out it's limitations.
Visitor445 (not verified)
How can this work with pleskSun, 2014/02/02 - 16:44
How can this work with plesk installed on the vps container?