Execute a .cgi file in Ubuntu - perl

I am running Apache/PHP under Ubuntu
When I run the .cgi file, by going at http://localhost/mycgi.cgi, the browser will display the code instead of running it.
How do I make the browser execute the CGI file instead of showing its contents?

Add these lines to your apache2.conf file
<Directory /usr/local/apache2/htdocs/somedir>
Options +ExecCGI
</Directory>
AddHandler cgi-script .cgi .pl

Obviously, CGI execution is not set up properly. Did you try the tutorial?
Usually, all CGI scripts are put into a certain location such as cgi-bin. Only the files present in that folder are executed at all. Otherwise, they are treated as normal files and the web server simply returns them as source.
The tutorial shows you the alternatives that you can use to make the scripts being executed instead of just being returned as text files.

Make sure that the shebang line in your .cgi or .pl file is correct. The shebang line tells Apache where the Perl interpreter is (the Perl interpreter is the perl.exe file).
The Apache tutorial says:
Your first CGI program
The following is an example CGI program that prints one line to your browser. Type in the following, save it to a file called first.pl, and put it in your cgi-bin directory. #!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";
So here the shebang line is #!/usr/bin/perl
However, if perl.exe is located in a different folder, then #!/usr/bin/perl needs to be changed! This is true even for Windows users who, normally, can completely ignore and omit the shebang line. (Why the tutorial doesn't mention this fact, i don't know - i guess they didn't realise that some people always ignore the shebang line?)
For example, when I installed Perl it was automatically suggested that i install Perl in C:\Program Files (x86)\Perl64
Inside my Perl64 folder is folder called 'bin' and inside that is perl.exe
So i had to change the shebang line to: #!C:/Program Files (x86)/Perl64/bin/perl.exe
before i could get cgi scripts to work with Apache.

This foxed me on Amazon Ubuntu 14.04. I had a configuration that had 'worked' for a few years:
sudo a2enmod cgi
sudo service apache2 restart
Sorted this out for some existing Perl scripts with a .cgi extension and the existing virtual host configuration. Also wrong [not this problem but associated with this upgrade]:
virtual.host
needed to become
virtual.host.conf
before it was recognised as valid.

Related

How to set correct perl path

I have 2 versions of perl installed. perl v5.18.2 and v5.20.0 . But when I do perl -v I get perl v5.18.2. I don't need v5.18.2 at all. I need v5.20.0. How do I change the path to include v5.20.0 and not v5.18.2?
Here is my $PATH:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
v5.18.2 is installed at /usr/bin/perl and /usr/bin/perl5.18.2, and v5.20.0 at /root/perl5/perlbrew/perls/perl-5.20.0/bin/perl.
See the perlbrew documentation:
switch Permanently use the specified perl as default
perlbrew switch perl-5.20.0
If you haven't already, you will need to add source /root/perl5/perlbrew/etc/bashrc to your login script for this to work.
The following will add the desired build of Perl to the search path so that it's found first:
export PATH="/root/perl5/perlbrew/perls/perl-5.20.0/bin:$PATH"
You may add that to your login script to make this change permanent.
Note that you'll need to update the shebang (#!) lines of scripts installed with a different perl to the following:
#!/root/perl5/perlbrew/perls/perl-5.20.0/bin/perl
It looks like you have three copies of perl installed, as neither of the paths you mentioned are in the PATH variable yet your shell still finds one
There's no need for perlbrew. All you need to do is set your PATH variable on the command line
$ export PATH=/usr/bin/perl5.18.2:$PATH
If you want to make that change permanent then add the command to your profile file at ~/.profile

CGI Script not responding through Wamp

