Getting a Perl file to run under Ubuntu?

I gave this a go on my Windows 10
VMware Player - Ubuntu 16.04 LTS guest

http://www.ubuntugeek.com/how-to-install-apache2-webserver-with-phpcgi-and-perl-support-in-ubuntu-server.html

sudo apt-get install aptitude
sudo aptitude install apache2
# verify install success - browse to localhost - default Apache index page

sudo apt-get install php7.0 php7.0-fpm php7.0-mysql -y
sudo apt-get install libapache2-mod-php7.0
sudo apache2ctl restart
sudo nano /var/www/html/testphp.php

<?php phpinfo(); ?>

# verify PHP config success - browse to localhost/testphp.php

sudo aptitude install libapache2-mod-perl2
sudo mkdir /home/www
sudo mkdir /home/www/cgi-bin
sudo nano /etc/apache2/sites-enabled/000-default.conf

ScriptAlias /cgi-bin/ "/home/www/cgi-bin/"

<Directory /home/www/cgi-bin/>
Options +FollowSymLinks +ExecCGI
AddHandler cgi-script .cgi .pl
Require all granted
</Directory>

cd /home/www/cgi-bin
sudo nano perltest.pl

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello there!<br />\nJust testing.<br />\n";

for ($i=0; $i<10; $i++)
{
print $i."<br />\n";
}

sudo chmod a+x perltest.pl
sudo apache2ctl restart
# verify Perl config success - cd /home/www/cgi-bin - at prompt ./perltest.pl
# verify Perl config success - browse to localhost/cgi-bin/perltest.pl

I’ve done a bit of hacking from what was posted at the ubuntugeek site trying to resolve my last hurdle - getting the script to run in Firefox instead of getting the “open with / save” dialog.

I’m too bushed to keep at it tonight, but maybe the above will help some,

1 Like

A bit more info from me on what I’ve done. First off, PHP files run just fine without me doing anything - the LAMP side of things was set up when I did the Ubuntu installation, though in my case, as I’m not running more than a single site, all my web content sits in its default position under /var/www/html. For example, the server’s home page looks like this…

No one ever accused me of not having a sense of humour, but moving swiftly on…

From a browser, I can point at 192.168.6.100 and pages com up exactly as I’d expect them to, and they’ve done so for the last 2-3 years this server has been in existence. So far so good, phpinfo.php works and so do a couple of other PHP oddities I’ve set up.

To get Perl running, I’ve followed the instructions in the page linked above from the point after phpinfo.php is tested, and have then adapted the directory path to follow where my web content lives, so…

  1. sudo aptitude install libapache2-mod-perl2
  2. sudo mkdir /var/www
  3. sudo mkdir /var/www/cgi-bin
  4. sudo nano /etc/apache2/sites-enabled/000-default.conf
  5. Then added…
ScriptAlias /cgi-bin/ /home/www/cgi-bin/

<Directory /var/www/cgi-bin/>
  Options ExecCGI
  AddHandler cgi-script cgi pl
</Directory>


6. cd /var/www/cgi-bin
7. sudo nano perltest.pl
8. Added the following content to the file…

###Start###

#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";

for ($i=0; $i<10; $i++)
{
print $i."<br />";
}

###End###

then…
9. sudo chmod a+x perltest.pl

Now, by going to 192.168.6.100/cgi-bin/perltest.pl I just get the contents of the file itself presented to me.

If I run it from the CL though, I get this, which I gather is what I should get.

So Perl is definitely installed and functioning. It’s just getting it to run from a browser that seems to be the issue, pointing me back towards the Apache2 config again.

That looks like where I’m stuck.

I noticed my browser options had
Application - PL - always ask

I tried changing to open in Firefox, but when going to
localhost/cgi-bin/perltest.pl

Tabs kept opening non-stop until I closed the browser.

I’m thinking it might be necessary to call the Perl output into another page. eg. using PHP exec() or passthru(). Or maybe a .shtml page.

No, you shouldn’t. Perl should be served via Apache. I didn’t get a chance to try and get this running last night. Got swamped with other things, but this is still on my to do list.

I’m beginning to think much the same myself. I tried opening the file in a variety of browsers and got the following

IE11 & Edge offer me the option of opening the file in VS Code
Chrome, FF, Opera & Brave display the perl code itself uninterpreted

I didn’t get the multiple tab thing btw.

I’ve tried placing my original Perl file in the cgi-bin and modifying the HTML file to point at it in that location to see what I got. Once I eventually got the path to it right, it spat out the contents of my Perl file (echo.pl).

One thing I did notice was that the contents of cgi-bin had rather different owners; perltest.pl is owned by root, where echo.pl is owned by my admin account, chris. I’m wondering whether this has something to do with it.

I can run echo.pl from the CL. It looks to be acting much as perltest.pl did, though it complains a bit about the method used to call it.

Okay, I got it to work.

Install the libapache2-mod-perl
Create /var/www/cgi-bin
Chmod a+x /var/www/cgi-bin/
Add the following to 000-default.conf

ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory /var/www/cgi-bin/>
  Options +ExecCGI
  AddHandler cgi-script cgi pl
</Directory>

Run a2enmod cgi
Run service apache2 restart

Create file perltest.pl in /var/www/cgi-bin

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />Just testing.";

Then load localhost/cgi-bin/perltest.pl

1 Like
Run a2enmod cgi
Run service apache2 restart

Thanks!

Those two commands were the missing pieces for me.

1 Like

Close, but no cigar…

Aarrgghh, I hate those generic non-specific error messages. :ghost:
Did the error log have anything to go on?

That’s my next stop, just need to figure out how to get at them again (I don’t do this often).

1 Like

I think when I failed to setup the 000-default.conf changes properly, I got that error. Take a look at those.

Actually I take that back, your perl script is bad, can you copy and paste that script here? You have some sort of error in it.

There’s the error log, back in a min with the Perl script…

As you can see, I tried my echo.pl file a couple of times too. That one looks to be failing on permissions, which doesn’t surprise me based on the note I made above somewhere.

Yeah, so your perltest.pl one is due to bad script. You either have output or something happening before headers are written.

The echo.pl is a permission issue, meaning it didn’t have chomd a+x run against it.

Apologies for the lack of editable version - can you cut & paste in nano?

Yeeah, that’s a bad script. I made the same mistake at first (it has been many years since I’ve written a Perl script). Remove the ###Start### and ###End### lines. Make sure the file starts with that she-bang statement #!

1 Like

I took the thing too literal huh?

I did too at first and then after looking at what I did, I face palmed as I knew better.

Ah, I forgot all about those being there.

I didn’t include the “Start” or “End” in the script and had the shebang as the first line out of habit.

That’s a wrap folks…

Time to fix the permissions on echo.pl now, then maybe, just maybe, I can carry on with learning JS, which is where this all started.

2 Likes