WWW FAQs: Why won't my web server run my CGI script?

2005-07-05: CGI scripts written in Perl and other scripting languages are not automatically run by all web servers. If the web browser displays the source code of your script instead of the output the script generates, then one or more of the following is true:
  • Your web server expects CGI scripts to be in a certain location, such as a cgi-bin directory. Your script is located somewhere else.
  • Your web server recognizes CGI scripts by their file extension, such as myscript.cgi or myscript.pl. Your script has the wrong file extension.
  • Your script is on a Unix (or Linux) host, and you did not make the script executable with the chmod command.
  • Your web host doesn't allow CGI scripts at all.
"But I see my CGI script code in the browser! I must be almost there!"

Unfortunately, no. CGI scripts execute on the server. The browser should never see the CGI script code. You need to configure the server correctly, or name and set the permissions of your script correctly to match the configuration that has already been done on your server. If you are seeing your CGI script code in your browser, then your server has not been configured to recognize the CGI script and treat it as a program.

If you run your own web server, see the official Apache 2.0.x CGI HOWTO and the official Apache 1.3.x CGI HOWTO. If you are running Internet Information Server, see how do I configure Microsoft Internet Information Server to allow CGI programs?

If you don't run your own web server, talk to the system administrator and find out how it recognizes CGI programs. Note: If you're using an inexpensive web host, there's a good chance CGI simply isn't included in your service.

PHP is an easier approach for beginning web developers - and advanced web developers too. In most cases, there isn't much advantage to using CGI anymore. Those hosting their content on Windows servers will want to consider ASP, but many Windows web hosts also support PHP, and you can take PHP skills to Unix/Linux later.

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!