Using CGIget help

This service is available to faculty, staff, student and sponsored accounts.

CGI-mode scripts can be run on the cgi.sfu.ca server using a variety of languages or scripting methods. We only support the use Perl and PHP, but other methods may work.

This page documents the use of Perl and PHP on cgi.sfu.ca, but the information is quite generic and most of the information can be applied to other methods should you wish to experiment with other languages, shell script approaches, etc.

Those of you using Perl for your CGI programs will find Perl at /usr/local/bin/perl on fraser.sfu.ca. That is, the first line of your program should read

#!/usr/local/bin/perl

If you use PHP, the first line would be similar, simply replace "perl" with "php" as in:

#!/usr/local/bin/php

The general rule for scripts running on cgi.sfu.ca is that the first line determines what program will handle the request from the web server. You'll often see or hear references to this first line by the name "shebang line" which is a reference to the first two characters "#!"

You have a limited quota of disk space under your SFU Computing Account, some of which you may already be using for other purposes. To check out how much space you are using and how much you have available, log into fraser.sfu.ca and use the quota command:

quota -v

This will return how much disk space you are using and what your quota is, in kilobytes. For example, the usage number 12718 would indicate that you are using about 12 megabytes.

CGI scripts must run under one of two URLs:  http://cgi.sfu.ca or the "secure" server URL: https://cgi.sfu.ca; generally CGI-based scripts are not available on www.sfu.ca.  The only difference between the two is that traffic sent between a browser and your script will be encrypted when you set your site up to run under https://cgi.sfu.ca, so it's a better choice if you're expecting people to send sensitive information such as personal data to your site.

Directories (folders) and permissions for your CGI scripts

For security reasons, CGI scripts need to be placed in a directory that has access limited only to your account, and the permissions for the folders that contain your scripts, as well as your scripts themselves should be set appropriately.

CGI scripts should be placed in /home/yourAccount/pub_html/cgi-bin/ (if you use the non-secure URL), or /home/yourAccount/sec_html/ (for the secure URL)

Generally directories starting at cgi-bin or sec_html and any directories below those should have their permissions set at "711" with the chmod command; if you are using a graphical tool to set the permissions, set the owner's permission to "read, write and execute" for the owner, and "execute" only for "group" and "other"

cd ~/pub_html/
chmod 711 cgi-bin

or, for the secure server:

cd
chmod 711 sec_html

Your files should be set to more restrictive permissions: "read, write and execute" for the owner only, or mode "700". For example, for a script called helloworld.php, you'd use these commands:

cd pub_html/cgi-bin
chmod 700 helloworld.php

Non-secure server

Log into fraser.sfu.ca. Normally you will already have an existing directory called pub_html, and a cgi-bin directory within pub_html.  If you don't have those directories you can create them by: 

make a directory called pub_html:

mkdir pub_html

Permit this directory as follows:

chmod 755 pub_html

Change into this directory:

cd pub_html

Create a sub-directory called cgi-bin within pub_html to hold your CGIs:

mkdir cgi-bin

Permit cgi-bin like this:

chmod 711 cgi-bin

Place your CGI programs in the cgi-bin sub-directory (using a Unix editor, or transfer into the directory using scp or FTP). Your CGI files must end in the suffix .cgi.

Permit your CGI files like this:

chmod 700 filename.cgi

The URL for your CGI program will be:

http://cgi.sfu.ca/~yourID/cgi-bin/filename.cgi

Replace yourID with your SFU Computing ID.

Note the server name is cgi.sfu.ca NOT www.sfu.ca.

Secure Server

Log into fraser.sfu.ca. Create a directory called sec_html:

mkdir sec_html

Permit this directory as follows:

chmod 711 sec_html

Place your CGI programs in this directory (using a Unix editor, or transfer into the directory using scp or FTP).

Your CGI files must end in the suffix .cgi.

Permit your CGI files like this:

chmod 700 filename.cgi

The URL for your CGI program will be:

https://cgi.sfu.ca/~yourID/filename.cgi

Replace yourID with your SFU Computing ID.

Note the server name is cgi.sfu.ca NOT www.sfu.ca, and you call it with https NOT http.

Very Simple Example of a PHP script

The following is an example of creating a very simple PHP script to display a “Hello World” message.

  1. Put the following lines into a file called helloworld.php in the directory pub_html/cgi-bin in your account on fraser.sfu.ca:

     

    #!/usr/local/bin/php
    <?
    $message="Hello World"
    echo $message;
    ?>
  2. Make sure that your file is permitted in such a way that other users on fraser cannot view the code (it could contain important information such as special codes or passwords):

    chmod 700 helloworld.php
  3. Suppose your SFU Computing ID on Fraser is kipling. The URL for the sample described above would be:

    http://cgi.sfu.ca/~kipling/cgi-bin/helloworld.php

Troubleshooting

If you are transferring your Perl program from another system (that is you are not creating the .cgi file directly on fraser using a text editor there), a common problem is that the file does not have the correct line-ending characters in it. fraser (cgi.sfu.ca) is a Unix machine and expects line-ending characters to be in Unix format.

If you create your .cgi file on a Microsoft Windows system, for example, and then transfer it to fraser in binary mode, fraser will not understand where line endings are in your file and you will get a “Server Failed” message.

To prevent or fix this problem try one of the following methods:

  • Ensure your FTP or scp file transfer program is set to send your .cgi files in Text (also sometimes called ASCII) mode. When a file is transferred in Text mode, the line ending characters are automatically translated for you.
  • Use a text editor that allows you to set the type of line-ending characters to use. Usually a programmer’s text editor or web publishing text editor will allow you to set the line ending type appropriately. The appropriate setting for fraser/cgi.sfu.ca is “Unix”
  • You can also use the dos2unix command on fraser to translate your file from Windows/DOS line-endings to Unix. Assuming your file was in your pub_html/cgi-bin folder and called mytest.cgi, you could convert that file to Unix from Windows/DOS called mytestnew.cgi by using the following commands:

    cd ~/pub_html/cgi-bin 
    dos2unix -ascii mytest.cgi > mytestnew.cgi
    chmod 700 mytestnew.cgi

    The final chmod command sets the permissions for the new file appropriately so that the file is executable on the web since the dos2unix command doesn’t copy the permissions for the source file to the new file.