I recently installed Apache 2.4 on my local machine, together with PHP 5.4.8 using PHP-FPM.
Everything went quite smoothly (after a while...) but there is still a strange error:
I configured Apache for PHP-FPM like this:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/apfelbox/WebServer"
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1
</VirtualHost>
It works, for example if I call http://localhost/info.php
I get the correct phpinfo()
(it is just a test file).
If I call a directory however, I get a 404 with body File not found.
and in the error log:
[Tue Nov 20 21:27:25.191625 2012] [proxy_fcgi:error] [pid 28997] [client ::1:57204] AH01071: Got error 'Primary script unknown\n'
Update
I now tried doing the proxying with mod_rewrite:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/apfelbox/WebServer"
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [L,P]
</VirtualHost>
But the problem is: it is always redirecting, because on http://localhost/
automatically http://localhost/index.php
is requested, because of
DirectoryIndex index.php index.html
Update 2
Ok, so I think "maybe check whether there is a file to give to the proxy first:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/apfelbox/WebServer"
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [L,P]
</VirtualHost>
Now the complete rewriting does not work anymore...
Update 3
Now I have this solution:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Users/apfelbox/WebServer"
RewriteEngine on
RewriteCond /Users/apfelbox/WebServer/%{REQUEST_FILENAME} -f
RewriteRule ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/Users/apfelbox/WebServer/$1 [L,P]
</VirtualHost>
First check, that there is a file to pass to PHP-FPM (with the full and absolute path) and then do the rewriting.
This does not work when using URL rewriting inside a subdirectory, also it fails for URLs like http://localhost/index.php/test/
So back to square one.
Any ideas?
After hours of searching and reading Apache documentation I've come up with a solution that allows to use the pool, and also allow the Rewrite directive in .htaccess to work even when the url contains .php files.
As per Apache documentation, the SetHandler proxy parameter requires Apache HTTP Server 2.4.10.
I hope that this solution will help you too.
I ran into this problem yesterday as well – Apache 2.4 moved out of Debian/experimental into Debian/unstable forcing me to deal with this new stuff; not on our production servers of course ;).
After reading what feels like millions of sites, Apache docs, bug reports and debugging output in the error log I finally got it to work. No, there's no support for FPM with sockets, yet. The default Debian config has been using sockets for some time now, so Debian users will have to change that too.
Here's what works for a CakePHP site and PHPMyAdmin (the latter needs some config if you're using the Debian packages though), so I can confirm that
mod_rewrite
still works as expected to do fancy URL rewriting.Notice
DirectoryIndex index.php
, which might be the reason none of your configs worked for "folders" (at least that's what didn't work here).I still get
File not found.
for directories, but only if there's no index file it can parse. Would love to get rid of that too, but it's not that critical as for now.The above vhost works perfectly well with an .htaccess in the root like this:
I don't quite get what you mean by
URL rewriting inside a subdirectory
though (I'm only rewriting to the root's index.php).(Oh, and you'll have to make sure Xdebug doesn't conflict with FPM on your system, out of the box they want to use the same ports.)
All you need to do is to set:
And do not forget to set the customer page by:
Yet another solution (requires Apache >= 2.4.10) - Inside the vhost:
So here, the fcgi handler for PHP will be set only if the file exists and if its name matches with PHP file extension.
BTW: For those which would have idea to set the ProxyErrorOverride to On, be aware that this is really a bad idea. Usage of this directive is not without causing any issue. For instance, any PHP application sending HTTP code such as 503 would lead to unexpected result. Default error handler would be involved in any cases and for PHP applications that provide API, that is really a bad behavior.
This is what I've got. It seems to work OK. I put Drupal in a subdirectory and its rewrites work, directory indexes work, and PATH_INFO works.
I've tried to do something like this without rewrites ("If" and such), but I couldn't get anything to work.
EDIT: Note that if you were to implement this as a shared hosting provider, this could be a security issue. It would allow users to pass PHP scripts to an arbitrary fcgi proxy. If you had a seperate pool for every user, that would allow for elevation of privilege attacks.
Best way to solve this is to turn on debugging logs for mod_proxy and mod_rewrite and php-fpm. In apache 2.4 you can now turn on debugging logs for specific modules only. http://httpd.apache.org/docs/current/mod/core.html#loglevel Per-module and per-directory configuration is available in Apache HTTP Server 2.3.6 and later
Maybe you're getting a double slash on directories?
Here's what I use and it works fine:
One thing I came across in my dealing with this issue, is that if you use the combination of:
In your fpm pool configuration, don't pass the full path to the
ProxyPass
directive.But -ONLY- if the pool on that port is chrooted.
I'm not sure if the problem is related, but i've found a partialy working solution here:
https://stackoverflow.com/questions/44054617/mod-rewrite-in-2-4-25-triggering-fcgi-primary-script-unknown-error-in-php-fpm
The trick seems to be adding a ? char in the .htaccess RewriteRule, eg using:
instead of:
The source of the problem seems to be a change in mod_rewrite of Apache 2.4.25. I've used Apache trace1 log level to observe a "loop" that pass $1 to php-fpm after index.php/$1 has been passed. The $1 generate the "AH01071: Got error 'Primary script unknown\n'" error.
Hope this little tidbit help someone to solve their problems.
A slightly modified version of @FrancescoA's answer that does not require mod_rewrite
Linode has a great tutorial on this subject
Basically you setup a handler for the entire server that will catch any php scripts and pass them to fast-cgi.