CGI Scripting

From HE FAQ
Revision as of 23:13, 17 May 2010 by Mestes (Talk | contribs)

Jump to: navigation, search

CGI Scripting

We currently run Perl5 for CGI scripting. Compiled C/C++ CGI executables are allowed, along with other available language interpreters (PHP, for example).

CGI scripts are a way of using programs to both generate web pages dynamically and process user input. You will need to learn a little about how to program in a Unix environment either in C or a script language like Perl.

You can call a program written in C/C++ as a CGI script. To compile the code on our servers, you will need to log onto the server using ssh and use the GCC compiler suite.

We provide the environment so that you can develop scripts and programs. However, you are completely responsible for your development efforts.

In general, when you are modifying an example program, which is now failing but worked when you started, go back to the example exactly, copy it, and then modify it slowly until it stops working, then you will know what you did that caused the bug.

Here are a few things to check:

  • Be sure to upload your scripts with your SFTP client in text/ASCII mode. Unix uses LF (line feed) to as an end of line marker, Macs use a CR (carriage return) by itself, Windows/DOS uses both CR+LF. Therefore, if you're not on a Unix system, it's important to ensure that your SFTP client makes the line ending change for you. If you use a command line SFTP client, type "ASCII" before you send it. If you are using some form of GUI SFTP client, be sure that the ASCII transfer option is enabled. Please remember to switch back to binary before you download any executables.
  • Set your script to executable. "chmod 700" will be the usual command permissions you'll want for a CGI script.
  • If your script is an interpreted script, ensure the first line of the script invokes the proper interpreter. If your script is a Perl script the first line must be "#!/usr/bin/perl" or "#!/usr/local/bin/perl". If your script is a Bourne shell script you first line must be "#!/bin/sh". If your script is using another interpreted language check to make sure that the path name specified is valid for the interpreter you're using. Also, make sure that it is the first line in the script, and that the script was uploaded in ASCII mode.
  • Try running your script from the command line. This will provide a basic syntax check of your script, and possibly more, depending on how much of the code relies on parameters or environment variables set by the web server. However, some scripts may produce run-time errors when invoked in this manner. If possible, modify your script to fail politely when run at the command line so that you can use this technique to debug it. If you are a more sophisticated programmer, you can write a test script to set the necessary environment variables and invoke your main script. If your script is Perl, running it from the command line will print out any syntax errors. If it works from the command line, but fails with an error 500 when you call it, it may be failing to open a file. One common gotcha is the use of tilde (~) within a Perl script to reference your home directory. Just use "/home/username/".
  • Server side include scripts (the "exec cmd" variety) need to use a full path name to be able to correctly locate your file. For convenience server side includes are usually put in your personal cgi-bin directory. The path name will be something like "/home/username/cgi-bin/userscript".
  • When in doubt: simplify. Reduce the number of subroutines and the amount of code involved to verify that you can invoke your script at all. Then as you can verify that you are getting to a certain point, start adding code back in.

You need not ask us to enable CGI scripting for your account as we provide you with your own personal cgi-bin into which you can install and modify scripts at your leisure. You do, however, need to have a Starter Virtual Host or higher account. All we ask is that you follow the guidelines on the user scripting page available at http://www.he.net/faq/tutorials, with the key one being respect for server resources.

We do not offer any search scripts. You are welcome to install a CGI script to do this. There are several general search packages for Unix which could be invoked via a CGI script.

We support user CGI scripting and you can install your own form processing CGI script. NMS FormMail is one of many such scripts we know works with our service for handling email form processing.

We have MySQL available to all accounts. There are many different publicly available CGI front-ends available for it. We provide phpMyAdmin on version3 servers at http://yourdomain/phpmyadmin/ and https://servername.he.net/phpmyadmin/. A MySQL database is included with all accounts. Your database name is the same as your account name. Your MySQL database is initialized when your account is created.

See our MySQL FAQ or [1]MySQL Mirror Site for more information on using MySQL.

Due to performance considerations, we do not offer support for ASP or JSP.

You are welcome to install your own set of additional Perl modules. When you build modules, use the PREFIX option when generating Makefiles:

perl Makefile.PL PREFIX=/home/mydir/perl

then either set the PERL5LIB environment variable before you run scripts that use the modules/libraries (see the perlrun manpage) or say:

use lib '/home/mydir/perl';