How to run CGI programs?

General

The Common Gateway Interface (CGI) is a standard for interfacing external applications with Web servers. Unlike a plain HTML document which returns only static information, a CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.

  1. Instructions to setup CGI programs
  2. Setup authenticated CGI programs

Instructions to setup CGI programs

  1. Before you can write your own CGI program, you will need to have an account on the Teaching Web Server. If you haven't got an account on the Teaching Web Server yet, please refer to our web page on "Who can apply" for more information.
  2. You have to place your CGI programs in a directory called cgi-bin in your public_html directory. Use the following command to create the "cgi-bin" directory first:

      mkdir $HOME/public_html/cgi-bin
      

  3. From now on, you can place your CGI programs under the directory $HOME/public_html/cgi-bin and the URL to access your CGI program is :

      http://teaching.ust.hk/cgi-bin/cgiwrap/~course_code/CGI_program_name
      

  4. Examples

  5. Note to C programmers
    If you are writing your CGI programs in C, it may happen that when running your program, the dynamic linker would warn you that the library you are using is older than expected. As this warning will usually be given out as first line of output, this makes the CGI program does not work as expected. The solution to this is to recompile your C program in Solaris OS before you run it on Web as our Web Server is running Solaris 2.6 now.


Setup authenticated CGI programs

  1. First read Instructions to setup CGI programs in order to understand basic CGI program setup procedure.

  2. Because cgiwrap does not support .htaccess placed underneath in your $HOME/public_html/cgi-bin, the web server does not read and follow any commands in .htaccess right there.

  3. To execute your CGI program with authentication, you should use the following URL instead:
    1. http://teaching.ust.hk/cgi-bin/auth/cgiwrap/course_code/CGI_program_name
      
      or
      http://teaching.ust.hk/cgi-bin/auth/cgiwrap/~course_code/CGI_program_name
      

  4. We have put a generic .htaccess (see below) in URI  /cgi-bin/auth/cgiwrap , the web server will request authentication when accessing the above URL.  After entering correct ITSC account and password, the CGI environment variables  e.g. REMOTE_USER and REMOTE_HOST will pass along to your CGI program.

      AuthName HKUST
      AuthType Basic
      
      <Limit GET POST>
      require valid-user
      </Limit>
      

  5. It is your task to check who are authorized to execute the CGI program based on the variable REMOTE_USER.

  6. This setup differs from general web authentication as because we use cgiwarp for user CGI programs execution.  In traditional web authentication, both authentication (who you are) and authorization (are you right to do that?) are handled or defined by web server and .htaccess file.  However, authentication is still handled by web server but authorization will be your responsibility in CGI program.

  7. Under this setup, please note that you are not possible to define your own username and password pair for authentication.