Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Python script giving me a 500 Internal Server Error on nginx
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Python script giving me a 500 Internal Server Error on nginx

FreekFreek Member
edited January 2012 in Help

I'm trying to get a python script to run on my LEB, but it's giving me a 500 Internal Server Error.
I've uploaded it into the cgi-bin directory, but I had to create it manually as it wasn't there yet.
I'm running a LEMP stack and the script works as I tried it on my shared hosting package.
Python is running and I've got enough free RAM. Any tips in the right direction are well appreciated, as Google isn't giving me any clues.. or maybe I'm searching on the wrong terms.

Thanks!

«1

Comments

  • FranciscoFrancisco Top Host, Host Rep, Veteran

    Any chance you have error_log enabled?

    It's sounding like it doesn't know where python.

    Francisco

  • There's actually a very nice walkthru on what steps to take here:

    http://nixcraft.com/web-servers/15937-nginx-500-internal-server-error.html

    complete with what commands to run.

  • FreekFreek Member
    edited January 2012

    Thanks for the pointing me into the right direction @Francisco and @drmike.
    I've opened the errorlog and here's the corresponding error:

    2012/01/14 21:26:45 [error] 20208#0: *15 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 85.191.XXX.XX, server: localhost, request: "GET /cgi-bin/rooster.cgi?jaargang=2011&vakcodes=0I003 HTTP/1.1", host: "78.41.XXX.XX"
    

    I've Googled and found this solution but I'm a bit confused by it:
    http://forum.nginx.org/read.php?2,199260,207142
    Do I have to make a new rule for every directory I create? I certainly do not hope so.
    Also, how should this rule look like? I've uploaded the script into the cgi-bin directory I created. Should it look like this?

    location @cgi-bin {
    fastcgi_pass mypass
    fastcgi_param SCRIPT_FILENAME /cgi-bin/rooster.cgi;
    }
    

    And one last offtopic question, as I don't think it deservers it's own:
    I have disabled my root account for security reasons. In putty, I can get root by typing 'su' and then my password.
    In SFTP or SCP I can't get root so I cannot access all directories which is a pain in the a... sometimes.
    I found this article:
    http://winscp.net/eng/docs/faq_su#use_sudo_on_login
    And did was told over there (Added

    yourusername ALL=NOPASSWD: /usr/lib/openssh/sftp-server

    to /etc/sudoers') but that keeps giving me the same error as when you have set a password for your root account

    Error skipping startup message. Your shell is probably incompatible with the application (BASH is recommended).

    (in WinSCP).

    Thanks.

  • Giving this a small and humble bump as I would like to finish this project/installation before my midterms next week :)

  • Using CGI in nginx is not as straightforward as with apache. Maybe you can tell us what steps have you done, what fcgi did you use, your nginx config, the file permissions and the owner of the cgi file/directory, and perhaps the python code too. I've not tried python on web servers other than apache, but I might give nginx a try if I have time to kill. google leads me to http://wiki.nginx.org/SimplePythonFCGI , http://wiki.nginx.org/SimpleCGI and http://webpy.org/cookbook/fastcgi-nginx

  • @dnom thanks for the reply.

    As far as I can remember, I did an apt-get install python, chmodded the folder containing the script to 755 and that's pretty much it to be honest. I was hoping it would work out of the box ;)
    I used the included FastCGI with nginx, or am I saying a stupid thing right now?

    Thanks for the links.
    I tried what's described here:
    http://wiki.nginx.org/SimpleCGI
    But I get stuck at "If this all works, then the next part is to setup Nginx:".
    What file do I paste that code into? /etc/nginx/sites-available/default or
    /etc/nginx/nginx.conf? And where exactly? It's not clear to me.

    I also tried to follow this guide:
    http://webpy.org/cookbook/fastcgi-nginx
    Same problem. Where to save that nginx configuration? I'm confused as there are two places to 'configure' things.

    Sorry for asking such dumb questions but I really want to get things clear instead 'just doing something and hope it works', so that I can do it again in the future :)

  • dnomdnom Member
    edited January 2012

    @Freek said: I used the included FastCGI with nginx, or am I saying a stupid thing right now?

    I don't think nginx comes with it's own FCGI wrapper :)

    @Freek said: What file do I paste that code into? /etc/nginx/sites-available/default or

    /etc/nginx/nginx.conf? And where exactly? It's not clear to me.

    either of the two, you can even make another .conf (I suggest that you do) on the site-available folder.(it's conf.d for me) I'll give it a try later (or tomorrow) I just have lot's of things to do and I should not be LET'ing right now :D

  • @dnom said: I don't think nginx comes with it's own FCGI wrapper :)

    Yep, you need an external wrapper
    http://localdomain.pl/wiki/FcgiWrap

  • Alright thanks guys, then I'll just wait patently till after my exams, as I don't want to rush through this right now. I hope that you can find some time in the meantime @dnom :)

  • sleddogsleddog Member
    edited January 2012

    Another option is to run an instance of thttpd on localhost and a high port (e.g., 8000) and have nginx pass cgi's to it. Something like:

    location ~ \.cgi {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header  Host $host;
    }

    That's what I do here (though it's Bash scripts instead of Python).

    Thanked by 1yomero
  • The @sleddog option seems a little bit easier than the cgi wrapper =)

  • I did some informal testing (some time ago now) and found cgi performance with thttpd faster.

  • Okay, but wouldn´t it be better to run httpd instead of nginx then ?

  • @Freek said: Okay, but wouldn´t it be better to run httpd instead of nginx then ?

    thttpd != apache

    Thanked by 2tux Freek
  • @Freek said: Okay, but wouldn´t it be better to run httpd instead of nginx then ?

    By httpd you mean Apache, right?

    If you want one web server daemon that can do everything, then yes apache is better. But then there's the memory & performance debate about nginx vs. apache. :)

    If you want to run cgi with nginx there's (at least) two options, as discussed above.

    Running thttpd costs very little in terms of memory...

    [me@xxx:~] ps aux | grep thttpd
    www-data   981  0.0  0.6   2280   848 ?        Ss   Jan18   0:02 /usr/sbin/thttpd -C /etc/thttpd/thttpd.conf -i /var/run/thttpd.pid
    

    848kb memory usage. YMMV according to your traffic and what you're serving.

  • @dmmcintyre3
    Thanks, I did not know that. I thought the extra t infront of the httpd was a typo to be honest.. Heh, newbies ;)

    @sleddog
    Then thttpd looks like a good solution indeed. Care 2 share how you've set it up :) ?

  • sleddogsleddog Member
    edited January 2012

    @Freek said: Care 2 share how you've set it up

    If you're on a debian-derived distro then

    apt-get install thttpd

    Its config file will be at /etc/thttpd/thttpd.conf

    If you're running a single site then set the 'site' directive to the site's base directory -- same as nginx. Set 'cgipat' to a file-matching pattern for cgi files, e.g.,

    cgipat=**cgi

    Read the thttpd config options page for more details

    If you're running multiple sites that require cgi capabilities there's a few other things to do. Just say if that's the case for you...

  • Thanks sleddog, will look into this as soon I find some time on my hands.
    I'm just running 1 site that needs cgi :)

  • I've tried the SimpleCGI I got i working, spawns 5 processes with <3MB RSS each.
    Did not manage to run the script out of the box though, So even though I made it work, I still suggest you follow sleddog's suggestion. It's way easier and probably even more efficient.

  • http://freevps.us/t745 is how I made nginx proxy .cgi to thttpd and make it work with multiple virtual hosts.

  • @sleddog Did you also edit something in your nginx.conf or sites-enabled file? As I'm still getting a 500 internal server error.

    @dmmcintyre3 Thanks for the link, however I'm using Debian :)
    For instance, I cannot find the /etc/nginx/php file

  • @Freek said: Did you also edit something in your nginx.conf or sites-enabled file? As I'm still getting a 500 internal server error.

    Yes, as posted above. Here it is again. This goes in your nginx config file for the domain:

    location ~ \.cgi {
      proxy_pass http://127.0.0.1:8000;
      proxy_set_header  Host $host;
    }

    If your CGI files have an extension other than .cgi than you need to edit that and your thttpd.conf to match.

    Ensure thttpd is running and listening on the designated port...

    netstat -ltn

    You need to edit /etc/default/thttpd to allow thttpd to start.

    Don't forget to restart thttpd / nginx after making configuration changes :)

  • Thanks for the reply @sleddog :)

    We're getting somewhere. First I got this error:
    404 Not Found
    The requested URL 'blabla' was not found on this server.
    thttpd/2.25b 29dec2003
    But it's there for sure.

    File extension is .cgi, chmod is 755 and nginx and thttpd have been restarted. Do you have any ideas what this could be causing?

    Thanks!

  • Sounds like a misconfig in your thttpd.conf

    Post it up for a look... you might strip out the comments to shorten it up.

  • @sleddog

    # /etc/thttpd/thttpd.conf: thttpd configuration file
    port=8000
    dir=/home/freek/domains/freek.ws
    #nochroot
    chroot
    #data_dir=
    #symlinks
    #nosymlinks
    #vhost
    #novhost
    #globalpasswd
    #noglobalpasswd
    user=www-data
    cgipat=**.cgi.
    throttles=/etc/thttpd/throttle.conf
    throttles=/etc/thttpd/throttle.conf
    #host=
    logfile=/var/log/thttpd.log
    #pidfile=
    #charset=iso-8859-1
    #p3p=
    #max_age=
    

    Not quite sure about the dir, chroot and user paramter, as the link from @dmmcintyre3 says something else, but again that's CentOS.

    Thanks!!

  • sleddogsleddog Member
    edited January 2012

    Try only this in thttpd.conf

    # /etc/thttpd/thttpd.conf: thttpd configuration file
    port=8000
    dir=/home/freek/domains/freek.ws
    nochroot
    novhost
    user=www-data
    cgipat=**cgi
    logfile=/var/log/thttpd.log

    If you want to run thttpd on localhost only, so it isn't directly accessible from outside, add:

    host=127.0.0.1

    (And make sure you nginx site config points to 127.0.0.1).

    Other thttpd options (not included in this thttpd.conf) will use default values, which should be fine.

  • Try visiting your thttpd to isolate and repair the problem first.

  • @Freek said: For instance, I cannot find the /etc/nginx/php file

    that's something my install scripts add.

  • FreekFreek Member
    edited January 2012

    @sleddog I've did as you said but sadly I still get the same error 404 Not Found error.
    @yomero When I visit myip:8000 I get a 403-Forbidden. Should that be happening?

    Thanks guys, really appreciate it :)

  • Hmmm, that sound like 2 things

    • Your thttpd isn't binded to only 127.0.0.1 (you must fix that later).
    • And Forbidden sounds like permissions issues
Sign In or Register to comment.