librelist archives

« back to archive

Nginx+uWsgi+Flask cannot setup success

Nginx+uWsgi+Flask cannot setup success

From:
王建旭
Date:
2011-06-03 @ 16:18
Hi, guys:

I want use nginx, uWsgi, Flask on centos 5, but always has some error.

3 app are all newest version, the uwgsi_params file copy from uwsgi/nginx.

I use virtualenv to config python env.

project path: /path/to/project

virtualevn: /path/to/virenv/pyenv27

*First step:*

I use nginx and uWsgi, and browse show error page:

uWSGI Error
wsgi application not found

*Nginx config:*

location / {
      uwsgi_pass 127.0.0.1:5555;

      uwsgi_param UWSGI_PYHOME /path/to/virenv/pyenv27;

      uwsgi_param UWSGI_CHDIR /path/to/project;

      uwsgi_param UWSGI_SCRIPT uwsgi;
      uwsgi_param SCRIPT_NAME "";

      include uwsgi_params;

}

*Flask file is simple:*

import sys
sys.path.append(sys.path[0])

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():

    return "Hello World!"


#app.config.from_pyfile('product.cfg')

def application(environ, start_response):

    return app(environ, start_response)

if __name__ == '__main__':
    app.run()

*uWsgi command:*

/opt/uwsgi -s 127.0.0.1:5555 -M 4 -t 30 -A 4 -p 4 -d
/var/log/uwsgi.log --pidfile /var/run/uwsgi.pid --no-site --vhost
--limit-as 256

------------------------------

*2 Step*

I use uWsgi only.

cmd:

/opt/uwsgi/uwsgi --xml /path/to/project/uwsgi.xml

but I got error:

ImportError: No module named site

uWsgi xml:

<uwsgi>
<chdir>/path/to/project/</chdir>
<pythonpath>/path/to/project</pythonpath>
<virtualenv>/path/to/virenv/pyenv27</virtualenv>

<wsgi-file>/path/to/project/uwsgi.py</wsgi-file>


<callable>app</callable>

<socket>:6000</socket>
<chmod-socket>666</chmod-socket>

<master />
<processes>1</processes>
<uid>uwsgi</uid>
<gid>uwsgi</gid>

<disable-logging>false</disable-logging><!-- Errors are still logged;
this just disables request logging which Cherokee takes care of -->
<daemonize>/path/to/project/uwsgi.log</daemonize>
<vacuum />
<no-orphans />
</uwsgi>

Is there something wrong here?
http://stackoverflow.com/questions/6229649/nginxuwsgiflask-cannot-setup-success

Re: [flask] Nginx+uWsgi+Flask cannot setup success

From:
Roberto De Ioris
Date:
2011-06-03 @ 19:26
> Hi, guys:
>
> I want use nginx, uWsgi, Flask on centos 5, but always has some error.
>
> 3 app are all newest version, the uwgsi_params file copy from uwsgi/nginx.
>
> I use virtualenv to config python env.
>
> project path: /path/to/project
>
> virtualevn: /path/to/virenv/pyenv27
>
> *First step:*
>
> I use nginx and uWsgi, and browse show error page:
>
> uWSGI Error
> wsgi application not found
>
> *Nginx config:*
>
> location / {
>       uwsgi_pass 127.0.0.1:5555;
>
>       uwsgi_param UWSGI_PYHOME /path/to/virenv/pyenv27;
>
>       uwsgi_param UWSGI_CHDIR /path/to/project;
>
>       uwsgi_param UWSGI_SCRIPT uwsgi;
>       uwsgi_param SCRIPT_NAME "";
>
>       include uwsgi_params;
>
> }


You are using some of the most advanced configuration ways. I suggest you
to remain "simple".

Try first with a uWSGI-only setup

suppose your virtualenv is in /home/foo/venv and your app file is in
/home/foo/bar.py (do not call it uwsgi.py as it will clash with the
embedded uwsgi module)

./uwsgi --http :8080 --home /home/foo/venv --wsgi-file /home/foo/bar.py

Connect the browser to port :8080 and you should see your page.

