CGI script runned with ModPerl::Registry doesn't print result on html page, just download it - mod-perl2

Here is my sandbox https://github.com/pavelsr/mod_perl_sandbox
When I make docker-compose up then go to http://localhost:3000/env.pl resulted html is downloaded, not served.
Under CGI mode (with removed -DMODPERL in docker-compose) everything works fine.
Seems like ModPerl::Registry does not process Content-type: text/html
What I need to do fix in httpd.conf ?
Adding PerlOptions +ParseHeaders wil not fix the problem

Fixed. PerlOptions +ParseHeaders was inside <Location tag so was not working. To fix it you should move ParseHeaders to <Directory or set globally.
https://github.com/pavelsr/mod_perl_sandbox - work example of how to run same perl script under CGI and mod_perl

Related

Apache sends script from cgi-bin directory as plain text

This is on FreeBSD 11.3, with Apache 2.4 freshly installed via pkg.
As the minimal possible test of CGI, I'm trying to get the test-cgi script distributed as part of the Apache package for FreeBSD to run. (Serving static HTML pages works fine.)
I'm using the default directories, so the cgi-bin directory is /usr/local/www/apache24/cgi-bin. I have put a shebang for /bin/sh into the test-cgi file, and I have set the protection of the test-cgi file to 755. I can run the test-cgi file from the command line.
I have started Apache running.
I have checked that the config points the script alias directory to the right place:
ScriptAlias /cgi-bin/ "/usr/local/www/apache24/cgi-bin/"
I have checked the Directory block in the config for that directory and even tried adding some things (which didn't help):
<Directory "/usr/local/www/apache24/cgi-bin">
AllowOverride None
Options +ExecCGI
AddHandler cgi-script .cgi
Require all granted
</Directory>
When I put the URL for that script into my browser, I get the source file for the script as text, rather than the output from running the script. That source includes the shebang line I added, which confirms that the URL points to the file on the server that I expect it to.
I've read many articles, all of which suggest things I've checked and have correct. (I've been running Apache since I stopped running the NCSA web server, back in the day; but the last decade I haven't been doing clean installs on systems I have root on mostly, so I'm in danger of having out-of-date knowledge, sometimes worse than ignorance.)
No errors are logged when this happens. In the access log, I get a 304 error followed by a 200:
192.168.1.14 - - [15/Feb/2022:17:45:42 -0600] "GET /cgi-bin/test-cgi HTTP/1.1" 304 -
192.168.1.14 - - [15/Feb/2022:18:00:24 -0600] "GET /cgi-bin/test-cgi HTTP/1.1" 200 1269
That ought to mean something useful, I would think? The 304 is a "not modified" status, which should be returned on a conditional request. This is probably somehow relevant? If I update the file timestamp it doesn't change this, though.
This has got to be something really simple, perhaps even stupid, that I'm overlooking. Would somebody please point out what precise stupid thing it is? Thanks!!
Got it! In the default config the LoadModule commands for cgi_module and cgid_module were commented out.
Uncommenting them (only one is actually loaded due to surrounding <ifModule> code) got the CGI working normally.

Default route (controller/action) and links of css/js doesn't work cakephp

After extracting the cake 2.5.2 (on Linux) to the directory /var/www/html/teste when accessing the url http://localhost/teste it correctly loads the contents of the controller pages and action display as from routes.php:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
But when trying to access the controller directly through url:
http://localhost/teste/pages/display I have the return of 404 Page not found.
Otherwise, the files css/js/img the webroot folder (not being included for the url generated) are not loaded.
Url generated that returns file not found:
http://localhost/teste/css/cake.generic.css
Url to access the file correctly:
http://localhost/teste/app/webroot/css/cake.generic.css
My solution to the files css/js/img was the following, I changed the form to call the file.
Standard form used:
echo $this->Html->css('cake.generic');
New form:
echo $this->Html->css('/app/webroot/css/cake.generic.css');
In short, I think the error might actually be in apache or something, but do not know exactly how can I validate that part.
My solution to the css / js / img so is it correct? Because I think that was to work as the example I downloaded the version of the cake properly.
If they need more information just tell me, thanks.
This is a symptom of an environment with mod_rewrite disabled.
If you use Ubuntu, just type:
sudo a2enmod rewrite
In your /etc/apache2/apache2.conf find the a block similar to this one:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
And replace the line
AllowOverride None
With
AllowOverride All
And finally, restart your apache with:
sudo service apache2 restart

Apache expanding cgi output with ssi

I use Apache to serve web pages using both cgi and ssi (server side include). I even use them both in some cases, that is, the output of the cgi program is further expanded using ssi to generate the final html delivered to the client.
The system works fine on my commercial web host and did work on the Apache installed on my personal computer. But I recently upgraded to Ubuntu 14.04 Trusty Tahr (64 bit) with Debian Apache 2.4.7. Both cgi and ssi work, but only individually. The attempt to expand cgi output further with ssi fails, and the raw output of the cgi program gets delivered to the client.
Suggestions for tweaking the system to work will be appreciated. Another helpful answer would be a short working example, under a hundred lines, of a document that expands cgi output with ssi on the Ubuntu configuration above.
A few things worth checking:
Run apache2ctl -M in the Ubuntu terminal and make sure you have cgi_module and include_module enabled.
Open your website configuration file for Apache and:
make sure that under the Options directive, you have ExecCGI, Includes and not IncludesNOEXEC
check that for the directories which have your CGI scripts, you have SetHandler cgi-script set, or are using the ScriptAlias directive.
As far as I know, it's not possible to run both SSI and CGI on the same source file. One can pre-generate the other before run time (like a pre-proccessor), or they can 'include' each other at run time as separate files.
SSI options for including CGI:
<!--#include virtual="/cgi-bin/example.cgi?argument=value" -->
<!--#exec cgi="/cgi-bin/example.cgi" -->
<!--#exec cmd="perl /path/to/perlscript arg1 arg2" -->
I hope something there helps.
Add this to your .htaccess file:
<Files ~ ".ssicgi$">
Options +SymLinksIfOwnerMatch +Includes
AddOutputFilter INCLUDES .ssicgi
</Files>
rename the cgi scripts whose output you want parsed for cgi to the extension ".ssicgi" and rename any calls to the original scripts so it calls the filtered versions.
Make sure to upvote and accept the answer if this solves it for you.

How can I rewrite URLs in XML with Apache 2.4?

Apache 2.4 includes mod_proxy_html and that's great, it's catching all kinds of URLs inside the HTML coming back from the server and fixing them. But I've got a Seam app that sends back text/xml files to the client sometimes with fully qualified URLs that also need to be rewritten and mod_proxy_html doesn't fix them.
Apparently there was a mod_proxy_xml that used to exist separately from mod_proxy_html but Apache didn't include that. Is there a way to get mod_proxy_html configured to do the same thing? I need it to fix URLs in both the HTML and XML files coming back from a server.
Follow up:
I continue to fight with this and I've tried a few different solutions with no success including using mod_substitute (which somehow I'm configuring incorrectly because it never seems to substitute anything for anything) and using the force flag mod_proxy_html has to try and force it to do all files under a certain path.
This is an old question, but I just faced the same issue.
I tried with mod_proxy_html, compiled mod_proxy_xml, nothing worked.
#JonLin's suggestion is spot on, it works with mod_sed.
The only if is mod_sed is documented to work inside Directory nodes.
If you declare a Location though and do a SetOutputFilter instead of AddOutputFilter (which requires a mime type) it works beautifully.
The config that works is:
<Location "/">
SetOutputFilter Sed
OutputSed "s,http://internal:80,https://external.com,g"
</Location>

How do I serve a script with apache instead of running it?

I'm trying to host a python script using an apache web server, but the server tries to run the script instead of just offering it for download.
I do not have direct access to server, and adding the line
AddType text/plain .py
to .htaccess in the root folder does not appear to work, though I could be doing something wrong.
How do I get the server to just send the file as text instead of trying to run it?
-Edit
Changing the name does not work. Script.py.safe still give a 500 Server error when you click it.
I should also mention that the .htaccess file does work, but for some reason that one addType line is not working. Either because it's not overriding something, or the line is wrong.
In your .htaccess:
RemoveHandler .py
If you can't change the Apache config and you can't override it with an htaccess file, then it seems to me that the easiest solutions would be either to change the file extension, or else to write a script that prints the contents of the target script.
Both are hacks to some extent, but the correct solution is to change the Apache config.
One option is to change the extention and make clear that it should be renamed. IE python.py.safe or python.py.dl. The user would then need to remove the extra bit.
You could also Zip it up.
I would write something that loads the python script up. This way you could even get energetic and include formatting and styling of the code. You could even write it in python since you will not have precluded that by file extension.
<IfModule mime_module>
<Files *.py>
ForceType text/plain
</Files>
</IfModule>
in a .htaccess for the folder should work ;)