How do I configure Apache 2 to run Perl CGI scripts?
Solution 1
This post is intended to rescue the people who are suffering from *not being able to properly setup Apache2 for Perl on Ubuntu. (The system configurations specific to your Linux machine will be mentioned within square brackets, like [this]).
Possible outcome of an improperly setup Apache 2:
- Browser trying to download the .pl file instead of executing and giving out the result.
- Forbidden.
- Internal server error.
If one follows the steps described below with a reasonable intelligence, he/she can get through the errors mentioned above.
Before starting the steps. Go to /etc/hosts
file and add IP address / domain-name` for example:
127.0.0.1 www.BECK.com
Step 1: Install apache2
Step 2: Install mod_perl
Step 3: Configure apache2
open sites-available/default
and add the following,
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]
<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
AddHandler cgi-script .pl
Order allow,deny
allow from all
</Directory>
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory [path-to-store-your-website-files-like-.html-(perl-scripts-should-be-stored-in-cgi-bin] >
####(The Perl/CGI scripts can be stored out of the cgi-bin directory, but that's a story for another day. Let's concentrate on washing out the issue at hand)
####
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ [path-where-you-want-your-.pl-and-.cgi-files]
<Directory [path-where-you-want-your-.pl-and-.cgi-files]>
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
AddHandler cgi-script .pl
Order allow,deny
allow from all
</Directory>
Step 4:
Add the following lines to your /etc/apache2/apache2.conf
file.
AddHandler cgi-script .cgi .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>
<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
Step 5:
Very important, or at least I guess so, only after doing this step, I got it to work.
AddHandler cgi-script .cgi .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
<Files ~ "\.cgi$">
Options +ExecCGI
</Files>
<IfModule mod_perl.c>
<IfModule mod_alias.c>
Alias /perl/ /home/sly/host/perl/
</IfModule>
<Location /perl>
SetHandler perl-script
PerlHandler Apache::Registry
Options +ExecCGI
</Location>
</IfModule>
<Files ~ "\.pl$">
Options +ExecCGI
</Files>
Step 6
Very important, or at least I guess so, only after doing this step, I got it to work.
Add the following to you /etc/apache2/sites-enabled/000-default
file
<Files ~ "\.(pl|cgi)$">
SetHandler perl-script
PerlResponseHandler ModPerl::PerlRun
Options +ExecCGI
PerlSendHeader On
</Files>
Step 7:
Now add, your Perl script as test.pl in the place where you mentioned before in step 3 as [path-where-you-want-your-.pl-and-.cgi-files].
Give permissions to the .pl
file using chmod
and then, type the webaddress/cgi-bin/test.pl
in the address bar of the browser, there you go, you got it.
(Now, many of the things would have been redundant in this post. Kindly ignore it.)
Solution 2
You'll need to take a look at your Apache error log to see what the "internal server error" is. The four most likely cases, in my experience would be:
The CGI program is in a directory which does not have CGI execution enabled. Solution: Add the
ExecCGI
option to that directory via either httpd.conf or a .htaccess file.Apache is only configured to run CGIs from a dedicated
cgi-bin
directory. Solution: Move the CGI program there or add anAddHandler cgi-script .cgi
statement to httpd.conf.The CGI program is not set as executable. Solution (assuming a *nix-type operating system):
chmod +x my_prog.cgi
The CGI program is exiting without sending headers. Solution: Run the program from the command line and verify that a) it actually runs rather than dying with a compile-time error and b) it generates the correct output, which should include, at the very minimum, a
Content-Type
header and a blank line following the last of its headers.
Solution 3
(Google search brought me to this question even though I did not ask for perl)
I had a problem with running scripts (albeit bash not perl). Apache had a config of ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
however Apache error log showed File does not exist: /var/www/cgi-bin/test.html
.
Tried putting the script in both /usr/lib/cgi-bin/
and /var/www/cgi-bin/
but neither were working.
After a prolonged googling session what cracked it for me was
sudo a2enmod cgi
and everything fell into place using /usr/lib/cgi-bin/
.
Solution 4
If you have successfully installed Apache web server and Perl please follow the following steps to run cgi script using perl on ubuntu systems.
Before starting with CGI scripting it is necessary to configure apache server in such a way that it recognizes the CGI directory (where the cgi programs are saved) and allow for the execution of programs within that directory.
-
In Ubuntu cgi-bin directory usually resides in path /usr/lib , if not present create the cgi-bin directory using the following command.cgi-bin should be in this path itself.
mkdir /usr/lib/cgi-bin
-
Issue the following command to check the permission status of the directory.
ls -l /usr/lib | less
Check whether the permission looks as “drwxr-xr-x 2 root root 4096 2011-11-23 09:08 cgi- bin” if yes go to step 3.
If not issue the following command to ensure the appropriate permission for our cgi-bin directory.
sudo chmod 755 /usr/lib/cgi-bin
sudo chmod root.root /usr/lib/cgi-bin
-
Give execution permission to cgi-bin directory
sudo chmod 755 /usr/lib/cgi-bin
Thus your cgi-bin directory is ready to go. This is where you put all your cgi scripts for execution. Our next step is configure apache to recognize cgi-bin directory and allow execution of all programs in it as cgi scripts.
Configuring Apache to run CGI script using perl
-
A directive need to be added in the configuration file of apache server so it knows the presence of CGI and the location of its directories. Initially go to location of configuration file of apache and open it with your favorite text editor
cd /etc/apache2/sites-available/ sudo gedit 000-default.conf
-
Copy the below content to the file 000-default.conf between the line of codes “DocumentRoot /var/www/html/” and “ErrorLog $ {APACHE_LOG_DIR}/error.log”
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Require all granted
-
Restart apache server with the following code
sudo service apache2 restart
-
Now we need to enable cgi module which is present in newer versions of ubuntu by default
sudo a2enmod cgi.load sudo a2enmod cgid.load
-
At this point we can reload the apache webserver so that it reads the configuration files again.
sudo service apache2 reload
The configuration part of apache is over now let us check it with a sample cgi perl program.
Testing it out
-
Go to the cgi-bin directory and create a cgi file with extension .pl
cd /usr/lib/cgi-bin/ sudo gedit test.pl
-
Copy the following code on test.pl and save it.
#!/usr/bin/perl -w print “Content-type: text/html\r\n\r\n”; print “CGI working perfectly!! \n”;
-
Now give the test.pl execution permission.
sudo chmod 755 test.pl
Now open that file in your web browser http://Localhost/cgi-bin/test.pl
If you see the output “CGI working perfectly” you are ready to go.Now dump all your programs into the cgi-bin directory and start using them.
NB: Don't forget to give your new programs in cgi-bin, chmod 755 permissions so as to run it successfully without any internal server errors.
Solution 5
As of Ubuntu 12.04 (Precise Pangolin) (and perhaps a release or two before) simply installing apache2
and mod-perl
via Synaptic and placing your CGI scripts in /usr/lib/cgi-bin is really all you need to do.
Related videos on Youtube
![Peter Mortensen](https://i.stack.imgur.com/RIZKi.png?s=256&g=1)
Peter Mortensen
Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)
Updated on June 14, 2020Comments
-
Peter Mortensen about 4 years
I would like to configure Apache 2 running on
Kubuntu
to execute Perl CGI scripts. I've tried some steps that I came across by googling, but nothing seems to work.What is the right way of achieving this?
-
Smilediver over 15 yearsit might help if you linked to what you'd tried already and said what didn't work with it.
-
brian d foy over 15 yearsWhat error messages are you getting in the error log? Those usually tell you what isn't right.
-
capser over 12 years@cletus - i have a bunch of old cgi scripts from work - they work fine, however putting them on a different web server is proving to be a real pain. I am not going to throw the babies out with the bathwater. this is a good post
-
-
Admin over 15 yearsYeah, ive already installed libapache2-mod-perl2 , but still when i try to access my page using localhost/cgi-bin/mypage.cgi , i get an 'Internal Server Error'
-
innaM over 15 yearsSo perhaps mypage.cgi is broken?
-
Admin over 15 yearsThanks a lot :)..However instead of httpd.conf, i had to make changes in /etc/apache2/sites-enabled/default, and then everything worked...httpd.conf was a blank file. Any reason why i didnt have to make changes in httpd.conf ?
-
Lawrence Hutton over 15 yearsThe more "modern" way of handling configs is to put everything into a flock of partial config files (in sites-enabled, mods-enabled, and conf.d) so that programs can manipulate them more easily. When you're doing this, httpd.conf is no longer needed and can be either used or not.
-
Dims over 9 yearsOne million lines just to run perl?
-
cpb2 over 9 yearsI don't understand step 5 and 6. You explain that it is 'very important'. You give a command
AddHandler cgi-script .cgi .pl
. Then there is a block of code. Where are we suppose to place this exactly? -
Andrea Gottardo over 8 yearsThanks, on Debian 7 you need to run
a2enmod cgi
otherwise it won't work. -
いちにち about 8 years
a2enmod cgi
fixed it for me too! This answer really deserves more votes. -
Admin almost 8 yearsIsn't there anyway to deploy
.cgi
to another type of server? likepuma
:| -
Stefan Vorkoetter over 7 yearsThanks! I can't believe the dozen or so other pages I found first never mentioned this one simple, small, IMPORTANT, thing.
-
gpwr almost 7 yearsWow... this looks complicated. Isn't there an easier way? Looks like re-inventing the wheel :(
-
Meetai.com over 4 yearsAlso, In your <Directory> add Options +ExecCGI AddHandler cgi-script .cgi .pl Also seen here: stackoverflow.com/a/20473841