Using PHP, Perl, and Python

ITS supports the scripting languages PHP, Perl, and Python on most campus web sites. Bingweb URLs which use PHP, Perl, and Python will automatically be redirected to the Linux server, Harvey, since Bingweb does not support these languages and Harvey does. Bingweb and Harvey point to the same network file space so your files go in the same place regardless of whether you are using the Bingweb or Harvey URL.

Basic Script Requirements

Certain requirements must be met so that your PHP, Perl, and Python scripts will work properly on the campus web servers. Failure to follow these rules will often lead to the generic "Internal Server Error" message when attempting to view your page.

Location

On Bingwww and Bingdev scripts can reside anywhere on your account. There are no special rules for scripts that also reside in a user's cgi-bin directory.

On Bingweb and Harvey scripts must be in your public_html directory just like any HTML file. Scripts which also reside in a user's cgi-bin sudirectory are not subject to the file extension rules described below. That is, scripts in cgi-bin directories are assumed to be CGI scripts and do not need any special file extension to run.

File Extensions

In addition to the location, the method used to run the script can depend upon the file extension.

On Bingwww and Bingdev PHP, Perl, and Python will all run in CGI mode if they have a .cgi extension. In addition, Perl scripts will also run in CGI mode if they have a .pl extension. PHP scripts that have a .php extension will run as an Apache module.

On Bingweb and Harvey PHP, Perl, and Python will all run in CGI mode if they have a .cgi extension. PHP scripts that have a .php extension and Perl scripts that have a .pl extension will run as an Apache module.

Permissions

Scripts that are run in CGI mode must be executable at least for the user. Scripts run as an Apache module do not need to be executable. CGI scripts must also have as their fist line the magic shebang (#!) line which indicates the location of the script binary.

The table below summarizes the basic script requirements.

Scripting Language Server Location Mode File Extension Shebang (first line)
PHP Bingwww, Bingdev Any Apache Module .php none
PHP Bingwww, Bingdev Any CGI .cgi #!/usr/local/bin/php
PHP Bingweb, Harvey outside cgi-bin directory Apache Module .php none
PHP Bingweb, Harvey outside cgi-bin directory CGI .cgi #!/usr/bin/php
PHP Bingweb, Harvey inside cgi-bin directory CGI none required #!/usr/bin/php
Perl Bingwww, Bingdev Any CGI .cgi or .pl #/usr/bin/perl
Perl Bingweb, Harvey outside cgi-bin directory Apache Module .pl none
Perl Bingweb, Harvey outside cgi-bin directory CGI .cgi #!/usr/bin/perl
Perl Bingweb, Harvey inside cgi-bin directory CGI none required #!/usr/bin/perl
Python Bingwww, Bingdev Any CGI .cgi #!/usr/bin/python
Python Bingweb, Harvey Any CGI .cgi #!/usr/bin/python

 

Troubleshooting

Transferring Files

Transferring CGI scripts between different operating systems can create problems. The problem occurs with the end-of-line characters. With UNIX operating systems (MacOS X, Linux, Solaris) the end-of-line character is a line feed (LF). In Windows, the end-of-line character is really two characters, a carriage return followed by a line feed (CRLF). If you use an FTP client you have two modes of transferring your files, ASCII mode and binary mode. In ASCII mode the end-of-line characters are translated for you. In binary mode, the end-of-line characters are left as-is. If you use binary mode to upload a text file from your Windows computer to one of our UNIX web servers, each end-of-line carriage return will show up as a control-M. For an HTML file this doesn't cause a problem, though it looks a bit ugly when viewed in the UNIX text editor, vi; however, with a CGI script, that extra control-M at the end of the shebang (#!) line will break it. To avoid this problem, you need to transfer your text files (including CGIs) using ASCII mode. Photos, audio, videos, etc... should be transferred in binary mode as you don't want any translation taking place that will corrupt them.

If you are using Dreamweaver to transfer your files, choose "Code Format" from the Preferences menu and set the "Line Break type" to "LF (Unix)". This only needs to be done once.

Dreamweaver Code Format

 

Command Line Execution

When debugging your script, it often helps to run it from the command line before viewing it through a web browser until you get it error free . Often the only error message you will get from the web browser if there is a problem with your script is "Internal Server Error". This is a generic error message which isn't very helpful in identifying the cause of the problem. To run your script from the command line, log in to your account, change to the directory where your script is located and issue the following command:

./myfile.cgi (where myfile.cgi is the name of your CGI script)

This will execute your script and you should see any syntax error messages on the screen. When your script runs without error, it is time to try it out from your favorite web browser. Even though it may be syntactically correct, you may still have logic errors.

Last Updated: 8/13/14