Web Server

How to install Apache2 worker MPM, FCGI and APC on CentOS

apache-worker-php-fcgi-in-centos-6 Apache on CentOS uses Prefork by default, supporting the PHP module. If the system must handle heavy loads, it may be necessary to switch to Apache worker + PHP fcgi (or PHP fastcgi) to reduce the high memory usage of Prefork mode. Installing Apache2 worker MPM and FCGI on CentOS is slightly easier than on Ubuntu. Apache on CentOS supports both Prefork and Worker, so it does not need to be uninstalled and reinstalled. PHP FCGI is also available as a package through yum.

Installing Apache2 worker MPM and PHP FCGI

  1. Install the EPEL repository with this command. CenOS 5 # rpm -ivh http://mirror1.ku.ac.th/fedora-epel/5/x86_64/epel-release-5-4.noarch.rpm CentOS 6 # rpm -ivh http://mirror1.ku.ac.th/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

  2. Install Apache and fcgid with the following command. # yum install httpd php-cgi mod_fcgid

  3. After installing Apache and fcgid, configure Apache to use worker MPM. Edit /etc/sysconfig/httpd and uncomment the following line, or copy it into the file. HTTPD=/usr/sbin/httpd.worker

  4. Disable mod php, which CentOS normally installs automatically when Apache and PHP are installed.

    # cd /etc/httpd/conf.d/
    # mv php.conf php.conf.orig
  5. Set up FCGI for PHP by editing php.ini. # vi /etc/php.ini Add or uncomment the following parameter. cgi.fix_pathinfo = 1

  6. Add a PHP script handler for fcgid by adding the following settings to /etc/httpd/conf.d/fcgid.conf.

    AddHandler fcgid-script .php
    FCGIWrapper /var/www/cgi-bin/php-fcgi .php
    AddType text/html .php
    DirectoryIndex index.php
  7. Create an FCGIWrapper to act as the PHP script interpreter. # vi /var/www/cgi-bin/php-fcgi Add the following content.

    #!/bin/sh
    PHPRC=/etc/
    export PHPRC
    export PHP_FCGI_MAX_REQUESTS=5000
    export PHP_FCGI_CHILDREN=0
    exec /usr/bin/php-cgi
  8. Make it executable by all users with this command. # chmod 755 /var/www/cgi-bin/php-fcgi

  9. Add +ExecCGI to the vhost options, as in the following example, to allow the vhost to run CGI.

    <Directory /var/www/vhost/htdocs>
          Options -Indexes FollowSymLinks +ExecCGI
          AllowOverride All
    </Directory>
  10. After making these changes, test the configuration with this command. # service httpd configtest

  11. If there are no errors, restart Apache with this command. # service httpd restart

Installing PHP-APC

  1. Install PHP-APC with the following command. # yum install php-pecl-apc
  2. After installation, edit /etc/php.d/apc.ini and make sure apc.enabled=1. Then set the shared memory available to APC with apc.shm_size. The following example allows up to 128MB. apc.shm_size=128M
  3. Restart Apache to load the new configuration with this command. # service httpd restart
EOF.← Back to articles