Tuesday, February 10, 2015

Configuration of mod_fcgid in XAMPP Apache for Windows

First thing first, which one is the fastest of serving PHP source?

Here's a nice comparison on various PHP implementations in Apache:

http://2bits.com/articles/apache-fcgid-acceptable-performance-and-better-resource-utilization.html

Interesting points here:


Apache's mod_php
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
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
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 and 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
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.


Looks like a brief history on how CGI evolves.

mod_fcgid is a high performance alternative to mod_cgi or mod_cgid, which starts a sufficient number instances of the CGI program to handle concurrent requests, and these programs remain running to handle further incoming requests.

The Apache Software Foundation and the Apache HTTP Server Project are pleased to announce the release of version 2.3.9 of mod_fcgid, a FastCGI implementation for Apache HTTP Server versions 2.0, 2.2, and 2.4. This version of mod_fcgid is a security release.

I have been searching around for the way to implement mod_fcgid in Apache 2.4 for Windows which is embedded in XAMPP 1.8.x package.

Here's the procedure that would work on Windows:

1. Please download the zip package via the following link:
https://www.apachelounge.com/download/win32/modules-2.4/mod_fcgid-2.3.9-win32.zip

2. Stop Apache service.

3. Expand and extract the library file “mod_fcgid.so” and then copy it under the directory C:\xampp\apache\modules\.

4. Create new configuration file with the following content (adjust the actual paths of XAMPP installation directory to suit the needs):

C:\xampp\apache\conf\extra\httpd-fcgid.conf

##################################################################

 # Configuration for mod_fcgid

 # FileName: conf/extras/mod_fcgid_php.conf







 # Fast CGI module Settings (PHP 5.3, 5.4)



  FcgidInitialEnv PHPRC "C:\\xampp\\php"

  FcgidInitialEnv PATH "C:\\xampp\\php;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"

  FcgidInitialEnv SystemRoot "C:\\Windows"

  FcgidInitialEnv SystemDrive "C:"

  FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"

  FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"

  FcgidInitialEnv windir "C:\\WINDOWS"



 # Fast CGI module Settings (PHP 5.2)



 # FcgidInitialEnv PHPRC "C:/php/"

 # FcgidInitialEnv PATH "C:/php;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;"

 # FcgidInitialEnv SystemRoot "C:/Windows"

 # FcgidInitialEnv SystemDrive "C:"

 # FcgidInitialEnv TEMP "C:/WINDOWS/TEMP"

 # FcgidInitialEnv TMP "C:/WINDOWS/TEMP"

 # FcgidInitialEnv windir "C:/WINDOWS"



 # Global mod_fcgid settings



  FcgidIOTimeout 40

  FcgidConnectTimeout 10

  FcgidMaxProcesses 8

  FcgidOutputBufferSize 64

  ProcessLifeTime 240

  FcgidMaxRequestsPerProcess 500

  FcgidMinProcessesPerClass 0





 # Global Config Example

 # Comment out next 4 lines to use per-Directory or per-VirtualHost configuration

  

     #Options ExecCGI

  Options Indexes FollowSymLinks ExecCGI

     AddHandler fcgid-script .php

     FcgidWrapper "C:/xampp/php/php-cgi.exe" .php

  



 #

 # Examples below show how you can use per-Directory and per- VirtualHost Configs

 # You can use multiple Directory and VirtualHost Configs at the same time

 #



 # Per Directory Config Example

 #

 #

 #  FcgidInitialEnv PHPRC "C:/php"

 #  AddHandler fcgid-script .php

 #  Options Indexes FollowSymLinks ExecCGI

 #  AllowOverride all

 #  FcgidWrapper "C:/php/php-cgi.exe" .php

 #  Require all granted

 #





 # Per VirtualHost Config Example

 #

 #

 # DocumentRoot  /Apache22/htdocs/fcgi

 # ServerName fcgi.local

 # ErrorLog logs/fcgi.error.log

 # CustomLog logs/fcgi.access.log common

 # FcgidInitialEnv PHPRC "C:/php"

 #

 #  AddHandler fcgid-script .php

 #  Options Indexes FollowSymLinks ExecCGI

 #  AllowOverride all

 #  FcgidWrapper "C:/php/php-cgi.exe" .php

 #   Require all granted

 #

 #





 # Do not comment out below line



 ######################################################################





5. Add new lines to the following configuration file:
C:\xampp\apache\conf\httpd.conf
…

#LoadModule watchdog_module modules/mod_watchdog.so

#LoadModule xml2enc_module modules/mod_xml2enc.so

LoadModule fcgid_module modules/mod_fcgid.so





…

