If you are using apache2, are these the content there in your file /etc/apache2/sites-available/default
Code:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
Restart apache2 after putting it there.
If you are using CentOS, have you checked following code in /etc/httpd/conf/httpd.conf
Code:
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
#"/var/www/cgi-bin" should be changed to whatever your ScriptAliased CGI directory exists,if you have that configured.
<Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
Restart httpd after enabling this code.
Have you executing it in following way?
http://localhost/cgi-bin/filename
or
http://server-IP/cgi-bin/filename
Your code is working fine on my apache.