CGI Setup

From OpenLiteSpeed Wiki

CGI is supported in OpenLiteSpeed using 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 => Configuration => Virtual Host => 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 need only 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.

CGI can be written in shell 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 script

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

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

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

Hello World

[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 script

[root@localhost cgi-bin]# cat /usr/local/lsws/DEFAULT/cgi-bin/helloworld2
 print "Content-type: text/html\n\n";
 print <<HTML;
 <title>A Simple Perl CGI</title>
 <h1>A Simple Perl CGI</h1>
 <p>Hello World</p>
[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").