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. Use VPN (Virtual Private Network) to connect to the UND networks.
  4. You may not need to do this if you are on campus.

  5. Log in to the Linux server undcemcs02.und.edu .

  6. Create a sub-directory “~/public_html/cgi-bin” under the root directory.
  7. Create the directory by using the command mkdir:
      shell> mkdir ~/public_html 
      shell> mkdir ~/public_html/cgi-bin 
    where the symbol ~ is the user’s root in the Linux file system.

  8. Execute the command chcon .
  9. Execute the following command at the server undcemcs02. Otherwise, your CGI scripts will not work.
      shell> chcon -R -t httpd_sys_script_exec_t /home/“USER.ID”/public_html 
    Swap out “USER.ID” with your login name:
      shell> chcon -R -t httpd_sys_script_exec_t /home/first.lastname/public_html 

  10. Create a CGI script.
  11. 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.

  12. Open the CGI script.
  13. This step is NOT needed if the default permission of files is open or the web server is the script’s owner:
      shell>       
    Otherwise, open the CGI scripts to let the browser access them by using the command chmod:
      shell> chmod 755 ~/../user.id 
      shell> chmod 755 ~/public_html/ 
      shell> chmod 755 ~/public_html/* 
      shell> chmod 755 ~/public_html/cgi-bin/ 
      shell> 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:
      shell> chmod 755 ~/../user.id 
      shell> chmod 755 ~/public_html/ 
      shell> 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:
      shell> chmod 777 ~/public_html/cgi-bin/ 
       
  14. View the execution results of the CGI scripts.
  15. 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: