Make file extension to be interpreted by PHP
Asked Answered
G

6

21

I added this to my .htaccess file:

AddHandler fcgid-script .test

(I'm using FastCGI / VirtualMin / WebMin)

And crated a test.test file with the contents;

<?php echo "test"; ?>

This results in an internal server error and this message in the error log:

[Thu Apr 16 14:12:57.631287 2015] [fcgid:warn] [pid 2646] (104)Connection reset by peer: [client xxxx:53595] mod_fcgid: error reading data from FastCGI server
[Thu Apr 16 14:12:57.631402 2015] [core:error] [pid 2646] [client xxxx:53595] End of script output before headers: test.test

What am I doing wrong? I tried using different handler namees like x-httpd-php or x-httpd-php5 but that doesn't do anything at all.

I also tried:

<FilesMatch "\.test$">
  SetHandler fcgid-script
</FilesMatch>

but it has the same internal server error.

Goodkin answered 17/4, 2015 at 6:26 Comment(3)
"End of script output before the headers" - maybe you have to set some headers to define file type?Jochbed
@Jochbed As I can see, there are 2 error messages in the log, and the second one ("End of script output before the headers") is probably only the consequence of the first one.Latty
cgi script must be an application ie first line must be #!/usr/bin/php (path to interpreter)Tammitammie
W
2

Tested and verified to work:

Because you mentioned you are using Webmin, I tailored my answer for Webmin. You can do this by editing Apache's configuration through shell as well.

I will note that it is generally preferable to edit Apache's configuration, and in this case probably unavoidable, as I do not believe you can make this sort of edit in .htaccess. Although there may be a hack I'm unfamiliar with, a server admin would probably want to keep this locked inside of apache configuration. Thoughts of .htaccess files with rogue file extensions flying around on a per-directory basis not only sounds slow, but also highly insecure.

Steps

I installed Apache on Ubuntu 14.04 through VirtualBox. I added Webmin and FastCGI.

I took the following steps:

  1. Log into Webmin, go to your Virtual Host.
  2. Click on Edit Directives
  3. Add the following at the bottom:

<Directory />
    #Your SetHandler script here 
    #(@MrTux's script works here but NOT IN .htaccess)
    
    Require all granted
</Directory>
  1. Click Save
  2. Click "Apply Changes" at the top.
  3. You're done!

I will note that SetHandler passes some headers, so if you declare this in .htaccess there is a chance that headers have already been sent which is what is triggering your error. Again, .htaccess is not the preferred way to set rules if you have control over your system. In this case it would probably cause security issues anyway.

Warthog answered 26/4, 2015 at 7:44 Comment(1)
IIRC application/x-httpd-php oinly works with mod_php, see httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples as an example for mod_fastcgidBruce
B
7

It seems as if the FastCGId module doesn't know how to execute your script - the FcgidWrapper directive seems to be missing (as your PHP script doesn't have a Shebang).

Depending on your distribution you have to set it up manually or you can adjust the existing directive which already handles .php files to also handle .test files (Usually this should be done using the "SetHandler fcgid-script" directive and setting/duplicating the FcgidWrapper directive).

Adjusted example (taken from https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples):

<FilesMatch "\.test$">
    AddHandler fcgid-script .test
</FilesMatch>
Options +ExecCGI
FcgidWrapper /usr/bin/php5-cgi .test

More information and examples: https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples and http://tltech.com/info/php-on-fcgid/

Other problems could be based on permissions, e.g. if you're using suexec that the fastcgid process cannot write to the logs or cannot execute the wrapper script. Here it would be really helpful if you could state if normal .php files are executed properly.

Bruce answered 19/4, 2015 at 11:39 Comment(1)
Yes, normal PHP files (using FCGI) work just fine. I'm using the default config provided by webmin / virtualmin when you add a site.Goodkin
H
2

Try to use this in your htaccess file

AddType application/x-httpd-php .test

See this Stack Overflow question: Problem with Executing CGI Scripts on Apache.

Herrick answered 20/4, 2015 at 11:5 Comment(2)
You should be aware that this method is insecure, as Apache will match .test as well as anything that has .test in its name (i.e. .test.bad). This is why <FilesMatch> is superior, because it uses a regex to ensure only the specified files are executed.Serpentiform
IIRC application/x-httpd-php oinly works with mod_php, see httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples as an example for mod_fastcgidBruce
W
2

Tested and verified to work:

Because you mentioned you are using Webmin, I tailored my answer for Webmin. You can do this by editing Apache's configuration through shell as well.

I will note that it is generally preferable to edit Apache's configuration, and in this case probably unavoidable, as I do not believe you can make this sort of edit in .htaccess. Although there may be a hack I'm unfamiliar with, a server admin would probably want to keep this locked inside of apache configuration. Thoughts of .htaccess files with rogue file extensions flying around on a per-directory basis not only sounds slow, but also highly insecure.

Steps

I installed Apache on Ubuntu 14.04 through VirtualBox. I added Webmin and FastCGI.

I took the following steps:

  1. Log into Webmin, go to your Virtual Host.
  2. Click on Edit Directives
  3. Add the following at the bottom:

<Directory />
    #Your SetHandler script here 
    #(@MrTux's script works here but NOT IN .htaccess)
    
    Require all granted
</Directory>
  1. Click Save
  2. Click "Apply Changes" at the top.
  3. You're done!

I will note that SetHandler passes some headers, so if you declare this in .htaccess there is a chance that headers have already been sent which is what is triggering your error. Again, .htaccess is not the preferred way to set rules if you have control over your system. In this case it would probably cause security issues anyway.

Warthog answered 26/4, 2015 at 7:44 Comment(1)
IIRC application/x-httpd-php oinly works with mod_php, see httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples as an example for mod_fastcgidBruce
M
1

I'm not a fastcgi guy. I googled the issue. I got 2 similar issues posted in virtualmin. I'm just appending the answer posted there.

https://www.virtualmin.com/node/34903

  1. go to virtualmin\your virtual server\server configuration\website options\
  2. select "Apache mod_php (run as Apache's user)" then save
  3. switch back "FCGId (run as virtual server owner)"(it will request you Run CGI scripts as domain owner)
  4. Save

https://www.virtualmin.com/node/25539

Go to /home/sitename/fcgi-bin Use the follpwoing command to reset the immutable flag

"chattr -i php5.fcgi"

Then delete the /home/sitename/etc and /home/sitename/fcgi-bin directories (making sure that anything that's not php related in etc is saved) Using virtualmin switch back to apache mod_php mode and then back to FCGId mode. The site should now work Dont forget to chown any files/directories in public_html that were apache to : Note that these are both usually the sitename.

Hope it will help you.

Morrow answered 20/4, 2015 at 12:46 Comment(0)
S
0

I question the fcgid-script handler. That's not a normal PHP handler, even for FCGI. Normally you would see

<FilesMatch "\.test$">
     SetHandler application/x-httpd-php
</FilesMatch>

Or

<FilesMatch "\.test$">
     SetHandler php5-fcgi
</FilesMatch>

If neither of those works, check to make sure PHP FCGI is installed.

Serpentiform answered 24/4, 2015 at 23:38 Comment(1)
IIRC application/x-httpd-php only works with mod_php, see httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#examples as an example for mod_fastcgidBruce
S
0

All CGI scripts requires that you add the Shebang line at the top of the script.

So if you want to run PHP you should have included something like:

#!/path/to/php

At the top of your script to make it work

Starofbethlehem answered 25/4, 2015 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.