I was looking for working config for the combination of apache mod_fastcgi and PHP-FPM and could not find any. I mean there were plenty of examples and discussions but none of them were operational. The closest was the article on server-world.info site.
Based on that information I was able to put together working configuration for multiple virtual hosts.
- Initial assumption – we have Centos with installed stock httpd, mod_fastcgi (from rpmforge repository) and php54-fpm as part of IUS repository packaged php-5.4 modules
- Apache configured as prefork, php-fpm configured with 3 process pools on TCP ports (while unix socket considered by many as fast way of communication with FastCGI server it is known with data loss under heavy load. So we will configure port 9000 – default, PHP process user/group apache/apache. 9001 – domainA, user/group userA/groupA correspondingly, 9002 – domainB, userB/groupB
- default pool configuration
1234567891011121314151617181920212223242526272829303132333435[www]listen = 127.0.0.1:9000listen.allowed_clients = 127.0.0.1;listen=/var/run/php-www.sock;listen.backlog = -1;listen.owner = web;listen.group = www;listen.mode = 0666user = apachegroup = apachepm.status_path = /fpm-statuspm = dynamicpm.max_children = 20pm.start_servers = 4pm.min_spare_servers = 4pm.max_spare_servers = 10pm.max_requests = 500request_terminate_timeout = 600srlimit_files = 131072rlimit_core = unlimitedcatch_workers_output = yes; environmentenv[TMP]=/dev/shmenv[TEMP]=/dev/shm; admin flagsphp_admin_flag[log_errors] = onphp_admin_value[error_log] = /var/log/httpd/fpm-error.log;php_flag[display_errors] = on;php_flag[display_startup_errors] = onphp_admin_value[memory_limit] = 128Mphp_value[session.save_handler] = filesphp_value[session.save_path] = /dev/shmThe only 4 differences between this pool configuration and domainA and domainB pool configurations would be – pool name, port number, process user/group. Configure php-fpm pools for domainA and domainB with different pool names, port numbers and user/group accordingly.
- Now, apache /etc/httpd/conf.d/fastcgi.conf
1234567891011User apacheGroup apacheLoadModule fastcgi_module modules/mod_fastcgi.soFastCgiIpcDir /var/run/mod_fastcgiFastCgiConfig -idle-timeout 20 -maxClassProcesses 1AddType application/x-httpd-php .phpDirectoryIndex index.phpAddHandler php-fastcgi .phpAction php-fastcgi /php-fastcgiScriptAlias /php-fastcgi /var/www/cgi-bin/php-fastcgiFastCgiExternalServer /var/www/cgi-bin/php-fastcgi -host 127.0.0.1:9000 -pass-header Authorization
There are 4 important lines in it –
AddHandler, Action, ScriptAlias, FastCgiExternalServer
that will appear again in virtual host configuration. Directory configured in ScriptAlias has to exist.
Now you can restart php-fpm and httpd services for testing. - And the most interesting – virtual hosts domainA and domainB
12345678910111213141516171819202122232425262728293031<VirtualHost *:80>ServerName domainA.comServerAlias www.domainA.comDocumentRoot /home/domainA/public_htmlErrorLog /home/domainA/logs/error_logCustomLog /home/domainA/logs/access_log combinedScriptAlias /cgi-bin/ /home/domainA/cgi-bin/DirectoryIndex index.html index.htm index.php<IfModule mod_fastcgi.c>AddHandler php-domainA .phpAction php-domainA /php-domainAScriptAlias /php-domainA /var/www/cgi-bin/php-domainAFastCGIExternalServer /var/www/cgi-bin/php-domainA -host 127.0.0.1:9001 -pass-header Authorization</IfModule></VirtualHost><VirtualHost *:80>ServerName domainB.comServerAlias www.domainB.comDocumentRoot /home/domainB/public_htmlErrorLog /home/domainB/logs/error_logCustomLog /home/domainB/logs/access_log combinedScriptAlias /cgi-bin/ /home/domainB/cgi-bin/DirectoryIndex index.html index.htm index.php<IfModule mod_fastcgi.c>AddHandler php-domainB .phpAction php-domainB /php-domainBScriptAlias /php-domainB /var/www/cgi-bin/php-domainBFastCGIExternalServer /var/www/cgi-bin/php-domainB -host 127.0.0.1:9001 -pass-header Authorization</IfModule></VirtualHost>
The important point is that you have to have distinctive handler for .php for each virtual host – that is how you can associate virtual host to specific pool on PHP-FPM. Once I’ve figured that out the rest was easy.
0 Comments.