Skip to main content

Executing CGI scripts on an Apache web server

By April 12, 2013September 12th, 2022No Comments

One thing you will certainly need to do with your Apache web server is being able to execute CGI scripts, server-side scripts, on the server. For this we will have to enable a few modules if we are to start, as ourselves, with a minimal httpd configuration. The three modules that we will add are:

  • : for CGI scripts execution
  • : so we can house our scripts in anoahter location to the DocumentRoot for higher security
  • : for file type associations , so we can asscoiate, in this case, .pl files with CGI

The modules.conf file should now be similar to this:


		LoadModule authz_host_module modules/
		LoadModule dir_module modules/
		LoadModule info_module modules/
		LoadModule status_module modules/
		LoadModule cgi_module modules/
		LoadModule mime_module modules/
		LoadModule alias_module modules/

With this in place we are going to add some lines to the httpd.conf. We will want a ScriptAlias directive to we can ensure that our executable scripts can be kept separate from out main pages. The pages do no need the execute option but, of course, scripts will. In the directory block for the aliased directory we add in execute permission with Options +ExecCGI The httpd.conf will now look similar to this:


		User apache
		Group apache
		ErrorLog /var/log/httpd/error.log
		Include conf.d/modules.conf
		DirectoryIndex index.html
		DocumentRoot /var/www/html
		<Directory /var/www/html>
        	AllowOverride None
        	Order allow,deny
        	allow from
		<Location /server-status>
		SetHandler server-status
		Order allow,deny
		<Location /server-info>
		SetHandler server-info
		Order allow,deny
		TypesConfig /etc/mime.types
		AddHandler cgi-script .pl
		ScriptAlias /cgi-bin/ /var/log/cgi-bin
		<Directory /var/log/cgi-bin/ >
		Options +ExecCGI

So now that the server is up and running we can add, in this case, perl scripts into the directory, /var/log/cgi-bin. We will need to make sure they have an extension of .pl to match the association we created and we will need to make sure we have the execute permission in Linux added with chmod.

		print "Content-type: text/plainnn";
		print "Hello worldn";

		use CGI qw(:standard);
		my $username = param('username');
		print header;
		print start_html("My perl page");
		print h2("Hello $username");
		print p("This page was generated by perl");
		print end_html;