Then remove the --http part and use a --socket directive

./uwsgi --socket 127.0.0.1:3030 --home /home/foo/venv --wsgi-file
/home/foo/bar.py

And in nginx add only a location directive

location /{
    include uwsgi_params;
    uwsgi_pass 127.0.0.1:3030;
}


After this setup you can start adding more features to the stack (as
resource monitoring or concurrency)


-- 
Roberto De Ioris
http://unbit.it

Re: [flask] Nginx+uWsgi+Flask cannot setup success

From:
王建旭
Date:
2011-06-04 @ 09:17
Yes, I test it step by step, and I found the problem.

I write a simple py to output sys.path:

> def application(environ, start_response):
>     start_response("200 OK", [("Content-type", "text/html")])
>     spath = '<br/>'.join(sys.path)
>     return ["Hello World : " + spath,]
>

nginx:
        include uwsgi_params;

        uwsgi_pass 127.0.0.1:8081;
        uwsgi_param UWSGI_PYHOME   /home/wjx/pyenv/env27;
        uwsgi_param UWSGI_CHDIR   $document_root;
        #uwsgi_param UWSGI_PYTHONPATH   $document_root;
        uwsgi_param UWSGI_MODULE   app_wsgi1;

uwsgi:

> /opt/uwsgi/uwsgi -s 127.0.0.1:8081 --vhost --no-site -M
>

browse output:

/usr/local/lib/python27.zip
/usr/local/lib/python2.7/
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload

my virtuanenv is not here,why???

On Sat, Jun 4, 2011 at 3:26 AM, Roberto De Ioris <roberto@unbit.it> wrote:

>
> > Hi, guys:
> >
> > I want use nginx, uWsgi, Flask on centos 5, but always has some error.
> >
> > 3 app are all newest version, the uwgsi_params file copy from
> uwsgi/nginx.
> >
> > I use virtualenv to config python env.
> >
> > project path: /path/to/project
> >
> > virtualevn: /path/to/virenv/pyenv27
> >
> > *First step:*
> >
> > I use nginx and uWsgi, and browse show error page:
> >
> > uWSGI Error
> > wsgi application not found
> >
> > *Nginx config:*
> >
> > location / {
> >       uwsgi_pass 127.0.0.1:5555;
> >
> >       uwsgi_param UWSGI_PYHOME /path/to/virenv/pyenv27;
> >
> >       uwsgi_param UWSGI_CHDIR /path/to/project;
> >
> >       uwsgi_param UWSGI_SCRIPT uwsgi;
> >       uwsgi_param SCRIPT_NAME "";
> >
> >       include uwsgi_params;
> >
> > }
>
>
> You are using some of the most advanced configuration ways. I suggest you
> to remain "simple".
>
> Try first with a uWSGI-only setup
>
> suppose your virtualenv is in /home/foo/venv and your app file is in
> /home/foo/bar.py (do not call it uwsgi.py as it will clash with the
> embedded uwsgi module)
>
> ./uwsgi --http :8080 --home /home/foo/venv --wsgi-file /home/foo/bar.py
>
> Connect the browser to port :8080 and you should see your page.
>
> Then remove the --http part and use a --socket directive
>
> ./uwsgi --socket 127.0.0.1:3030 --home /home/foo/venv --wsgi-file
> /home/foo/bar.py
>
> And in nginx add only a location directive
>
> location /{
>    include uwsgi_params;
>    uwsgi_pass 127.0.0.1:3030;
> }
>
>
> After this setup you can start adding more features to the stack (as
> resource monitoring or concurrency)
>
>
> --
> Roberto De Ioris
> http://unbit.it
>

Re: [flask] Nginx+uWsgi+Flask cannot setup success

From:
Roberto De Ioris
Date:
2011-06-05 @ 06:41
> Yes, I test it step by step, and I found the problem.
>
> I write a simple py to output sys.path:
>
>> def application(environ, start_response):
>>     start_response("200 OK", [("Content-type", "text/html")])
>>     spath = '<br/>'.join(sys.path)
>>     return ["Hello World : " + spath,]
>>


You may want to check this example:

http://projects.unbit.it/uwsgi/browser/vhosttest

(mainly nginx.conf as the usage of "set" is very elegant)

But i suggest you to upgrade to 0.9.8 (released some minutes ago) as its
virtualhosting mode has been improved a lot.
-- 
Roberto De Ioris
http://unbit.it

Re: [flask] Nginx+uWsgi+Flask cannot setup success

From:
Abdul Bijur Vallarkodath
Date:
2011-06-03 @ 17:03
On 03/06/11 18:18, 王建旭 wrote:
>
> Hi, guys:
>
> I want use nginx, uWsgi, Flask on centos 5, but always has some error.
>
> 3 app are all newest version, the uwgsi_params file copy from uwsgi/nginx.
>
> I use virtualenv to config python env.
>
> |project path:  /path/to/project
>
>
> virtualevn:  /path/to/virenv/pyenv27
>
>
> |
>
> /*First step:*/
>
> I use nginx and uWsgi, and browse show error page:
>
> |uWSGIError
> wsgi applicationnot  found
>
>
> |
>
> *Nginx config:*
>
> |location/  {
>        uwsgi_pass127.0.0.1:5555;
>
>
>        uwsgi_param UWSGI_PYHOME/path/to/virenv/pyenv27;
>
>
>        uwsgi_param UWSGI_CHDIR/path/to/project;
>
>
>        uwsgi_param UWSGI_SCRIPT uwsgi;
>        uwsgi_param SCRIPT_NAME"";
>
>        include uwsgi_params;
>
>
>
> }
> |
>
> *Flask file is simple:*
>
> |import  sys
> sys.path.append(sys.path[0])
>
>
>
> from  flaskimport  Flask
> app=  Flask(__name__)
>
>
>
> @app.route('/')
> def  hello_world():
>
>
>      return  "Hello World!"
>
>
> #app.config.from_pyfile('product.cfg')
>
>
>
> def  application(environ,  start_response):
>
>
>      return  app(environ,  start_response)
>
>
>
> if  __name__==  '__main__':
>      app.run()
>
>
> |
>
> *uWsgi command:*
>
> |/opt/uwsgi-s127.0.0.1:5555  -M4  -t30  -A4  -p4  
-d/var/log/uwsgi.log--pidfile/var/run/uwsgi.pid--no-site--vhost--limit-as
256
>
>
> |
> ------------------------------------------------------------------------
>
> /*2 Step*/
>
> I use uWsgi only.
>
> cmd:
>
> |/opt/uwsgi/uwsgi--xml/path/to/project/uwsgi.xml
>
>
> |
>
> but I got error:
>
> |ImportError:  No  module  named site
>
>
> |
>
> uWsgi xml:
>
> |<uwsgi>
> <chdir>/path/to/project/</chdir>
>
>
> <pythonpath>/path/to/project</pythonpath>
> <virtualenv>/path/to/virenv/pyenv27</virtualenv>
>
>
>
> <wsgi-file>/path/to/project/uwsgi.py</wsgi-file>
>
>
> <callable>app</callable>
>
>
>
> <socket>:6000</socket>
> <chmod-socket>666</chmod-socket>
>
>
>
> <master  />
> <processes>1</processes>
>
>
> <uid>uwsgi</uid>
> <gid>uwsgi</gid>
>
>
>
> <disable-logging>false</disable-logging><!-- Errors are still logged; 
this just disables request logging which Cherokee takes care of -->
>
>
> <daemonize>/path/to/project/uwsgi.log</daemonize>
> <vacuum  />
>
>
> <no-orphans  />
> </uwsgi>
> |
>
> Is there something wrong here?
>
> http://stackoverflow.com/questions/6229649/nginxuwsgiflask-cannot-setup-success
>
> 	
>
I had the same error with apache mod_wsgi, i gave the following in the 
.wsgi file :

#-----------------------
import sys

|sys.path.append('/path/to/application')|
|from yourapplication import app as application|
||#-----------------------

Make sure the wsgi user set for the wsgi process has access to the 
direction in the path

This worked for me. Good luck :)

Abdul


|


|