I have configure wamp for cgi scripts correctly but it is not running following code and giving following server error on executing.
Bad file descriptor: don't know how to spawn child process:
C:/wamp/www/New folder/hello.cgi, referer: http://localhost/New%20folder/
My active perl is installed on C:/wamp/www/perl
here is code:
#!C:\wamp\bin\perl.exe -w
print "Hello World.\n";
Windows does not pay attention to #! lines. You need to make sure that your file extension (.cgi in your case, or .pl more commonly) is associated with your perl executable in the registry.
More info:
There are two ways to run a perl program/script, one is to execute perl directly with the file name of the main program/script as a parameter:
C:\wamp\bin\perl.exe mydir\myprog.pl
Don't ever do this in the cgi directory of your web server.
The other way to execute a program is to just name the file to be run and depend on the OS's built in method to find the right program to run it.
mydir\myprog.pl
On a *nix OS, the 1st two bytes of the file are analyzed to determine what to do with it, if those two bytes are the equivalent of the ASCII string #! then the file is treated as text, & the rest of the 1st line is read with the expectation that it will contain the path to the file's interpreter.
On a Windows OS, the file extension is used to search the registry for the path to the interpreter associated with that file type.

How to run a perl script in localhost?

I had already installed Apache. I am using PHP for my scripting in localhost. Need to know how to run the perl script.
I have installed sudo aptitude install libapache2-mod-perl2
I have created a directory name cgi-bin in my /var/www/cgi-bin
there inside this folder i have kept my perl script perl_1.pl
The directory permissions are given.
What more i have to do to run the script????
i just type http://localhost/cgi-bin/
and i got error 403
You don't have permission to access /cgi-bin/ on this server.
please help!!
Thanks
you can't read the cgi-bin contents. You must refer directly to one of the scripts in it, in this case: http://localhost/cgi-bin/perl_1.pl
Outside of that, ensure that your cgi-bin/ directory is actually treated as such in httpd.conf.
Oh, and in case you stumble on 500 afterwards: make sure that your perl script prints a valid HTTP header. This can easily be achieved by:
use CGI qw(:standard);
print header();
And as Pwex pointed out: make sure your script has the executable bit set.
chmod 755 perl_1.pl
...should work in most cases
Additionally, for future reference it is worth mentioning mod_perl, as it is a natural next step after getting the basics of cgi + perl + apache down. Going into detail about it would be beyond the scope of this answer, but I thought I'd mention it so that you know where to go next when you've got the basics nailed down as well as seen the limitations of cgi.
How's your Apache configured ?
Did you make sure you're telling the Apache to execute CGI script in the cgi-bin directory ?
Something like:
ScriptAlias /cgi-bin/ "/var/www/website/cgi-bin/"
<Directory "/var/www/website/cgi-bin/">
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
...
</Directory>
If you are not tied to apache or can run these scripts on different port then you can use Plack/PSGI toolchain that have solutions to run old CGI scripts as PSGI applications. See Running CGI scripts on Plack for several ways to do it.

How do I fix "bash: perl myscript.pl: command not found"?

