Difference between revisions of "Setup php-fpm 7 with Apache on Debian 9 Stretch"
(Created page with "These are steps to install php-fpm7.0 on a Debian 9 Stretch server. php-fpm allows for faster processing of PHP documents while using less memory than Apache2 with standard ph...") |
|||
Line 9: | Line 9: | ||
==Set up apache2== | ==Set up apache2== | ||
− | + | apt-get update | |
− | apt-get install apache2 php7.0 | + | apt-get install apache2 php7.0 |
==Test apache2 with phpinfo== | ==Test apache2 with phpinfo== | ||
− | rm /var/www/html/index.html | + | rm /var/www/html/index.html |
− | echo "<?php phpinfo(); ?>" > /var/www/html/index.php | + | echo "<?php phpinfo(); ?>" > /var/www/html/index.php |
− | Access your server, note the Server API | + | Note:Access your server, note the Server API |
==Install php-fpm== | ==Install php-fpm== | ||
− | apt-get install php-fpm | + | apt-get install php-fpm |
==Change listen address for php-fpm== | ==Change listen address for php-fpm== | ||
− | vim /etc/php/7.0/fpm/pool.d/www.conf | + | vim /etc/php/7.0/fpm/pool.d/www.conf |
− | Line 36 should be changed to this: | + | Line 36 should be changed to this: |
− | listen = 127.0.0.1:9000 | + | listen = 127.0.0.1:9000 |
==Add PHP handler to virtualhost or apache2.conf== | ==Add PHP handler to virtualhost or apache2.conf== | ||
− | <FilesMatch "\.php$"> | + | <FilesMatch "\.php$"> |
− | + | SetHandler "proxy:fcgi://127.0.0.1:9000/" | |
− | </FilesMatch> | + | </FilesMatch> |
Note: this tells Apache (global) or a specific virtualhost (per host) to use php-fpm depending on where you put it. | Note: this tells Apache (global) or a specific virtualhost (per host) to use php-fpm depending on where you put it. | ||
==Enable php-fpm== | ==Enable php-fpm== | ||
− | a2enmod proxy_fcgi | + | a2enmod proxy_fcgi |
==Restart apache and test== | ==Restart apache and test== | ||
− | service apache2 restart | + | service apache2 restart |
− | service php7.0-fpm restart | + | service php7.0-fpm restart |
Check your server's phpinfo for Server API - it should be "FastCGI" now | Check your server's phpinfo for Server API - it should be "FastCGI" now | ||
Revision as of 15:37, 16 January 2018
These are steps to install php-fpm7.0 on a Debian 9 Stretch server. php-fpm allows for faster processing of PHP documents while using less memory than Apache2 with standard php7.0. This means you can run faster websites with less resource use.
Written: 1-16-2018
VPS - Debian 9.2 Apache 2.4 Memory: 1GB
Contents
Set up apache2
apt-get update apt-get install apache2 php7.0
Test apache2 with phpinfo
rm /var/www/html/index.html echo "<?php phpinfo(); ?>" > /var/www/html/index.php
Note:Access your server, note the Server API
Install php-fpm
apt-get install php-fpm
Change listen address for php-fpm
vim /etc/php/7.0/fpm/pool.d/www.conf Line 36 should be changed to this: listen = 127.0.0.1:9000
Add PHP handler to virtualhost or apache2.conf
<FilesMatch "\.php$"> SetHandler "proxy:fcgi://127.0.0.1:9000/" </FilesMatch>
Note: this tells Apache (global) or a specific virtualhost (per host) to use php-fpm depending on where you put it.
Enable php-fpm
a2enmod proxy_fcgi
Restart apache and test
service apache2 restart service php7.0-fpm restart
Check your server's phpinfo for Server API - it should be "FastCGI" now
Speed and Performance
I tested the speed using Pingdom and Webpagetest before and after php-fpm installation on my test server that was under 0 load. Running my test Wordpress website averaged 2.1 seconds load time before php-fpm, and 1.3 seconds after. With MySQL, 4x php-fpm7.0 processes, and 4x apache2 prefork processes I was running at ~300MB real memory usage, with my buffers and cache filling up 600MB.
More speed
- Tune php-fpm's pool.d/www.conf file for more or less children depending on your resources
- Switch to Apache Worker MPM instead of prefork. This only works on some sites, but it allows you to disable mod_php and use php-fpm with threaded Apache2 children.