PHP via FastCGI under Arch Linux

The Apache web server can be run in two modes: prefork and threaded. Threaded is far more efficient from a memory standpoint, however, PHP is not generally threadsafe and therefore canot easily be used with threaded Apache with mod_php. Using a wrapper like FastCGI allows easy use of PHP with threaded Apache. Here is how to configure it in Arch Linux.

First, install PHP, Apache, and FastCGI.

# pacman -S apache php-cgi mod_fcgid

FastCGI needs a wrapper or else suexec will get angry, so create one in /srv/http:

# mkdir /srv/http/fcgi-bin.d/php5-default
# ln -s /usr/bin/php-cgi /srv/http/fcgi-bin.d/php5-default/php-fcgi-wrapper

Create the FastCGI Apache configuration file /etc/httpd/conf/extra/php5-fcgid.conf with the following content:

<IfModule !mod_php4.c>
<IfModule !mod_php4_filter.c>
<IfModule !mod_php5.c>
<IfModule !mod_php5_filter.c>
<IfModule !mod_php5_hooks.c>
<IfModule mod_actions.c>
<IfModule mod_alias.c>
<IfModule mod_mime.c>
<IfModule mod_fcgid.c>
    # Path to php.ini – defaults to /etc/phpX/cgi
    DefaultInitEnv PHPRC=/etc/php
    
    # Number of PHP childs that will be launched. Leave undefined to let PHP decide.
    #DefaultInitEnv PHP_FCGI_CHILDREN 3
    
    # Maximum requests before a process is stopped and a new one is launched
    #DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000
    
    # Define a new handler "php-fcgi" for ".php" files, plus the action that must follow
    AddHandler php-fcgi .php
    Action php-fcgi /fcgi-bin/php-fcgi-wrapper
    
    # Define the MIME-Type for ".php" files
    AddType application/x-httpd-php .php
    
    # Define alias "/fcgi-bin/". The action above is using this value, which means that
    # you could run another "php5-cgi" command by just changing this alias
    Alias /fcgi-bin/ /srv/http/fcgi-bin.d/php5-default/
    
    # Turn on the fcgid-script handler for all files within the alias "/fcgi-bin/"
    <Location /fcgi-bin/>
        SetHandler fcgid-script
        Options +ExecCGI
    </Location>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>

Now, add the necessary information to /etc/httpd/conf/httpd.conf to load FastCGI:

LoadModule fcgid_module modules/mod_fcgid.so
Include conf/extra/php5_fcgid.conf

Finally, restart apache:

# rc.d restart httpd