The Apache 2.4 series introduced mod_proxy_fcgi, enabling Apache to communicate directly with PHP-FPM via TCP/IP sockets without requiring older third-party modules like mod_fastcgi or mod_fcgid. This setup provides superior performance and memory isolation for multi-tenant web servers running Virtualmin.
1. Installing PHP-FPM
Install the latest PHP-FPM packages using Remi (CentOS) or Dotdeb (Debian):
# On CentOS:
yum install php-fpm -y
# On Debian:
apt-get install php5-fpm -y
2. Compiling Apache 2.4 from Source (CentOS 6)
If your distribution does not ship Apache 2.4 in its default repositories, compile from source with event MPM and mod_proxy_fcgi:
yum install pcre-devel -y
cd /usr/src
wget https://archive.apache.org/dist/httpd/httpd-2.4.25.tar.bz2
wget https://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
wget https://archive.apache.org/dist/apr/apr-util-1.5.4.tar.bz2
tar -jxf httpd-2.4.25.tar.bz2
tar -jxf apr-1.5.2.tar.bz2
tar -jxf apr-util-1.5.4.tar.bz2
mv apr-1.5.2 httpd-2.4.25/srclib/apr
mv apr-util-1.5.4 httpd-2.4.25/srclib/apr-util
cd httpd-2.4.25
./configure --prefix=/opt/apache2 --with-mpm=event --enable-rewrite --enable-proxy --enable-proxy-fcgi --enable-ssl --with-included-apr
make && make install
3. Virtualmin Apache VirtualHost Integration
To route PHP requests from Virtualmin virtual hosts to their respective PHP-FPM pools via mod_proxy_fcgi, configure the VirtualHost directive:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /home/example/public_html
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/home/example/public_html/$1
<Directory /home/example/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This allows each virtual server in Virtualmin to leverage isolated PHP-FPM user pools with event-driven concurrency.
Thanks for the steps and confirming this works.
ReplyDeleteIs there any reason why not to just overwrite the existing copy of apache so it can be managed directly by virtualmin/webmin?
Since you use /opt/ I assume this was originally done in debian and then you converted the script to centos.
The version of default apache package on debian and centos is 2.2 which doesn't support tcp/ip fcgi to work with php-fpm.
Delete2.4.x introduces new mod_proxy_fcgi so it can contact php-fpm pools directly.
I've installed httpd 2.4.x in /opt folder ( no matter what os ) and integrated it to virtualmin in step #3 so you'll be able to manage it directly using both webmin/virtualmin.
Please note that using PHP-FPM with TCP/IP or a socket with too open permissions is a severe security fuck-up waiting to happen.
ReplyDeleteYou're right ...
DeleteI've asked apache developers for socket connection fpm support :
http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html
See comments ...
Great Tutorial,
ReplyDeleteBut there is a problem with /script/php-fpm it show an error when it come to the step of applying configuration to existing domains, it show:
/script/php-fpm: 18: /script/php-fpm: [[: not found
/script/php-fpm: 19: /script/php-fpm: [[: not found
/script/php-fpm: 48: /script/php-fpm: [[: not found
/script/php-fpm: 49: /script/php-fpm: [[: not found
thanx for your help
I think you need to ask the question on a linux forum, I've tested the script on centos and debian.
Delete'[[' is something like 'if statement' but I don't have any idea why it's not working for you !
I changed #/bin/sh to #/bin/bash and this is fixed.
DeleteAny chance of getting the php-fpm script somewhere? Can't download it anymore
ReplyDeleteI attached it to the post.
DeleteI cannot get scripts to install in virtualmin following this guide: "This script cannot be installed, as this virtual server does not meet its requirements : Could not work out exact PHP version" I followed the part about script install.. any suggestions.. everything else seems fine but I really like to fix this part?
ReplyDeleteI updated the post to fix this.
DeleteI see /opt/apache2/conf/httpd.conf for editing conf file first but then it is /opt/apache24/conf/httpd.conf. Is this a typo?
ReplyDeleteI cannot find /opt/apache24/conf/httpd.conf
I corrected the typo! thanks.
DeleteWhat directory should we mkdir /script? root? src? init.d?
ReplyDelete/script should be on / directory.
DeleteThanks a lot for guide. I think we should keep error log location same.
ReplyDeleteI am going to give this a go with Centos 7 so Apache 2.4 should already be there and good to go. I am thinking I can just skip step 3 and change the file path to httpd.conf where needed.
ReplyDeleteHello, I am going to try this on Ubuntu 16.04 lts.
ReplyDeleteI notice you don't turn off mpm-prefork/mod-php, and turn on mpm-worker (or mpm-event)? Why? You would significantly cut down on your Apache overhead...
You are right, Ive now updated this post and replaced mpm_prefork with mpm_event.
Delete