Welcome, Guest. [ Log In ]
Question   What is an internal server error, and why doesn't my CGI work?
Search KBase

Top 5 in this Area:
1. How can people subscribe to my mailing list?
2. Do you have this perl module installed on your servers?
3. What's the difference between PHP-CGI and PHP as an Apache module?
4. PHP Security
5. How do I dump data into MySQL?

What is an internal server error, and why doesn't my CGI work?
I've set up a couple of CGI scripts that I call from one of my pages, but it always returns a message saying 'Internal Server Error'. It doesn't actually seem to do anything. What do I do?

A CGI is a program or script that interfaces with the web server in order to provide some sort of extra functionality to a web site. Some CGIs provide counters or guestbooks, while some of the more ambitious ones provide feature-rich bulletin boards or search engines for your site's visitors. CGI is one of the most powerful tools you can use to create a professional, dynamic web experience for your target audience.

One very common problem when setting up a CGI is the error message 'Internal Server Error'. This is a generic message meaning that somewhere along the line, the script or program being run could not successfully complete the task.

For Perl scripts (the most common way to write CGIs), this can be one of a few things:
  • Often, scripts edited on Macs or Windows-based PCs will contain carriage return characters, which the Perl executable does not deal well with.
  • Also, any syntax error or mistake in the program's code which does not allow it to execute properly will result in this error.

Debugging CGIs

The best way to debug any CGI is to run it from a command line by using telnet to log onto the server, changing into the directory which the CGI resides in, and running it. For example, typing this:



perl scriptname.cgi (only with Perl scripts)

...will run the CGI and return whatever is meant to be sent to the user's web browser (usually a messy glob of HTML tags).

If you run a Perl script which is unable to be interpreted successfully, you will often receive a message stating the general problem and the line number it occurs on. Usually it is best to fix the first error that comes up, as that will often help with errors that occur further down the script.

Some scripts require input to function, usually provided by an HTML form. Since debugging from the command line does not provide you with such luxuries, you will be prompted for name and value pairs instead. Specify these on a separate line per pair, with an equal sign between the name and value. We suggest that value strings be wrapped in quotation marks. After the last line, hit the return key and then type control-D, which will run the script with those values. For example, these are three valid name/value pairs:


Carriage Returns In Perl Scripts

Perl scripts in particular will occasionally mention a problem caused by carriage returns within the script. Perl has problems executing files with carriage returns, which are usually caused by scripts being edited in many Mac or Windows PC based text editors (which natively use carriage returns to mark line endings).

The quickest way to fix the problem is to open the file up in pico, a Unix/Linux based text editor, and resave it. To do so, type this into the command prompt while in the same directory as the script:

pico scriptname.cgi

(replacing 'scriptname.cgi' with the name of your script)

This will open up the file in pico. Now, make a minor change (such as adding a space) and erase it. Then, quit pico and save when prompted by typing control-X. When pico saves files, it strips them of carriage returns.

However, this will only fix a single file. In the long term, you should find a text editor that gives you an option to save with 'Unix-style' line endings. For the Macintosh, Bare Bones BBEdit (or the free BBEdit Lite) is regarded as the best text editor to use.


For Windows-based PCs, a popular text editor that many use is Allaire HomeSite. A trial version is available here:


For the BeOS, a very well done text editor called 'Pe' is available. Those familiar with BBEdit for the Macintosh will feel right at home using this top-notch text editor.


Good text editors are available for just about any platform. If you need help finding one, please let us know.

Special considerations because we run suexec!

Normally on a web server, CGI scripts run as the special unix user nobody in the group nogroup. That means that in order for the cgi to write or read files, they must be readable or writable to the world. This isn't really that safe, since that means any other user on your server can read and write those files too!

On DreamHost servers, we have a special feature called suexec (which stands for "switch user execution") turned on that makes your CGI scripts on your web site run as though they were run by your user and group!

As a security precaution, suexec REQUIRES that all cgi scripts AND THE DIRECTORIES IN WHICH THEY RESIDE *NOT* be writable by anyone but the owner user. Otherwise, another user on your machine could go into the directory, edit your script to do something, then visit it from the web and they would then have access as though they were you! Then, they would essentially have full access to your user account, and that's bad!

