How to execute .pl script even if my browser trying to download it? - apache

I've got simple script. I already test it in commandline. Everything is fine.
But my browser cannot execute it. It's always trying to download it. I've cheked all apache configuration file,but i can't find any troubles.
/etc/apache2/sites-available looks like this:
ScriptAlias /cgi-bin/ /var/www/president/cgi
<Directory "/cgi-bin/ /var/www/president/cgi">
AddHandler cgi-script cgi pl
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
script is very simple
#!/usr/bin/perl -w
use CGI;
print "Content-type: text/html\n\n";
foreach (sort keys %ENV) {
print "<i>$_</i>: $ENV{$_}<br/>\n";
}

You should:
check the path to the perl
check the shebang, e.g. #!/path/perl
chmod 755 /var/www/president/cgi/myscriptname.pl, and
check your error_log! ;)
but probably the problem is
AddHandler cgi-script cgi pl
this should be
AddHandler cgi-script .cgi .pl
^ ^ dots here
You also has a wrong <directory... specs..
The section:
<Directory "/var/www/president/cgi">
AddHandler cgi-script .cgi .pl
And/or check the line such:
LoadModule cgi_module lib/apache2/modules/mod_cgi.so
Ps: Unrelated, but still a good advice, please read something about the Plack & PSGI. Could speed up your perl-web-app development many-many-many (really MANY) times. (you can live without such problems) :)

Related

apache error 403 forbidden - qgis

I just Install Apache 2.2.14(win32) mod_fcgid/2.3.6 on Windows 10 x64
this is part of my httpd.conf
#the path below is correct!
include "C:\OSGeo4W/httpd.d/httpd_qgis.conf"
and this is https_qgis.conf
AddHandler cgi-script .cgi .pl .exe
LoadModule fcgid_module modules/mod_fcgid.so
DefaultInitEnv PATH "C:\OSGeo4W\bin;C:\OSGeo4W\apps\qgis\bin;C:\OSGeo4W\apps\grass\grass-#grassversion#\bin;C:\OSGeo4W\apps\grass\grass-#grassversion#\lib;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem"
DefaultInitEnv QGIS_PREFIX_PATH "C:\OSGeo4W\apps\qgis"
DefaultInitEnv QT_PLUGIN_PATH "C:\OSGeo4W\apps\qgis\qtplugins;C:\OSGeo4W\apps\qt4\plugins"
DefaultInitEnv TEMP "C:\Users\admin\AppData\Local\Temp"
DefaultInitEnv PYTHONHOME "C:\OSGeo4W\apps\Python27"
ScriptAlias /cgi-bin/ "C:\OSGeo4W/bin/"
ScriptAlias /qgis/ C:\OSGeo4W/apps/qgis/bin/
#ScriptAlias /cgi-bin/ C:\OSGeo4W/apps/qgis/bin/
<Directory "C:\OSGeo4W/apps/qgis/bin/">
SetHandler fcgid-script
Options ExecCGI
#Order allow,deny
#Allow from all
Require all granted
</Directory>
Apache start correctly, without errors, but if I try to browse this address(for test):
http://localhost/qgis/qgis_mapserv.fcgi.exe?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetCapabilities
I have this error 403:
You don't have permission to access /qgis/qgis_mapserv.fcgi.exe on this server.
How can I resolve ?!
The path I write above, are correct !
Seems like you can't write files on at the address. Are you running your computer as an administrator? You can check if you go to the address and right-click on the folder, go to properties and see if the box "read-only" is checked. In the security tab you can change this if you have the permissions. Hope this helps

Testing CGI code on MacOS Sierra

