Running a Perl or CGI script on your hosting: permissions, line endings and the 500 that follows

cPanel & WHM··· 98 views· 4 min read

The short answer: Perl and CGI scripts (many old forms and counters) work on hosting, but they are demanding: the file must have execute permission (755), the first line must point to the right Perl, the lines must end as on Linux (LF), and the script must start by writing a header. Failing any one gives the same error: 500.

A minimal script that should work

Create a file called test.cgi with this content:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<h1>It works</h1>";

Upload it to the site’s cgi-bin folder (or to the folder where CGI is enabled), give it permission 755 and open your-domain.com/cgi-bin/test.cgi. If “It works” appears, the rest is your script.

The causes of error 500, in order

Cause How to recognise it What to do
The header is missing The log says “Premature end of script headers”. The first thing the script writes must be Content-type: text/html followed by an empty line.
Wrong permission The file has no execute permission, or is too open (777). Set 755 on the script and the folder: File permissions.
Wrong Perl path The first line (#!/usr/bin/perl) does not exist on this server. Confirm Perl’s path on your server (in a terminal, which perl) or ask us.
Windows line endings (CRLF) The script was written on Windows and uploaded as binary: the first line becomes “/usr/bin/perl^M”, which does not exist. Save the file with Unix line endings (LF) in the editor, or upload it in text mode by FTP.
An error in the code itself The log shows a line of the script. Run it in a terminal: perl -c test.cgi checks the syntax.
A missing Perl module “Can’t locate ... in @INC”. See the modules in cPanel, or ask us to install it.
Old scripts are a risk. Many were never updated and have known flaws (forms that let anyone send e-mail, for example). Before using them, see whether there is a current alternative: A safe, spam-free form. To read the log, see Reading the Errors pages.
For everything that runs here, Perl included, see Which technologies run here. And for error 500 in general: Error 500.

Does the script still give 500? Open a ticket with the address, the file’s path and the log line.

Open a support ticket

Where to see it in your account

We do not quote the Perl path here, because the account itself can tell you. Use these checks instead:

  • Perl path. If Terminal (or SSH Access) appears in your cPanel, the account has SSH: type which perl and use the answer in the first line of the script. If neither appears, put the usual path in test.cgi and, if it fails, open a ticket.
  • Is cgi-bin active? Do not assume it. Upload the test.cgi from this article to the cgi-bin folder with permission 755: if it prints the message, CGI works there.
  • Perl modules. If your cPanel shows a Perl Modules icon in the Software section, it lists the installed modules; if the icon is missing, ask us for the module you need.
  • The real error. The Errors page in cPanel shows the line from the log, such as Premature end of script headers.

SEE ALSO

Which technologies run here: PHP, Node, Python, Perl, games and the limits

Error 500 Internal Server Error: finding the cause, step by step

File permissions explained: what 755, 644 and 777 mean

Reading the Errors and Raw Access pages in cPanel when a page fails

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from R118.80/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.