Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwsgi + python + nginx + willy nilly file execution

I'm using uwsgi on Nginx to run some Python code.

I'd like to bind uwsgi to a directory and make it render any .py file that I call from the server in the browser. I'm thinking like PHP, here (/index.php executes that file, /login.php executes that file).

Is this a possibility? Or am I only able to explicitly specify a single module/app/file in uwsgi?

Here is my init syntax:

/opt/uwsgi/uwsgi -s 127.0.0.1:9001 -M 4 -t 30 -A 4 -p 4 -d /var/log/uwsgi.log --pidfile /var/run/uwsgi.pid --pythonpath /srv/www

I thought that would allow /srv/www to act as the folder where any .py files are executed.

Here is my nginx config:

server {
    listen       80;
    server_name  DONT_NEED_THIS;

    access_log  /srv/www/logs/access.log;
    error_log   /srv/www/logs/error.log;

    location / {
        root  /srv/www;

        # added lines    
        include        uwsgi_params;
        uwsgi_pass     127.0.0.1:9001;

    }

As it stands, when I try to call web root (ie www.site.com/) I get a:

wsgi application not found

With the following index.py file:

import sys
import os

sys.path.append(os.path.abspath(os.path.dirname(__file__)))

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Any ideas?

Thanks!

like image 913
eastydude5 Avatar asked Jun 06 '11 04:06

eastydude5


2 Answers

WSGI is not like PHP. You can't just point uwsgi to a directory with a bunch of .py files. In fact, never, ever make your python modules available in a public directory, accessible from the server. You need to hook uwsgi up to a WSGI application, preferably a framework. Read more about WSGI here. Check out bottle which is small, simple WSGI framework. It has great docs, and it's easy to get started with. There are actually tons of great web frameworks for Python though, so feel free to look around :)

like image 86
zeekay Avatar answered Sep 28 '22 08:09

zeekay


You may want to read this thread:

http://lists.unbit.it/pipermail/uwsgi/2011-March/001657.html

like image 21
roberto Avatar answered Sep 28 '22 08:09

roberto