LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash/CGI script won't execute - error 500 (http://www.linuxquestions.org/questions/programming-9/bash-cgi-script-wont-execute-error-500-a-4175414030/)

Annielover 06-29-2012 12:29 PM

Bash/CGI script won't execute - error 500
 
Hi all,

I want to display the output of a bash script on a html page using CGI,
but I can't get it working... I get an "500 internal server error".
I changed the default document root to "/home/www".
According to the error.log file Apache can't find the .sh file or am I wrong?
But why does it say "/www/cgi-bin/sysinfo.sh" instead of "/home/www/cgi-bin/sysinfo.sh"?

And there is something wrong with the permissions too, despite the 755 permission on the script and the cgi-bin folder...

Any help?

Apache2 error.log:
Code:

/home/www/cgi-bin/sysinfo.sh: line 2: /home/www/cgi-bin/sysinfo.sh: Permission denied
chmod: cannot access `/www/cgi-bin/sysinfo.sh': No such file or directory
[Fri Jun 29 18:11:13 2012] [error] [client 192.168.1.32] Premature end of script headers: sysinfo.sh

Apache2 default site:
Code:

<VirtualHost *:80>
        ServerAdmin laurentdw007@hotmail.com

        DocumentRoot /home/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /home/www/cgi-bin/
        <Directory "/home/www/cgi-bin">
                AllowOverride None
                Options ExecCGI MultiViews SymLinksIfOwnerMatch
                AddHandler cgi-script cgi pl
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

Directories:
/home
Code:

drwxr-xr-x  3 root      root      4096 2012-06-29 18:07 www
/home/www
Code:

drwxr-xr-x 2 root root    4096 2012-06-29 12:57 cgi-bin/
-rw-r--r-- 1 root root    2532 2012-06-29 18:07 index.htm

/home/www/cgi-bin
Code:

-rwxr-xr-x 1 root root  665 2012-06-29 12:57 sysinfo.sh
sysinfo.sh
Code:

cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html class="background"><head><link rel="stylesheet" href="../index.css" type="text/css" /><title>system information for $(hostname -s)"
echo "</title></head><body>"
echo "<h1 class="center">General system information for host $(hostname -s)</h1>"
echo ""
echo "<h2>Memory Info</h2>"
echo "<pre> $(free -m) </pre>"
echo "<h2>Disk Info:</h2>"
echo "<pre> $(df -h) </pre>"
echo "<h2>Logged in users</h2>"
echo "<pre> $(w) </pre>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
EOF

Thanks!!

lithos 06-29-2012 01:27 PM

Quote:

Originally Posted by Annielover (Post 4715082)
Hi all,

I want to display the output of a bash script on a html page using CGI,
but I can't get it working... I get an "500 internal server error".
I changed the default document root to "/home/www".
According to the error.log file Apache can't find the .sh file or am I wrong?
But why does it say "/www/cgi-bin/sysinfo.sh" instead of "/home/www/cgi-bin/sysinfo.sh"?

And there is something wrong with the permissions too, despite the 755 permission on the script and the cgi-bin folder...

Apache2 default site:
Code:

<Directory "/home/www/cgi-bin">
                AllowOverride None
                Options ExecCGI MultiViews SymLinksIfOwnerMatch
                AddHandler cgi-script cgi pl    <-- the files you treat as CGI-BIN are: .cgi , .pl (not .sh which you have)
                Order allow,deny
                Allow from all
        </Directory>

sysinfo.sh
Code:

cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html class="background"><head><link rel="stylesheet" href="../index.css" type="text/css" /><title>system information for $(hostname -s)"
echo "</title></head><body>"
echo "<h1 class="center">General system information for host $(hostname -s)</h1>"
echo ""
echo "<h2>Memory Info</h2>"
echo "<pre> $(free -m) </pre>"
echo "<h2>Disk Info:</h2>"
echo "<pre> $(df -h) </pre>"
echo "<h2>Logged in users</h2>"
echo "<pre> $(w) </pre>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
EOF

Thanks!!

Try CGI-BIN with Perl
- Hello world example in perl
- perl test example.

Then it should work.

- set up cgi-bin in apache

Or try google for more

good luck

sag47 06-29-2012 03:25 PM

What distro are you using? If you're using something like Fedora or CentOS then they come with SELinux enabled by default and you need to set the security context in order for httpd to execute cgi...

SAM

Annielover 07-02-2012 10:03 AM

I'm using Ubuntu Server 10.04 LTS

Annielover 07-02-2012 10:15 AM

Hi, I shortened the test script a little and this is what I get:
(but it still doesn't work...)

Code:

[Mon Jul 02 16:01:31 2012] [error] (8)Exec format error: exec of '/home/www/cgi-bin/sysinfo.sh' failed
[Mon Jul 02 16:01:31 2012] [error] [client 192.168.1.32] Premature end of script headers: sysinfo.sh

Bash script
Code:

cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html>"
echo "<head>"
echo "<title>system information for $(hostname -s)</title>"
echo "</head>"
echo "<body>"
echo "<h1>General system information for host $(hostname -s)</h1>"
echo "</body>"
echo "</html>"
EOF

When I execute the script from the terminal, this is the output:
(which is fine, isn't it?)
And I don't get any error messages at all
Code:

#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html>"
echo "<head>"
echo "<title>system information for server</title>"
echo "</head>"
echo "<body>"
echo "<h1>General system information for host server</h1>"
echo "</body>"
echo "</html>"

I also changed the default site:
(changes are marked in red)
Code:

<VirtualHost *:80>
        ServerAdmin laurentdw007@hotmail.com

        DocumentRoot /home/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /home/www/cgi-bin/
        <Directory "/home/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .cgi .sh

                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

According to the error log, the script isn't executed, but WHY NOT? It is also the only script in the "cgi-bin" directory...
Any help?
Thanks!!

Annielover 07-02-2012 10:24 AM

Nvm, I got it working:

Just removing "cat > /home/www/cgi-bin/sysinfo.sh << EOF" works fine for me!

lithos 07-02-2012 12:31 PM

Quote:

Originally Posted by Annielover (Post 4717150)
cat > /home/www/cgi-bin/sysinfo.sh << EOF

this can never work, it was so obvious, that probably most of us overlooked it ! Damn.

Because if you run "cat" command it expects a filename to parse (and you have just redirected the output to "sysinfo.sh"), without it, it doesn't do anything.


All times are GMT -5. The time now is 11:17 AM.