SO, suexec requires that you change the owner (chown) and change the group (chgrp) to be your user and group (don't worry, these are the defaults when you upload or create a file), AND that you chmod (change permissions) that file AND THE DIRECTORY it resides in to be not-writable by the world. You can do this with the

    chmod 755 filename.pl

command. You do NOT want to do chmod 777 filename.pl, even if your scripts' installation instructions tell you to do that. They don't know that you're installing your script on a server with suexec installed.

The various chmod modes are fairly complicated, but there is a good explaination of the chmod command which you can view here:

http://www.analysisandsolutions.com/code/chmod.htm So if you've tried everything else above, and your script keeps giving a 500 error (Internal server error), and ESPECIALLY if it worked before on a different (non-dreamhost) machine.. directory and file permissions may be the problem!

Check the error log!
tail -f /home/username/logs/domain.com/http/error.log

a line like this:
[Wed Mar 12 11:26:42 2003] [error] [client] 
Premature end of script headers:

would indicate the the script headers:
Content-Type: text/html; charset=ISO-8859-1

aren't being printed correctly.  here is a basic working script:


use CGI;
my $query= new CGI;

print $query->header;

print "hello people in my head\n";

Last updated: Mar 12, 2003.

User Post (2005-12-27 06:11:00 by jjmcc)
to add to what "mahuti" said about ftp apps being able to do this with out doing a chmod...
If you just use internet explorer to ftp you can simply right click the folder and select properties then click the "write" checkbox for "all users". just remember that this opens up the ability for EVERYONE to be able to write to the directory. (they still wouldn't be able to do anonymous ftp though)
User Post (2005-10-08 14:02:52 by yost)
The cgi directory and all cgi executable files must be group-owned by your NATIVE group. Your native group is listed first when you run the 'groups' command. I started getting [an error occurred while processing this directive] in my web pages until I stubled on this change.
User Post (2005-08-19 10:36:18 by euqsam)
Be aware that since the Sarge upgrade, files must now be in your NATIVE group - that is, the group listed in /etc/password for you - and may NO LONGER be owned by a group you're merely a member of. (This is a huge and frustrating change for some of us.) So if you're going to run scripts as you, you must chgrp all files executed to your native group. No more shared write access, even to trusted folks. Unless they're also in your native group. Argh.
User Post (2005-08-03 00:57:50 by erthian)
Wow! This totaly answered my question! Thanks again for providing great support.
User Post (2005-07-29 14:34:48 by t11s)
I'm assuming the chmod 755 rule only applies to cgi scripts? What about PHP? Are those ok to 777?
User Post (2005-06-02 08:38:39 by amandabee)
I was not getting anywhere with these instructions until I moved all the way up to my home directory (/home/[username]) and did "chmod -R 755 [directory]" on the whole directory for the whole dang domain.
User Post (2004-12-20 02:43:16 by michal)
This is great! Thanks!!
I have just started digging in Perl, with a lot of help from HTMLGoodies.com (recommended). All I did was pasting a ready made script in my directory, and I got this error. So I went through your troubleshooter and after cleaning all <cr> using Pico, and chmoding my dir to 755, it now works!!<standing ovations> Hurray Dreamhost! you are the best.</standing ovations>
User Post (2004-12-11 02:05:18 by skallas)
Wow, SFTP does break the ascii conversion. Use FTP I guess. This is a serious bug.
User Post (2004-12-04 20:21:57 by hoggardb)
You don't have to use find to recursively change a directory, and the version above doesn't handle filenames with spaces. Just use the -R recursive flag for chmod:

chmod -R go-w directoryname
User Post (2004-04-08 20:07:10 by priss)
Thanks for these tips. Some oncoming headaches were avoided!
User Post (2003-12-31 10:22:04 by isaac32767)
Here's a shell command that will turn off public writability for all files and directories in a hierarchy:

find . -print | xargs chmod go-w

Always do this when you install files from a zip archive or tarball. Unzip and tar preserve the original permissions of a file, and many canned CGI applications assume you need group writability -- something you never want on DreamHost.

User Post (2003-12-12 01:40:33 by visitus)
I put together what I hope is a better article to answer the question, "What is an internal server error, and why doesn't my CGI work?"

User Post (2003-10-13 19:35:06 by necco)
For some reason, ASCII mode doesn't work well when connecting via SFTP using WS_FTP for Win2000. It somehow makes the file appear as though it was uploaded in Binary mode and causes Perl errors. I had the same problem when installing the Gallery php script. Resolution: When installing ASCII files, use ASCII mode in FTP mode, not in SFTP mode.
User Post (2003-07-17 07:21:29 by mahuti)
You don't need unix to do CHMOD. You can use your FTP program. Somewhere in the average FTP program, you have access to something like "file inspector", "attributes", "info", it varies from program to program. Basically it will have 9 boxes you can check as on or off, these control the CHMOD of the file.
User Post (2003-07-02 05:05:14 by steelow)
Im a newbie to all this, can anyone tell me were to or how to access chmod to give permissions to my cgi, i cant how to anywere

User Post (2003-06-13 11:03:28 by alundeen)
The Allaire link just went to macromedia.com, and I couldnt find any links to it there, so I would recommend <a href="http://editpadpro.com/download.html">EditPadPro</a>. And corbettcyr, go to the users section under the users menu - click on "edit" for your user, and there you can allow telnet permissions for yourself.
User Post (2003-06-04 13:43:33 by pandc)
Ah! Great googly moogly! I got nailed by not having the permissions on the directory set as well. That definitely needs to be mentioned in this help file and not just down here in the anonymous notes.

find . -type d | xargs chmod g-w

Running that command in the shell will remove group write permissions for every directory under the one you are currently in.
User Post (2003-06-01 15:21:40 by corbettcyr)
Okay, for some reason I can't telnet to my account -- it says that only FTP access is allowed. And my (ultra-basic) script isn't working despite the fact that I've chmoded to 755 a zillion times.


User Post (2003-05-19 14:04:42 by pleasantonpizza)
I had my script permissions set to 755, but was still getting the "premature end of script headers" error. Turns out the DIRECTORY permissions need to be 755 also (the directory containing the script).
User Post (2003-04-02 00:43:21 by iamfez)
I'm trying to use Storyline v.1.8 but it says Chmod 777 to use it. Anyone know how I can get around this?
User Post (2003-03-12 23:15:44 by cachito)
The premature end of script headers may mean either you have your chmod set incorrectly or that you really have your script header wrong. Don't forget to check also the carriage return
User Post (2002-10-19 17:04:39 by robocop)
Anyone having a Greymatter Version Setup problem?
User Post (2002-10-11 00:10:44 by robocop)
Thank the Compu-Lord for you people, I would have assumed the "end of script headers" had nothing to do with chmod.
User Post (2002-10-03 18:55:38 by maxtar)
I'm having that exact error message... I tried the chmod 755 but still get the 500 error and the premature end of script headers... what can I do?
User Post (2002-09-09 10:06:20 by jandb)
Turns out my problem was that I hadn't specified the full path for a "required" file. Doing a pwd command to determine the full path to where the file was and then including that info in the requires statement cleared everything up.
User Post (2002-08-18 21:53:44 by garlog)
Thanks for the link diva. I had forgotten all about the "Content-type: text/html" requirement.
User Post (2002-07-20 04:18:46 by dagadaga)
Thank you awatters on your post 2001-08-05 for sharing the secret. I encountered the same problems as you did. I had "premature end of script headers" on my error log and then I did a chmod 755 filename.pl and the problem solved!!
User Post (2002-06-25 09:31:49 by rsz200)
make sure you put your files up as ascii
User Post (2002-05-04 11:01:26 by rdonkin)
I just had the permissions issue with suexec as well - this should be prominently mentioned, as the Dreamhost permissions setup (with a group that's per account) practically encourage you to have group-writeable files. It would really help if a more useful error message could be given, at least saying 'suexec failed to execute script', and preferably mentioning the permissions problem.

For anyone who has had this issue, just cd into your cgi-bin directory and do 'chmod 755 *' on your scripts. It won't hurt and it may help matters.

User Post (2002-04-20 15:33:55 by feedle)
Getting Greymatter to work on Dreamhost? Read http://www.moremayo.com/library/gm.shtml. (Note: this is not an official Dreamhost page, it's something I wrote up to help out frustrated fellow Dreamhosters).
User Post (2002-03-29 08:21:36 by hollis)
chmod 755 .
and the file name for subdirectorys

banging my head on the wall so you don't have to
User Post (2002-03-04 18:39:42 by tiffanybbrown)
Something to check: Make sure that the file(s) in question have proper line breaks. In one case, I had a script that I downloaded from the Web. Somehow, the line breaks were removed so that the script read as one big block of commented text --which of course made the whole thing not work.
User Post (2002-02-10 19:31:02 by jetblack)
what about using GreyMatter (weblog program: http://noahgrey.com/greysoft ) , where the archives folder must be chmod'd to 777 for the program to work ... ?

link to chmod info above is broken (www.dickinson...)
User Post (2001-08-16 13:00:02 by diva)
Another really good resource with more in-depth info on troubleshooting CGI scripts is "CGI: why things don't work" at <a href="http://www.raingod.com/raingod/resources/Programming/Perl/Notes/CGIDebuggi ng.html">http://www.raingod.com/raingod/resources/Programming/Perl/Notes/CGIDebu gging.html</a>
User Post (2001-08-05 17:30:36 by awatters)
The confusing "premature end of script headers" message in the apache logfile means that the chmod is set incorrectly. I discovered this after a few hours!