#FCGI Module

 Include "conf/extra/httpd-fcgid.conf"

#End of file





6. Comment out several lines to the following configuration file:
C:\xampp\apache\conf\extra\httpd-xampp.conf
…

#
# PHP-Module setup
#
LoadFile "C:/xampp/php/php5ts.dll"
LoadFile "C:/xampp/php/libpq.dll"
LoadModule php5_module "C:/xampp/php/php5apache2_4.dll"

#Comment out to use purely mod_fcgid to serve all .php files, see httpd-fcgid.conf
#
#    SetHandler application/x-httpd-php
#
#
#    SetHandler application/x-httpd-php-source
#
7. Start Apache service and test the web pages.

8. Examine and check if the libraries (including mod_fcgid) are loading properly via http://localhost/xampp/phpinfo.php







9 comments:

  1. Thanks, this tutor save a lot of my time

    ReplyDelete
  2. Thank you for the explanation, but although I have followed your instructions very carefully, my xampp apache server will not start up after I modify the httpd.conf - file. I can change all my settings back and start the server, but I can not start it with the modified httpd.conf-file. I have checked and rechecked but I cannot see what is happening here...
    This is my log file:

    [Tue Mar 29 00:21:10.387355 2016] [ssl:warn] [pid 3044:tid 376] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Tue Mar 29 00:21:10.449756 2016] [core:warn] [pid 3044:tid 376] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
    [Tue Mar 29 00:21:11.463757 2016] [ssl:warn] [pid 3044:tid 376] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Tue Mar 29 00:21:11.557358 2016] [mpm_winnt:notice] [pid 3044:tid 376] AH00455: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8 configured -- resuming normal operations
    [Tue Mar 29 00:21:11.557358 2016] [mpm_winnt:notice] [pid 3044:tid 376] AH00456: Apache Lounge VC11 Server built: Jan 28 2015 16:48:40
    [Tue Mar 29 00:21:11.557358 2016] [core:notice] [pid 3044:tid 376] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
    [Tue Mar 29 00:21:11.557358 2016] [mpm_winnt:notice] [pid 3044:tid 376] AH00418: Parent: Created child process 3520
    [Tue Mar 29 00:21:13.320161 2016] [ssl:warn] [pid 3520:tid 388] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Tue Mar 29 00:21:14.396563 2016] [ssl:warn] [pid 3520:tid 388] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Tue Mar 29 00:21:14.490163 2016] [mpm_winnt:notice] [pid 3520:tid 388] AH00354: Child: Starting 150 worker threads.

    I would be forever grateful if you could guide me somehow...
    Thank you!

    ReplyDelete
    Replies
    1. To follow up my previous post, the following code :
      "#FCGI Module

      Include "conf/extra/httpd-fcgid.conf"

      #End of file"
      is not making total sense to me ?
      I have added :Include "conf/extra/httpd-fcgid.conf" without the "" under "supplemental configuration". Following the includes of the other files from /extra -folder. I have also tried adding these lines at the end of the file or even adding them inside a IfModule directive.
      It is somewhat difficult it seems because the parsing of these conf-files seems to be very primitive and I guess my syntax is not spot on. So what am I missing ?

      Delete
    2. Think you should create file at conf/extra/httpd-fcgid.conf first, paste the content as described and then add that "Include ..." line to the bottom of file httpd.conf. httpd-fcgid.conf is a separate config file with FCGI parameters for initialization. Check the paths of PHP installation and see if there is any clue about wrong configs. Seeing that you are with PHP 5.6.8 which I have never tested this since I jumped from PHP 5 to PHP 7. The apache log you provided does not tell anything as it has not been started up successfully. You might check with apachelounge.com to see any new *.so file of mod_fcgid available. Or you might need to check it by commenting out those lines first and then uncomment them one by one for testing. Good luck!

      Delete
    3. Thank you for your answer! I will come back and post potential success in case others need it.
      Just one question. Developing for WT(Web toolkit) and wanting to use a local server for that, is Xampp a viable solution ?

      Delete
    4. Ok, managed to start my apache server!! I downloaded and installed the VS11 .so file, and it worked! Earlier I was using VS14 version.
      I added :
      "Alias /hello "C:/Users/xxx/Documents/Visual Studio 2013/Projects/G8/"

      Options Indexes FollowSymLinks Includes ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
      "
      to access directory outside xampp folder.
      Now I have to try to configure fastcgi on Apache for WT.
      Thank you for now!

      Delete
  3. Hello I have a problem with installing this on Windows Xp. It worked well on windows 10 and 8.1

    ReplyDelete
  4. Hey Hitchhiker, nice tutorial, it works great for me! THX a lot ;>)

    ReplyDelete