I have some CGI code written in C that I've developed on an Ubuntu linux machine. It works fine, but I wanted to make some changes and update it while away on travel. Thought I'd set it up to test on my Mac laptop (macOS 10.12.1) using Xcode and Safari. Tried looking online for instructions on how to get apache to recognize the CGI code and found a few sites, but it still doesn't work.
Here's what I have so far:
I set up a directory /User/username/Sites to store the html pages that will call the CGI code through a form. (where "username" is my username)
I put the compiled CGI code in /Library/WebServer/CGI-Executables (Note: I also put them in /Users/username/Sites just in case)
Following the online instructions, in /etc/apache2/httpd.conf I uncommented the following lines:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
AddHandler cgi-script .cgi
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
Include /private/etc/apache2/extra/httpd-userdir.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf
Also following the online instructions, I created the following file in /etc/apache2/users called username.conf (again, "username" is my username).
<Directory "/Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
When done with the above changes I restarted the apache server (also tried a full restart of the computer, but it didn't help).
I'm able to load the form page using http://localhost/~username/foo.html, but when I submit the form using the POST method to a code foo.cgi it just spits foo.cgi back at me and then safari dumps it in the download folder. Also tried writing a simple perl script and a simple cgi C code to just make a "hello world" web page and called it with http://localhost/~username/hello.pl (or .cgi). This gave me the same results, though for the perl script it spit the script itself back since it's just plain text.
There must be a step I'm missing, but haven't been able to find it. Any help would be greatly appreciated. Thanks!
DC
One step left out of the instructions I found online: need to also uncomment the line:
LoadModule cgi_module libexec/apache2/mod_cgi.so
in the httpd.conf file. Works now by putting the executables in the /Library/WebServer/CGI-Executables folder.
Besides all you did, I also added ExecCGI Includes to the Options line at the /etc/apache2/users/username.conf file. Restarted and worked. Config file looks like this:
<Directory "/Users/username/Sites/">
AllowOverride All
Options Indexes MultiViews FollowSymLinks ExecCGI Includes
Require all granted
</Directory>

How to Run perl script through URL in Ubuntu

I would like to run perl as CGI . i.e Want to run it in localhost rather than command prompt.
I have installed perl . I have kept my file at /var/www/cgi-bin/
and i have made following changes in /etc/apache2/apche2.conf
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin/>
Options ExecCGI
AddHandler cgi-script cgi pl
</Directory>
Its giving me 404 not found error.
Following is the url I am accessing:
http://localhost/cgi-bin/testscript.pl
If your file is at /var/www/cgi-bin/, and your configuration says:
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
Then I'd say that the problem is pretty obvious. /var/www/cgi-bin/ and /home/www/cgi-bin/ are not the same directory.
Update: Ok. So you say that you have fixed the value of ScriptAlias and it still didn't work. Assuming that you're still getting the 404 error, then the next obvious step is to look in your error log and see exactly which file Apache is looking for. There's probably a mismatch between how Apache is configures to map between URLs and files and how you think it is configured.
When you get that error message, add it to your question.

Apache: /cgi-bin/my.cgi was not found on this server

I'm struggling to get my server to recognize cgi-bin in a URL.
If I place my cgi script in any site's root folder, it works perfectly without including cgi-bin in the URL. So my server is processing cgi correctly. If I attempt to use any cgi-bin configuration, it results in a 404 error.
I'm running it on a Mac server with OS 10.10. Ideally I would like to locate the cgi script in /Library/WebServer/CGI-Executables and address it as cgi-bin: Some.com/cgi-bin/my.cgi?yaddayadda .
In pursuit of that, I've edited Apache's httpd.conf file. It was already set with that as the path but resulted in the 404. Currently httpd.conf is:
# "/Library/WebServer/CGI-Executables" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
ScriptAlias /cgi-bin/ /Library/WebServer/CGI-Executables/
<Directory "/Library/WebServer/CGI-Executables">
AllowOverride None
Options +ExecCGI
AddHandler cgi-script .cgi .wsgi
</Directory>
# To allow CGI program execution for any file ending in .cgi in
# users' directories, you can use the following configuration.
#ScriptAlias /cgi-bin/ /home/*
# <Directory /home/*>
# Options +ExecCGI
# AddHandler cgi-script .cgi
# </Directory>
# If you wish designate a cgi-bin subdirectory of a user's directory
# where everything will be treated as a CGI program, you can use the following.
# ScriptAlias /cgi-bin/ /home/cgi-bin/
# <Directory /home/cgi-bin>
# AllowOverride None
# Options +ExecCGI
# AddHandler cgi-script .cgi
# </Directory>
As this shows, I've tried using a cgi-bin folder within the site folders as well. I've also added the .wsgi handler and gotten the same results with a Python Hello World. It too works from the root folder of any site. I just can't get it to refer to what I want as the cgi-bin. Even if I nest a cgi-bin folder inside CGI-Executables. I always get "The requested URL /cgi-bin/my.cgi was not found on this server."
I do restart Apache after every edit and have rebooted the whole machine several times. Nothing changes.
Unless I'm doing something wrong in my edits above, it's almost as if Apache is ignoring the httpd.conf. I don't know how to confirm that the file is being read when Apache starts. There is no other httpd.conf file located anywhere that should matter and those typically bear names that suggest they're from the past: httpd.conf.pre-update and httpd.conf-previous.
My best guess is that I have some error in the path or aliased path. I have tried every variation on them that I can come up with. The full path to my desired cgi-bin is: Server HD2/Library/WebServer/CGI-Executables. Or a cgi-bin folder within a site's root folder would be: Server HD2/Library/WebServer/Documents/TheSite/cgi-bin
I've also checked for any .htaccess files that could be overriding my edits. None found.
Thanks in advance for any suggestions.

Permission denied: when I try to run a perl cgi program. Apache Server

It's wired, but I can't figure out what the problem is. I think it was on apache2 server configuration.
I'm using Ubuntu 13.04 server, and it worked well when I tried the test perl script.
Such as:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello World\n";
exit;
However, When I tried to run a perl script by using CGI module:
#!/usr/lib/perl
use CGI;
$cgi = new CGI;
print $cgi->header,
$cgi->start_html(-title=>'My Page Title'),
$cgi->h1('My page Header'),
$cgi->end_html;
exit;
it cannot work. The log file is:
(13)Permission denied: exec of '/ftphome/uploads/cgi-bin/36_CGI_GenerateHTML.cgi' failed
Premature end of script headers: 36_CGI_GenerateHTML.cgi
and Here is my apache2 configure file:
DocumentRoot /ftphome/uploads/public_html/
<Directory /ftphome/uploads/public_html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /ftphome/uploads/cgi-bin/
<Directory "/ftphome/uploads/cgi-bin/">
AllowOverride All
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
Can anyone tell me where is the problem? and how to fix it? Thanks!
ps: All files has been changed permission to 755. In addition, I also change the permission to 775, 777, 555, but no one works.
Answer:
Thanks for the advice from #Miller and #Borodin. Finally I found it's my fault.
The first line of code should be #!/usr/bin/perl, but I wrote to #!/usr/lib/perl. Not sure why it will alert for permission denied.