Getting a Perl file to run under Ubuntu?

I’ve got a Perl file sitting alongside a HTML form that requires it - it’s just intended to provide feedback as if it was a production server providing real responses to a user - a kind of demo thing.

The Ubuntu box is running 14.04 LTS and has Perl 5.18.x installed by default, but the Perl file doesn’t appear to be making use of it, and I just get the entire Perl script fed back to me instead of the small HTML table I should get.

Does anyone still remember how to get such things running? I’m guessing the var/www/html/[TEST DIR} folder doesn’t know the path to Perl, but am wholly unsure how to get it to find it.

1 Like

What server are you using? Apache? Nginx? Lighttpd?

It’s running Apache.

Did you install libapache2-mod-perl2?

Did you create a cgi-bin folder in your /www/ folder?
Does the perl script have execute permissions? (ie: chmod a+x

I think the answer there is “None of the above”

Aside from the name, I essentially know nothing about Perl, other than it’s installed by default on Ubuntu. I guess I’m looking for a pointer towards a digestible guide on making use of what is already there.

Okay, start with that. And then maybe scan over this document for some of the Apache configuration

OK. I’ll work through that (tomorrow)

No joy with this so far. As best I can tell I followed the steps to the letter, but it threw out as written, rather than interpreting it. More reading to do…

I’ll try to throw a VM up tonight to see if I have any luck. It has been a few years, but I don’t think it has changed significantly.

1 Like

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

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

cd /home/www/cgi-bin
sudo nano

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
sudo apache2ctl restart
# verify Perl config success - cd /home/www/cgi-bin - at prompt ./
# verify Perl config success - browse to localhost/cgi-bin/

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 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

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


#!/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 />";


9. sudo chmod a+x

Now, by going to 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

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 (

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

I can run from the CL. It looks to be acting much as 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

Run a2enmod cgi
Run service apache2 restart

Create file in /var/www/cgi-bin

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

Then load localhost/cgi-bin/

1 Like
Run a2enmod cgi
Run service apache2 restart


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.