Writing CGI Scripts

Note that CGI is an old technology, but it is only a connection method for connecting the Web to server-side scripts, which could use any kinds of programming languages such as Java and Perl. It gives developers great flexibility, but the security is a major concern.

  1. Check the CGI Help Pages.

  2. Apply for a UND Engineering Linux Account.

  3. Log in to the Linux Server undcemcs02.und.edu .

  4. Create a Sub-Directory “~/public_html/cgi-bin” under the Root Directory.
  5. Create the directory by using the command mkdir:
      undcemcs02> mkdir ~/public_html 
      undcemcs02> mkdir ~/public_html/cgi-bin 
    where the symbol ~ is the user’s root in the Unix file system.

  6. Execute the Command chcon .
  7. Execute the following command at the server undcemcs02. Otherwise, your CGI scripts will not work.
      undcemcs02> chcon -R -t httpd_sys_script_exec_t \ 
                  /home/”USER.ID”/public_html/cgi-bin 
    Swap out “USER.ID” with your login name:
      undcemcs02> chcon -R -t httpd_sys_script_exec_t \ 
                  /home/first.lastname/public_html/cgi-bin 
    (or replace the above two commands by the following two commands:
      undcemcs02> chcon -R -t httpd_sys_script_exec_t /home/”USER.ID”/public_html/cgi-bin 
      undcemcs02> chcon -R -t httpd_sys_script_exec_t /home/first.lastname/public_html/cgi-bin 
    where the backslash symbol ‘\’ is used to continue the current command to the next line.)

  8. Create a CGI Script.
  9. Create/edit a CGI script in the cgi-bin directory. The cgi-bin directories are set up so that they do not serve plain HTML pages; they just execute a given CGI script. An example of a CGI Perl script is ~/public_html/cgi-bin/demo/cgi/1.pl. Check the CGI Programming 101 for how to write CGI scripts.

    Note that a compiler, activated in a CGI script, can not successfully generate an object file if the source program results a warning message.

  10. Open the CGI Script.
  11. This step is NOT needed if the default permission of files is open or the web server is the script’s owner:
      undcemcs02>       
    Otherwise, open the CGI scripts to let the browser access them by using the command chmod :
      undcemcs02> chmod 755 ~/../user.id 
      undcemcs02> chmod 755 ~/public_html/ 
      undcemcs02> chmod 755 ~/public_html/* 
      undcemcs02> chmod 755 ~/public_html/cgi-bin/ 
      undcemcs02> chmod 755 ~/public_html/cgi-bin/* 
    where user.id is your Linux account identifier.

    Note that the above commands may not be complete. You have to open all the files and directories in the public_html directory.

    If CGI writes to your directory, such as ~user.id/public_html/out/, then you need to open that directory. For the example of that directory:
      undcemcs02> chmod 755 ~/../user.id 
      undcemcs02> chmod 755 ~/public_html/ 
      undcemcs02> chmod 777 ~/public_html/out/ 
    Note that if you write the file to the directory where the CGI script is, this will cause “Internal Server Error,” that is, try NOT to do the following:
      undcemcs02> chmod 777 ~/public_html/cgi-bin/ 
       
  12. View the Execution Results of the CGI Scripts.
  13. You can then check the execution results of your CGI scripts on the browser. For the example of the CGI script ~/public_html/cgi-bin/demo/cgi/1.pl, submit one of the following buttons:

           




      Grandpa, why don’t you have any life insurance?    
      So you can all be really sad when I die.