Topic: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory w/ different ProxyPass ... how?



It seems like I have tried everything that I could think of. I tried putting a sub directory Directory directive in, with a secondary Proxy Pass, with no luck.



  ProxyPass fcgi://127.0.0.1:9071/home/exampledomain/public_html$1 (PHP 7.1.2)

  ProxyPass fcgi://127.0.0.1:9056/home/exampledomain/public_html$1 (PHP 5.6)



I need to use PHP 5.6 on a sub directory. Does anyone know how I can achieve this?



# VIRTUAL SERVER #
# TLD: exampledomain.com #
# Username: exampledomain #

# SSL #

<VirtualHost 10.1.1.100:443>
ServerName exampledomain.com
ServerAlias exampledomain.com
DocumentRoot /home/exampledomain/public_html
ErrorLog /home/exampledomain/exampledomain.com_ssl_error_log
CustomLog /home/exampledomain/exampledomain.com_ssl_access_log combined
ScriptAlias /cgi-bin/ /home/exampledomain/cgi-bin/
DirectoryIndex index.html index.php index.php4 index.php5 index.htm
<Directory /home/exampledomain/public_html>
Options -Indexes +FollowSymlinks
#allow from all
#AllowOverride None Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AllowOverride All
Require all granted
</Directory>
<Directory /home/exampledomain/cgi-bin>
#allow from all
#AllowOverride None Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =www.exampledomain.com
RewriteRule ^(.*) https://exampledomain.com/ [R]
SSLEngine on
SSLCertificateFile /home/exampledomain/ssl.cert
SSLCertificateKeyFile /home/exampledomain/ssl.key
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
php_value memory_limit 64M
php_value suhosin.session.encrypt Off
<LocationMatch ^(.*\.php)$>
  ProxyPass fcgi://127.0.0.1:9071/home/exampledomain/public_html$1
</LocationMatch>
</VirtualHost>

Thanks in advance!

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Which Distribution / Version ?

With Apache 2.4 it's still much more simpler to use SetHandler
With Apache 2.2 it's a bit more complex, and you need mod_proxy_fcgi from EPEL (on ProxyPass*)

See the various posts on my blog about this.
* https://blog.remirepo.net/post/2012/04/ … -HTTPD-2.4   (ProxyPassMatch example)
* https://blog.remirepo.net/post/2014/03/ … mprovement
* https://blog.remirepo.net/post/2016/04/ … orkstation (multi-vhosts, 1 php version per vhost)

BTW, ProxyPassMatch should work (without LocationMatch)

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 26 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.9 x86_64 + EPEL + remi + remi-php70

3 (edited by phpninja 2017-02-22 16:51:05)

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

CentOs 7.3 (Latest) / Apache 2.4.6 (CentOS)

Removing LocationMatch would allow dual ProxyPass lines with different locations? How do I distinguish one for another since I can't put them inside Directory directives?

Do you know of an example

/public_html (PHP 7.1)
/public_html/forum/ (PHP 5.6)

I used LocationMatch due to troubleshooting and ran into that solution... which worked with re-write. So far in the root / is a working Joomla , and /blog is a working WordPress (so the LocationMatch block with the fastcgi pass line is working great!) ... it's just when getting to /forum (which doesn't work with PHP 7) is where i'm thrown completely off as to how to tackle this...

Thanks!

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

As you use apache 2.4, definitively better to use SetHandler

So, as shown in blog posts

<FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9071"
</FilesMatch>
Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 26 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.9 x86_64 + EPEL + remi + remi-php70

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Okay, good to know. How can I can restrict SetHandler for specific directories? Can I use multiple <FilesMatch> directives? Can FilesMatch be included within a Directory directive that you know of?

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Yes, FilesMatch can be in a Directory, and of course multiple FilesMatch can be used.

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 26 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.9 x86_64 + EPEL + remi + remi-php70

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Also notice that ProxyPassMatch allow to set both regex on URL, and target (so doesn't need the <Location>)

ProxyPassMatch ^/foo/(.*\.php)$ fcgi://127.0.0.1:9000/some/place/$1
Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 26 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.9 x86_64 + EPEL + remi + remi-php70

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

I just tried the following (which loads properly in apache2) but doesn't actually use PHP 5.6 on /forum

<LocationMatch ^(.*\.php)$>
  ProxyPass fcgi://127.0.0.1:9071/home/exampledomain/public_html$1
</LocationMatch>
<Directory /home/exampledomain/public_html/forum>
<FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9056"
</FilesMatch>
</Directory>
</VirtualHost>

9 (edited by phpninja 2017-02-22 17:50:19)

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

@Remi , Thanks a lot for your help! Much ablidged! Does ProxyPassMatch require the FilesMatch, or is this an alternative solution perhaps?

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9071/home/exampledomain/public_html/$1
(Sub-Directory)
ProxyPassMatch ^/forum/(.*\.php)$ fcgi://127.0.0.1:9056/home/exampledomain/public_html/forum/$1

So would I just use those above lines stand-alone? Or do I need to include those in separate Directory directives?

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Just tried the following:

(Loads fine... but now still showing /forum/ is using 7.1.2 vs. 5.6)

<Directory /home/exampledomain/public_html>
Options -Indexes +FollowSymlinks
#allow from all
#AllowOverride None Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AllowOverride All
Require all granted
<FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9071"
</FilesMatch>
</Directory>
<Directory /home/exampledomain/cgi-bin>
#allow from all
#AllowOverride None Options=ExecCGI,Includes,IncludesNOEXEC,Indexes,MultiViews,SymLinksIfOwnerMatch
AllowOverride All
Require all granted
</Directory>
RewriteEngine on
RewriteCond %{HTTP_HOST} =www.exampledomain.com
RewriteRule ^(.*) https://exampledomain.com/ [R]
SSLEngine on
SSLCertificateFile /home/exampledomain/ssl.cert
SSLCertificateKeyFile /home/exampledomain/ssl.key
SSLProtocol all -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
php_value memory_limit 64M
php_value suhosin.session.encrypt Off

<Directory /home/exampledomain/public_html/forum>
<FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9056"
</FilesMatch>
</Directory>
</VirtualHost>

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Actually... site wide i'm getting "File not found"

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

Don't know, you need to try, perhaps changing the order of files directives ?

Without <Directory>, i.e. <FilesMatch "^/home/exampledomain/public_html/forum/.*\.ĥphp$">

etc...

And reading Apache documenation => http://httpd.apache.org/docs/2.4/en/sections.html

Desktop: Fedora 25 x86_64 + rpmfusion + remi-test + remi-dev
Laptop:  Fedora 26 x86_64 + remi (SCL only)
Hosting Server: CentOS 6.9 x86_64 + EPEL + remi + remi-php70

13 (edited by phpninja 2017-02-22 19:12:03)

Re: Apache2 VirtualHost Block -> ProxyPass fastcgi PHP-FPM + sub-directory

[SOLVED] The Solution was this:

under </Directory> , i added 2 of these (all the others, SetHandler, etc, don't seem to work with my setup, which has multiple PHP Versions w/ Multiple PHP-FPM's ... but this is what worked.

Directly under your main </Directory>

add this block (Top one is Everything , otherwise specificed) and the second block is a designated sub directory with a custom PHP-FPM pass.

<LocationMatch ^(.*\.php)$>
  ProxyPass fcgi://127.0.0.1:9071/home/exampledomain/public_html$1
</LocationMatch>
<LocationMatch ^/forum(.*\.php)$>
  ProxyPass fcgi://127.0.0.1:9056/home/exampledomain/public_html/forum$1
</LocationMatch>

Thanks for the help!