Guide to OpenLiteSpeed’s CGI Setup

How Can We Help?
< Back
You are here:
Print

Setting up CGI on OpenLiteSpeed

OpenLiteSpeed supports CGI through virtual host-level CGI contexts. A CGI context specifies a directory containing only CGI scripts. All files in this context will be considered CGI scripts and, when accessed, will be run using CGI. To run CGI, you need only set up a CGI context and place your scripts inside it. This article will go through the steps for setting up and running a CGI script with OpenLiteSpeed using the default CGI context provided under the default virtual host, “Example.”

The Virtual Host CGI Context

Contexts are located in the WebAdmin Console => Virtual Hosts => Select Your VH => Context.

In the default virtual host, Example, there are four default contexts. One is a CGI context for the URI /cgi-bin/

The most important pieces of information in a CGI context are the URI that will be used to access it and the directory it is connected to (the Path setting). If you wish to set up your own CGI contexts in your virtual hosts, you only need to specify these two settings.

Create and Run a Test CGI Script

To test CGI, we will create a regular CGI script under the CGI context directory, $VH_ROOT/cgi-bin.

We can write CGI in a shell script or Perl script, or any language that the server supports. For illustration, we will use two simple helloworld CGI scripts. One in shell and one in Perl:

Shell CGI Hello World Script

[root@localhost cgi-bin]# cat /usr/local/lsws/DEFAULT/cgi-bin/helloworld
#!/bin/sh

date=`date -u '+%a, %d %b %Y %H:%M:%S %Z'`

cat << EOF
Content-type: text/plain
Expires: $date

Hello World

EOF
[root@localhost cgi-bin]#

If we point our browser to the script’s location, http://localhost:8088/cgi-bin/helloworld, the CGI context tells OpenLiteSpeed to run the script with CGI:

Perl CGI Hello World Script

[root@localhost cgi-bin]# cat /usr/local/lsws/DEFAULT/cgi-bin/helloworld2
#!/usr/bin/perl
 
 print "Content-type: text/html\n\n";
 print <<HTML;
 <html>
 <head>
 <title>A Simple Perl CGI</title>
 </head>
 <body>
 <h1>A Simple Perl CGI</h1>
 <p>Hello World</p>
 </body>
HTML
exit; 
[root@localhost cgi-bin]#

Again, if we point our browser to the script’s location, http://localhost:8088/cgi-bin/helloworld2, the CGI context tells OpenLiteSpeed to run the script with CGI:

Note: Make sure CGI scripts are executable by the web server user (i.e. “nobody”).