Skip to content

Instantly share code, notes, and snippets.

@naveenrajm7
Last active June 10, 2018 22:27
Show Gist options
  • Save naveenrajm7/ce7746960733481ed57eeec09fc7316d to your computer and use it in GitHub Desktop.
Save naveenrajm7/ce7746960733481ed57eeec09fc7316d to your computer and use it in GitHub Desktop.
Perl and Apache - Running and Debugging

Follow these command on ubuntu to intsall apache, my-sql, php, perl (LAMP)

Apache

sudo apt-get install vim  #vim for terminal editing
sudo apt-get install apache2  #apache

Following change required to run perl script using apache

1. add following to 000-default.conf file

sudo vi /etc/apache2/sites-enabled/000-default.conf
    <Files ~ "\.(pl|cgi)$">
        SetHandler perl-script
        PerlResponseHandler ModPerl::PerlRun
        Options +ExecCGI
        PerlSendHeader On
    </Files>

2. add following to apache.conf file

sudo vi /etc/apache2/apache2.conf
 <Directory /var/www/cgi-bin/>
    AddHandler cgi-script .cgi .pl
    Options FollowSymLinks ExecCGI
    AllowOverride None
 </Directory>

MySql

Install

sudo apt-get install mysql-server 

set password when prompted

Use

mysql -u root -p

enter password and start working!

PHP

sudo apt-get install php
sudo apt-get install -y php-{bcmath,bz2,intl,gd,mbstring,mcrypt,mysql,zip} && sudo apt-get install libapache2-mod-php  -y

Perl

sudo aptitude install libapache2-mod-perl2
sudo apt-get install libdbi-perl
sudo apt-get install libdbd-mysql-perl

How to work

  1. restart the server
sudo apache2ctl restart
  1. write pl, cgi scripts in /var/www/html
  2. change permission 755
  3. run in browser at port 8080 (default) by entering file name

500 Internal Server Error

please contact server administrator , [email protected]

Scared of 500 internal server error while running perl,cgi scripts? here is a hack to debug that not so useful 500 internal server to a more useful one

1. Check you perl/cgi script syntax :

first step to avoid error is to make sure your scripts compiles properly do , $perl -c file.pl #to check errors

a. if Syntax Errors

C:\>perl -c test.pl
String found where operator expected at test.pl line 1, 
near "printt "Hello World!""
(Do you need to predeclare printt?)
syntax error at test.pl line 1, near "printt "Hello World!""
test.pl had compilation errors.
C:\>

b. No errors

C:\>perl -c myscript.pl
myscript.pl syntax OK

2. Debugging 500 internal server error

Before doing anything else add this code to the top of your script. If you add this code your script will run for long enough that any errors appear in the browser window making it easy to see the problem, presuming of course that you have followed the advice above! This will avoid you getting the less than useful 500 Internal Server Error or Premature end of script headers/Malformed script headers messages. You add this code just below the shebang line and before everything else. That's everything else. The reason for this is to minimise the lines which can cause problems before we reliably direct output (including syntax errors) to the browser
Add following code to your perl program at begining

#!/usr/bin/perl -wT

## ensure all fatals go to browser during debugging and set-up
## comment this BEGIN block out on production code for security
BEGIN {
    $|=1;
    print "Content-type: text/html\n\n";
    use CGI::Carp('fatalsToBrowser');
}

# all the rest of the code goes here

reference : perlmonks

@avinashav
Copy link

Use below code in php to show php errors in browser

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Reference
@naveenrajm7

@SubrahmanyaHM
Copy link

Use below trick to debug JavaScript errors

Press Ctrl+Shift+j in browser & see console for errors
@naveenrajm7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment