Categories
Linux System Ubuntu

How to run perl CGI script in Apache

Last updated on February 28th, 2022 at 10:11 am

Click to rate this tutorial!
[Total: 1 Average: 5]

This is a step by step guide on how to configure CGI in Apache web server and run perl scripts in linux systems. We are enabling Apache module named mod_cgi to deliver dynamic content and run any programming languages(as a backend) of your choice by simply forwarding requests from Apache to CGI. In this tutorial our aim is to run Perl scripts behind Apache by configuring CGI (Common Gateway Interface).

Example below is shown in an Ubuntu server. But you can follow this step for (Redhat / Centos) .

My Apache Server and OS Details

Server version: Apache/2.4.41 (Ubuntu)

NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"

Step 1

Assuming that Apache is already installed lets start by enabling CGI module. Run the a2enmod command and follow the same steps shown

$ sudo a2enmod
Your choices are: access_compat actions alias allowmethods asis auth_basic auth_digest auth_form authn_anon authn_core authn_dbd authn_dbm authn_file authn_socache authnz_fcgi authnz_ldap authz_core authz_dbd authz_dbm authz_groupfile authz_host authz_owner authz_user autoindex brotli buffer cache cache_disk cache_socache cern_meta cgi cgid charset_lite data dav dav_fs dav_lock dbd deflate dialup dir dump_io echo env expires ext_filter file_cache filter headers heartbeat heartmonitor http2 ident imagemap include info lbmethod_bybusyness lbmethod_byrequests lbmethod_bytraffic lbmethod_heartbeat ldap log_debug log_forensic lua macro md mime mime_magic mpm_event mpm_prefork mpm_worker negotiation proxy proxy_ajp proxy_balancer proxy_connect proxy_express proxy_fcgi proxy_fdpass proxy_ftp proxy_hcheck proxy_html proxy_http proxy_http2 proxy_scgi proxy_uwsgi proxy_wstunnel ratelimit reflector remoteip reqtimeout request rewrite sed session session_cookie session_crypto session_dbd setenvif slotmem_plain slotmem_shm socache_dbm socache_memcache socache_redis socache_shmcb speling ssl status substitute suexec unique_id userdir usertrack vhost_alias xml2enc

Which module(s) do you want to enable (wildcards ok)?
cgid

Enabling module cgid.

To activate the new configuration, you need to run:
  systemctl restart apache2

Step 2

There should be a configuration file named serve-cgi-bin.conf in this location /etc/apache2/conf-enabled . Content of this file should be similar to the one below.

Note: This file normally gets created automatically. If that is not the case please create and enable it.

<IfModule mod_alias.c>
	<IfModule mod_cgi.c>
		Define ENABLE_USR_LIB_CGI_BIN
	</IfModule>

	<IfModule mod_cgid.c>
		Define ENABLE_USR_LIB_CGI_BIN
	</IfModule>

	<IfDefine ENABLE_USR_LIB_CGI_BIN>
		ScriptAlias /cgi-bin/ /var/www/perlscript
		<Directory "/var/www/perlscript">
			AllowOverride None
			Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
			AddHandler cgi-script .cgi .pl
			Require all granted
		</Directory>
	</IfDefine>
</IfModule>

Make sure to update path /var/www/perlscript to the one you have configured (Under ScriptAlias and Directory directives)

Step 3

Check the syntax and Restart apache,

$ sudo apachectl -t
Syntax OK
$sudo systemctl restart apache2
$

How to Test ?

As per above configuration perl files needs to be deployed in this location /var/www/perlscript

Let us say I have a script named ipaddress.pl in /var/www/perlscript , just try to do a curl and see whether that works. You are free to use our script (how to find IP address using perl) to test the configuration.

$curl -I localhost/cgi-bin/ipaddress.pl
HTTP/1.1 200 OK
Date: Wed, 16 Feb 2022 19:54:12 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Length: 50
Content-Type: text/html; charset=ISO-8859-1

If there is some configuration issue you might get a 404 file not found instead of 200 OK

$ curl -I localhost/cgi-bin/ipaddress.pl
HTTP/1.1 404 Not Found
Date: Wed, 16 Feb 2022 18:54:55 GMT
Server: Apache/2.4.41 (Ubuntu)
Content-Type: text/html; charset=iso-8859-1

Keep in mind that by default CGI scripts needs to be placed in /usr/lib/cgi-bin/ . If you get a 404 for the above configuration try putting the perl files in the /usr/lib/cgi-bin location and test again.

How to configure CGI in Redhat / Centos to run Perl

In Redhat based systems you may have to go to httpd.conf and uncomment LoadModule section for CGI module (mod_cgi.so or similar) manually instead of running the a2enmod available in Debian based systems. Once that is done add this to httpd.conf

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

Make sure that you uncomment scriptalias directive

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

Deploy you perl scripts in this location /var/www/cgi-bin/ (Default location as per above configuration, modify it if required).

Testing and restart of apache similar to the Ubuntu section.(Step 3)

Click to rate this tutorial!
[Total: 1 Average: 5]

Leave a Reply

Your email address will not be published.