How to install Apache2 worker MPM, FCGI and APC on CentOS
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
-
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.rpmCentOS 6# rpm -ivh http://mirror1.ku.ac.th/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm -
Install Apache and fcgid with the following command.
# yum install httpd php-cgi mod_fcgid -
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 -
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 -
Set up FCGI for PHP by editing php.ini.
# vi /etc/php.iniAdd or uncomment the following parameter.cgi.fix_pathinfo = 1 -
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 -
Create an FCGIWrapper to act as the PHP script interpreter.
# vi /var/www/cgi-bin/php-fcgiAdd 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 -
Make it executable by all users with this command.
# chmod 755 /var/www/cgi-bin/php-fcgi -
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> -
After making these changes, test the configuration with this command.
# service httpd configtest -
If there are no errors, restart Apache with this command.
# service httpd restart
Installing PHP-APC
- Install PHP-APC with the following command.
# yum install php-pecl-apc - 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 - Restart Apache to load the new configuration with this command.
# service httpd restart