0
[root@localhost html]# rpm -q centos-release
centos-release-7-9.2009.1.el7.centos.x86_64

[root@localhost html]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:   Dec 13 2020 00:35:05

[root@localhost html]# perl -v
This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux

[root@localhost html]# cpan
cpan shell -- CPAN exploration and modules installation (v2.28)


[root@localhost html]# pwd
/var/www/html

[root@localhost html]# ls -lha
-rw-r--r--. 1 apache apache  239 Jul 26 09:54 .htaccess
-rw-r--r--. 1 apache apache   47 Jul 25 20:00 index.html
-rwxr-xr-x. 1 apache apache   97 Jul 26 11:02 perl.pl

Script runs normally on http://localhost/perl.pl

PERL script content

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";

print "Hello World!";

exit;


[root@localhost html]# pp -x -c -o a.out perl.pl
/tmp/kjzLwQRe6G syntax OK

[root@localhost html]# mv a.out xx.pl
[root@localhost html]# chown apache:apache xx.pl
[root@localhost html]# chmod 755 xx.pl


[root@localhost html]# ./perl.pl
Content-type: text/html

Hello World![root@localhost html]# ./xx.pl
Content-type: text/html

Hello World![root@localhost html]#

When I run the script via browser http://localhost/xx.pl

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

[root@localhost html]# tail -f /var/log/httpd/ssl_error_log
[Mon Jul 26 11:06:18.315481 2021] [cgi:error] [pid 18880] [client 2.57.171.41:12219] End of script output before headers: xx.pl
[Mon Jul 26 11:07:21.702880 2021] [cgi:error] [pid 18881] [client 2.57.171.41:9437] End of script output before headers: xx.pl
[Mon Jul 26 11:07:22.742897 2021] [cgi:error] [pid 18879] [client 2.57.171.41:19913] End of script output before headers: xx.pl
[Mon Jul 26 11:07:28.448565 2021] [cgi:error] [pid 18882] [client 2.57.171.41:27467] End of script output before headers: xx.pl
[Mon Jul 26 11:30:08.557949 2021] [cgi:error] [pid 18883] [client 2.57.171.41:30987] End of script output before headers: xx.pl
Liandro Cos
  • 65
  • 11
  • 1
    Why does the log show both `perl.pl` and `xx.pl` ? Like this: `End of script output before headers: perl.pl` and then this: `End of script output before headers: xx.pl` ? – Håkon Hægland Jul 26 '21 at 15:55
  • I apologize, before I was doing mv a.out test.pl, and in order to post the problem here I renamed a.out to xx.pl perl.pl runs normally. – Liandro Cos Jul 26 '21 at 16:11
  • 1
    What is the output if you run the scripts from the terminal instead of from the browser at `http://localhost` ? – Håkon Hægland Jul 26 '21 at 16:17
  • 1
    The documentation for CGI says that basically you should not use it anymore, that there are better alternatives, and some HTML-generating functions are no longer being maintained. – TLP Jul 26 '21 at 17:23
  • 1
    `print` without ending newline can cause autoflush issues, try adding that. Although the program should flush buffers when it exits, maybe there is some slight delay. – TLP Jul 26 '21 at 17:33
  • 1
    Also: https://metacpan.org/pod/CGI::Alternatives – TLP Jul 26 '21 at 17:33
  • 1
    To show that it's not the CGI module, I did a HELLO WORD, and the error persists! – Liandro Cos Jul 27 '21 at 14:37
  • *"mv a.out xx.pl"* Here you rename an ELF executable `a.out` to a file with a `.pl` extension. I think that might confuse the web server – Håkon Hægland Jul 27 '21 at 16:08
  • According to [this](https://askubuntu.com/questions/547414/running-binary-cgi-on-apache2) answer, you might also need to add a `.bin` handler in your web servers configuration file. – Håkon Hægland Jul 27 '21 at 16:11
  • Even if .out the error script – Liandro Cos Jul 27 '21 at 16:54
  • *To show that it's not the CGI module, I did a HELLO WORD, and the error persists!* -- This tells me nothing. You are basically just repeating what you said in the question: That you printed "Hello world". Nothing indicates that you've read the comments and are responding to the information in them. – TLP Jul 27 '21 at 22:30

1 Answers1

0

I was able to make this work using XAMPP 8.0.8 on Ubuntu 21.04. I first set up a virtual host (which I named localhost2) by editing /opt/lampp/etc/httpd.conf to include the file etc/extra/httpd-vhosts.conf. Then edited the latter file to add a new virtual host. Then edited /etc/hosts to include the added virtual host.

Then enabled CGI scripts from /opt/lampp/etc/httpd.conf:

  • Make sure that you load these modules:

    LoadModule cgi_module modules/mod_cgi.so
    LoadModule alias_module modules/mod_alias.so
    
  • Then set up ScriptAlias directory like this:

    <IfModule alias_module>
        ScriptAlias /cgi-bin/ "/opt/lampp/cgi-bin/"
    </IfModule>
    
  • Finally, enable access to the script directory by including:

    <Directory "/opt/lampp/cgi-bin">
        AllowOverride None
        Options ExecCGI
        AddHandler cgi-script .cgi .pl .bin
        Require all granted
    </Directory>
    

Now make sure that the directory /opt/lampp/cgi-bin has read and search permissions for all.

Then create a perl script /opt/lampp/cgi-bin/hello.pl:

#!/usr/bin/perl
use strict;
use warnings;

print "Content-type: text/html\n\n";
print "Hello World!\n";
exit;

Make sure the script has read and execute permission by all:

$ sudo chmod 755 hello.pl

Convert the script into a binary using pp (from PAR::Packer) :

$ sudo pp -x -c -o a.bin hello.pl

(Again make sure the script has read and execute permissions by all) Note that the binary has a .bin extension and that I added a .bin handler in httpd.conf above.

Start the LAMP web server, visit the page http://localhost2/cgi-bin/a.bin. The output in the browser window was

Hello World!
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174