Maybe it's dumbest question in the world, but I seriously have problems with it and could use help. I am trying to run perl script on linux. It's a simple text editing script, nothing fancy. I googled for it and I found that I had to chmod +x it and then just run myscript.pl in the console. Since it's supposed to modify a text file I did myscript.pl > myfile.txt after chmoding it
But it doesn't work. I get: bash: perl myscript.pl: command not found
Unless myscript.pl is in your path you will need to specify the current directory.
$ ./myscript.pl
You can check if the current directory is in your path with $ echo $PATH. If you're frequently using this script you can put it in the path by moving it to a directory that's part of your path, usually ~/bin.
Or by adding the current directory to the $PATH environment variable. Check the documentation for your shell for instructions.
Can you post the first few lines of your script?
Specifically, if you have #!/usr/bin/perl are there any typos on that line, extra spaces, etc.?
Also do a ls /usr/bin/perl (or whatever is on that line) to make sure it's actually there.
It doesn't look like perl is installed on your Linux machine. Do you get the same thing when you try this: # perl -e 'print "hi";' ?
As Chirael said, it sounds like your shebang line (the directive at the top of the file, that tells the shell how to run the script) is invalid somehow. You can bypass the shebang line entirely by invoking your script as:
perl myscript.pl > myfile.txt
You also don't need to set the script's executable bit, as with this method of invocation, you are only reading the script, not executing it (from the shell's perspective).
According to this thread, it could be from different representation of the new line.
Have you written the script on a windows box and copied over to your linux box?
What is your text editor?
I had the same issue, and traced it to DOS line endings (^M). Running dos2unix on the .pl file fixed the issue.
Please use,
./myperl.pl > outfile.txt
to give the current directory path
thanks

How can I install CPAN modules locally without root access (DynaLoader.pm line 229 error)?

Doesn't work with other modules, but to give an example. I installed Text::CSV_XS with a CPAN setting:
'makepl_arg' => q[PREFIX=~/lib],
When I try running a test.pl script:
$ perl test.pl
#!/usr/bin/perl
use lib "/homes/foobar/lib/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi";
use Text::CSV_XS;
print "test";
I get
Can't load '/homes/foobar/lib/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/Text/CSV_XS/CSV_XS.so' for module Text::CSV_XS: /homes/foobar/lib/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/Text/CSV_XS/CSV_XS.so: cannot open shared object file: No such file or directory at /www/common/perl/lib/5.8.2/i686-linux/DynaLoader.pm line 229.
at test.pl line 6
Compilation failed in require at test.pl line 6.
BEGIN failed--compilation aborted at test.pl line 6.
I traced the error back to DynaLoader.pm it happens at this line:
# Many dynamic extension loading problems will appear to come from
# this section of code: XYZ failed at line 123 of DynaLoader.pm.
# Often these errors are actually occurring in the initialisation
# C code of the extension XS file. Perl reports the error as being
# in this perl code simply because this was the last perl code
# it executed.
my $libref = dl_load_file($file, $module->dl_load_flags) or
croak("Can't load '$file' for module $module: ".dl_error());
CSV_XS.so exists in the above directory
When you installed the module, did you watch the output? Where did it say it installed the module? Look in lib. Do you see the next directory you expect?
Look in ~/lib to see where eveything ended up to verify that you have the right directory name in your use lib statement:
% find ~/lib -name CSV_XS.so
Once you see where it is installed, use that directory name in your use lib (or PERL5LIB or whatever).
I expect you have a lib/lib in there somehow. The PREFIX is just the, well, prefix, and the installer appends other directory portions to that base path. That includes lib, man, bin, etc.
Personally I would suggest to use local::lib. :)
Try this instead:
'makepl_arg' => q[PREFIX=~/]
PREFIX sets the base for all the directories you will be installing into (bin, lib, and so forth.)
You may also be running into shell expansion problems with your '~'. You can try to expand it yourself:
'makepl_arg' => q[PREFIX=/home/users/foobar]
It would also be helpful if you included the commands you used to get the error you are asking about.
It looks from the error message ("at /www/common ...") that your script is a CGI or mod_perl script. The web server is probably not running as the user 'foo', under whose home directory you've installed the module - that could result in the web server being unable to read that directory.
It may also be running in a "chroot jail", which would mean that the directory in which you've installed the module may not be visible to the script.
In other words, just because you can see the module, does not mean that the web server, and therefore your script, can do so. You should check the relevant file permissions, and if the server is chrooted, whether your module directory is mounted within the virtual file system.
Does the file in question (CSV_XS.so) exist?
Does it exist at the listed location?
If you do:
set |grep PERL
What is the output?
Have you successfully installed other local perl modules?
I strongly suggest installing your own perl in your own home directory, if you have space. Then you can keep everything under your control and keep your own module set, as well as escaping if the admins are keeping you on an older version of perl. (Not to mention preserving yourself if they upgrade some day and leave out all the modules you are relying on.)