By default most Apache web servers -- at least prior to Apache 2.4 -- run Apache's Prefork MPM (multi-processing module). Prefork works OK and offers the greatest compatibility with existing Apache modules, but it consumes a relatively large amount of memory because it spawns many processes that each handle one connection at a time. Prefork typically uses non-thread-safe extensions like mod_php to interpret code.
Advantages of Worker Over Prefork
To reduce the memory footprint, you can adjust Apache to use an alternate method of handling incoming connections, like the Worker MPM. Since mod_php is considered incompatible with Worker's threading, this also means installing an alternative PHP interpreter like FastCGI. To make FastCGI more useful and flexible, we'll also install a new process manager, PHP-FPM.
To Replace Prefork With Worker
Go to /etc/apt/sources.list and uncomment the "multiverse" entries. This allows you to install the mod_fastcgi module:
$ vi /etc/apt/sources.list $ apt-get update $ apt-get install apache2-mpm-worker libapache2-mod-fastcgi php5-fpm php5-gd
Add php-apc here [Update 4/2016: APC is now outdated. Use the Zend OPcache instead.] if you didn't previously install it. Installing mpm-worker using the Ubuntu package manager will also automatically remove mod_php, temporarily breaking your PHP web sites.
Enable Apache's FastCGI and Actions modules:
$ a2enmod fastcgi actions
Tell Apache to send all PHP files to the PHP-FPM process using a Unix socket connection:
$ mkdir /var/www/fastcgi $ chown www-data:www-data /var/www/fastcgi $ vi /etc/apache2/httpd.conf
Enter the following in httpd.conf:
Alias /php5.fastcgi /var/www/fastcgi/php5.fastcgi AddHandler php-script .php FastCGIExternalServer /var/www/fastcgi/php5.fastcgi -socket /var/run/php-fpm.sock Action php-script /php5.fastcgi virtual # Protect your FastCGI directory Order allow,deny Order deny,allow
Edit your PHP-FPM configuration (/etc/php5/fpm/pool.d/www.conf), changing the "listen" line to match the socket you specified in httpd.conf:
listen = 127.0.0.1:9000 ; change to this: listen = /var/run/php-fpm.sock
If you want to see the PHP-FPM status page, you'll need to create a virtual host for it. First, uncomment these lines in /etc/php5/fpm/pool.d/www.conf:
pm.status_path = /status ping.path = /ping
Then create a virtual host for it in /etc/apache2/sites-available: