Reducing Apache Memory Consumption – Process PHP with FastCGI
What is FastCGI?
CGI are programme written to help the processing of Perl Language that has been written for the web and they also help the Web Server in processing requests and handling Webpages.
I think this is why Nginx is less memory consuming than Apache, most Apache users use the mod_php which consumes much memory. Per user request, mod_php consumes about 100MB of your RAM to process PHP pages, compared to Nginx which uses CGI, all it does is load static pages, if it is PHP send it to CGI to process and get back the response.
Of course this can be applied to Apache too since when using mod_php, PHP is now part of Apache module and its in the hand of Apache to process both web pages and PHP per request and that is why it takes much memory. Why not leave Apache to server webpages and give PHP to CGI to process and this can free up your memory. Now in this post I will show you how.
NOTE: I assume you already have Apache installed and running and also am showing this with Ubuntu 14.04.1
Update repositories and install FastCGI
$ sudo apt-get update && apt-get install libapache2-mod-fastcgi
If you get this error:
12345 Package libapache2-mod-fastcgi is not available, but is referred to by another package.This may mean that the package is missing, has been obsoleted, oris only available from another sourceE: Package 'libapache2-mod-fastcgi' has no installation candidate
You have to edit your source list and add multiverse to Ubuntu repos
vi /etc/apt/sources.list
add multiverse to each repo
deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse deb http://security.ubuntu.com/ubuntu trusty-security main restricted universe multiverse
Then retry installation which should go easily 🙂 now lets install the php-fpm which is FastCGI Process Manager for PHP because we are no more gonna use Apache PHP module 😉
$ sudo apt-get install php5-fpm
Now we are creating an Apache config file to make switching easier if you have intension of moving back to mod_php, we are creating php5-fpm.conf file then add these to it
$ sudo vi /etc/apache2/mods-available/php5-fpm.conf
<IfModule mod_fastcgi.c> AddHandler php5-fcgi .php Action php5-fcgi /php5-fcgi Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule>
Work made easier 😉 Lets not switch from mod_php to php5-fpm
$ sudo a2dismod php5 $ sudo a2enconf php5-fpm $ sudo service apache2 restart
In the above, we disabled the mod_php module and enabled the php5-fpm.conf we created then restarted Apache. Test your server by running
<?php phpinfo(); ?>
in a PHP file and you are gonna find Server API : FPM/FastCGI in the third row 😉 and we are good to go, you observe your Apache isn’t consuming much memory as before 🙂 .
If you wish to switch back to mod_php for some reasons, just run the opposite of the above:
$ sudo a2disconf php5-fpm $ sudo a2enmod php5 $ sudo service apache2 restart
Note: php.ini file for php-fpm is located at /etc/php5/fpm/php.ini