Install Configure Apache Localhost Perl on Linux Ubuntu

This article teaches those who want to run Perl scripts from browser on Ubuntu Linux with Apache HTTP Server.

1. First, you need to install apache2 by executing the following command from the terminal:

sudo apt-get install apache2

2. Now you can install mod_perl by executing the following command from the terminal:

apt-get install libapache2-mod-perl2

3. If you know how to set up a local server directory to let files like example.html running from browser like http://localhost/example.html or you previously installed LAMP, then you can read on, if not, please read this article first before continue.

4. Now you may need to edit Apache Configuration file in order to tell Apache Perl where your .pl script is located, so that the dedicated server can execute it as Perl script. So let’s say if you put your Perl script example.pl inside a folder located at your defined local server directory for instance /home/shi/Documents/Host/perl/example.pl, you need to open apache2.conf:

sudo gedit /etc/apache2/apache2.conf

add in the code:

Alias /perl/ /home/shi/Documents/Host/perl/

PerlModule ModPerl::Registry

<Location /perl/>
   SetHandler perl-script
   PerlHandler ModPerl::Registry
   #PerlHandler ModPerl::PerlRun
   Options +ExecCGI
   #PerlSendHeader On
</Location>

(change the line /home/shi/Documents/Host/perl/ to your server directory name)

And now restart your server by running the following command in terminal:

sudo /etc/init.d/apache2 restart

Now write a test.pl file with the test script below:

#!/usr/bin/perl
use CGI;
my $query= new CGI;
print $query->header;
print "hello people in my head\n";

set the test.pl file permission to 755 and put it in your local server directory and type the url path in the browser and you should see the message: ‘hello people in my head’.

Feel free to post a comment to let me know if you need any help!

Be Sociable, Share!

About Shi Chuan

I am a web developer.
This entry was posted in Perl and tagged , . Bookmark the permalink.

21 Responses to Install Configure Apache Localhost Perl on Linux Ubuntu

  1. jakub88 says:

    have been trying to install Perl for a month from now, finally get it installed and configured correctly by following your article, great article!

  2. Jose Esquivel ( Costa Rica ) says:

    Hi,

    I started at my new job 2 days ago, I was told to learn a little bit of Perl, as I like to do more I proposed to do also do some Perl on Web and this tutorial is awesome, it practically took me all the way to set my environment. One of the best tutorials, very direct, easy to follow and in the end you get what you specked

  3. admin says:

    Hi, guys, i am so glad that it helps!

  4. Gary Kuipers says:

    HI, thanks for the help but as most things linux, the how to’s are simple, direct, concise, beautiful and they don’t work. At least this did not on a Ubuntu 8.1 LAMP server (sigh! … back to Google for more)

  5. 2wihuh says:

    Hello, Very nice site. Universe help us, dont worry man.

  6. h07tfx says:

    c0f9c6c72d21f023a3111820e5a5c769 7075-4ed2d2cd080b49d1600670c2e65a74df

  7. MickG says:

    OK followed all the steps and get to the correct index.html in the correct location but selecting the pl file gives the fila as available for download by firefox.

    restarting apache gives:
    * Restarting web server apache2 apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
    … waiting apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

    I’m obviously doing something wrong because Apache says mod_perl is installed.

    • Bob Jones says:

      If you are having trouble setting up userdir.conf on ubuntu, but it works on the default webserver (/var/www/), first check to see that you are not using encrypted home folders. If you are, that situation will give you problems that aren’t unsurmountable, but there are too many hoops (in my opinion) to jump through to get it working. I just disabled encrypting my home folder (google it) and it now works fine. It is a toy computer for teaching so I don’t care if it is not encrypted.

      Unfortunately, apache2 and ubuntu don’t give specific errors when something like that doesn’t work. The browser just offers to download the perl program, it doesn’t execute it and send the html to your browser like it should. But, no errors or other clues as to why it wouldn’t work.

    • Bob Jones says:

      The fastest way to see of you are using encrypted home folder is to look at the output of

      $ mount

      Another way is to see if the file .encryptfs is in the /home/ directory with the command

      $ ls -al

      Another option would be to set up another user for your webdesign.
      Not all accounts that are set up use encrpted home folders.
      If you create an account name jim with the command

      $ sudo adduser jim

      you can then log in as jim an use files in /home/jim/public_html/ to set up the website. Chances are that in ubuntu they won’t be encrypted and so won’t give you the same problem.

  8. nikhil thakre says:

    how to install perl on ubunto server 9.0

  9. Pingback: Finally! Easy instructions to enable mod_perl on an ubuntu server :Seriously For Fun

  10. ryan says:

    if you have properly installed Perl on any linux (quite simple to do using graphical package managers or comes bundled with the base system if not)

    anyways, all you need is to add
    #!/usr/bin/perl -w
    at the top of your script and make your perl file executable

    add this line :
    AddHandler cgi-script cgi pl in a .htaccess file at the root of your folder and you should be good to go.

  11. Michael Dickerson says:

    Thank you. Thank you. Thank you.

    I’m BRAND NEW at this and just built an ubuntu server on a whim but have been banging my head on getting perl scripts to work. Both the test script you suggested and a simple script I downloaded work! Your instructions are simple and easy to follow and now I’m in business.

    Thank you. Thank you. Thank you.

  12. rich says:

    MickG

    I’m a total newbie, so this is just something that worked for me, not expert advice.

    I think the unqualified website warning is not related to any other problems. I got tired of seeing it, so here is my cure for that. If you go into gedit in the top your’ll see your site name, for example it might be mickg@MickG-desktop: /path if your Ubuntu was setup with your name the same as mine was with my name.

    Then, either in apache2.conf or a config file that is included in apache2.conf enter ServerName MickG-desktop. Just to keep the original config clean I created a file called MyChanges in the subdirectory /etc/apache2/conf.d and put this line in there.

    Thank you shichuanr for writing the helpful hints.

    rich

  13. k says:

    wen i try to run the perl file. the browser prompts to download the file instead of installing! plz help

  14. Prabhakaran says:

    hii sorry for my question.

    i followed by your steps.

    How to invoke url ?
    http://localhost/perl this is correct ?
    i faced below error message
    requested URL /perl was not found on this server.

    can u help me ?

  15. druapl says:

    Hello, i feel that i noticed you visited my blog thus i got here to go back the desire?.I am attempting to find things to enhance my web site!I suppose its adequate to use a few of your ideas!!

  16. Its like you learn my mind! You seem to know a lot approximately this, such as
    you wrote the e-book in it or something. I believe that you simply can do with some % to force the message house a little bit, however instead of that, this is fantastic blog. A great read. I will certainly be back.

  17. dir.e.zp.ua says:

    You need to take part in a contest for one of the highest quality websites on the net.
    I am going to highly recommend this site!

  18. site says:

    Not all companies are made equal, plus some are a lot more expensive compared to others.
    Try to use large bags; it will be easier to cut smaller shapes out of larger pieces than add two pieces together.

    When advertising in an ezine, you place your advert in ezine that of the same or similar niche with what you are offering in your site.

    First off I would like to say great blog!
    I had a quick question that I’d like to ask if you do not mind.
    I was interested to know how you center yourself and clear your mind prior to writing.

    I have had a tough time clearing my mind in getting my ideas out there.
    I do enjoy writing however it just seems like the first 10 to 15 minutes
    tend to be wasted simply just trying to figure
    out how to begin. Any ideas or hints? Kudos!

  19. tee for dogs says:

    Aw, this was an incredibly good post. Spending some time and actual effort to
    create a really good article… but what can I say… I put things off a lot
    and never seem to get nearly anything done.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>