Linux下 Nginx-uWSGI-Django 搭建

Posted 假如酷爱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下 Nginx-uWSGI-Django 搭建相关的知识,希望对你有一定的参考价值。

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server.

WSGI是为Python语言定义的通用网关接口,它承担python web框架(django、flask、web.py等)和web服务器(nginx、apache、lighttpd等)之间的中间层。


    浏览器                      chrome、firefox、ie等
      |
    web服务器                   nginx、apache等
      |
    网关接口                    CGI、FastCGI、WSGI等
      |
    Python(程序、Web框架)      Django、Flask、Tornado等

 

 

At the end, our complete stack of components will look like this:

the web client <-> the web server <-> the socket <-> uwsgi <-> Django

分步指南

 

  1. Django

    Install Django into your virtualenv, create a new project, and cd into the project:

        #$  pip install Django
        #$  django-admin.py startproject mysite
     #$ cd mysite
  2. uWSGI

        #$  pip install uwsgi

     

  3. 现在测试下   the web client <-> uWSGI <-> Python 栈是否能走通
     a.创建test.py文件并写入如下
       <code>
       # test.py
       def application(env, start_response):
           start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html)])
           return [b"Hello World"] # python3
           #return ["Hello World"] # python2

         </code>


      b.使用uWSGI直接调起python文件测试:

      #$ uwsgi --http :8000 --wsgi-file test.py

      c.访问  http://example.com:8000

     

  4. 测试Django工程是否能够运行,执行:
           #$   python manage.py runserver 0.0.0.0:8000

          如果能访问Django网页的话,执行:

     

           #$  uwsgi --http :8000 --module mysite.wsgi

     

          如果打开网页能看到Django页面说明 the web client <-> uWSGI <-> Django 配置成功

     

  5. 安装Nginx:
    #$  sudo apt-get install nginx
    #$  sudo /etc/init.d/nginx start    # start nginx

    访问  localhost:80 如果出现it work 说明Nginx安装成功

  6. 配置Nginx:

    You will need the uwsgi_params file, which is available in the nginx directory of the uWSGI distribution, or from https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

    Copy it into your project directory. In a moment we will tell nginx to refer to it.

    Now create a file called mysite_nginx.conf, and put this in it:

     # mysite_nginx.conf
    
    # the upstream component nginx needs to connect to
    upstream django {
        # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
        server 127.0.0.1:8001; # for a web port socket (we‘ll use this first)
    }
    
    # configuration of the server
    server {
        # the port your site will be served on
        listen      8000;
        # the domain name it will serve for
        server_name .example.com; # substitute your machine‘s IP address or FQDN
        charset     utf-8;
    
        # max upload size
        client_max_body_size 75M;   # adjust to taste
    
        # Django media
        location /media  {
            alias /path/to/your/mysite/media;  # your Django project‘s media files - amend as required
        }
    
        location /static {
            alias /path/to/your/mysite/static; # your Django project‘s static files - amend as required
        }
    
        # Finally, send all non-media requests to the Django server.
        location / {
            uwsgi_pass  django;
            include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
        }
    }

    This conf file tells nginx to serve up media and static files from the filesystem, as well as handle requests that require Django’s intervention. For a large deployment it is considered good practice to let one server handle static/media files, and another handle Django applications, but for now, this will do just fine.

    将配置好的Nginx配置文件软连接到Nginx配置文件夹下

     sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/
    
  7. Nginx测试

    Restart nginx:

     sudo /etc/init.d/nginx restart
    

    To check that media files are being served correctly, add an image called media.png to the /path/to/your/project/project/media directory, then visit http://example.com:8000/media/media.png - if this works, you’ll know at least that nginx is serving files correctly.

    It is worth not just restarting nginx, but actually stopping and then starting it again, which will inform you if there is a problem, and where it is.

  8. 将Nginx与uWSGI和Python文件关联起来

    Let’s get nginx to speak to the “hello world” test.py application.

     uwsgi --socket :8001 --wsgi-file test.py
    

    This is nearly the same as before, except this time one of the options is different:

    • socket :8001: use protocol uwsgi, port 8001

    nginx meanwhile has been configured to communicate with uWSGI on that port, and with the outside world on port 8000. Visit:

    http://example.com:8000/

    to check. And this is our stack:

     the web client <-> the web server <-> the socket <-> uWSGI <-> Python
    

    Meanwhile, you can try to have a look at the uswgi output at http://example.com:8001 - but quite probably, it won’t work because your browser speaks http, not uWSGI, though you should see output from uWSGI in your terminal.

  9. 将Nginx配置文件中的ports项使用Unix sockets代替

    Edit mysite_nginx.conf, changing it to match:

     server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    # server 127.0.0.1:8001; # for a web port socket (we‘ll use this first)

    and restart nginx.

    Run uWSGI again:

     uwsgi --socket mysite.sock --wsgi-file test.py
    

    This time the socket option tells uWSGI which file to use.

    Try localhost:8000 in the browser.

    如果访问不成功的话,做以下尝试:

    Check your nginx error log(/var/log/nginx/error.log). If you see something like:

     connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permission
    denied)

    then probably you need to manage the permissions on the socket so that nginx is allowed to use it.

    Try:

     uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (very permissive)

    or:

     uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (more sensible)

    You may also have to add your user to nginx’s group (which is probably www-data), or vice-versa, so that nginx can read and write to your socket properly.

    It’s worth keeping the output of the nginx log running in a terminal window so you can easily refer to it while troubleshooting.

     

  10. 使用uwsgi 和 nginx 运行 django app

    Let’s run our Django application:

     uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
    

    Now uWSGI and nginx should be serving up not just a “Hello World” module, but your Django project.

  11. 使用.ini文件启动uWSGI

    We can put the same options that we used with uWSGI into a file, and then ask uWSGI to run with that file. It makes it easier to manage configurations.

    Create a file called `mysite_uwsgi.ini`:

     # mysite_uwsgi.ini file
    [uwsgi]
    
    # Django-related settings
    # the base directory (full path)
    chdir           = /path/to/your/project
    # Django‘s wsgi file
    module          = project.wsgi
    # the virtualenv (full path)
    home            = /path/to/virtualenv
    
    # process-related settings
    # master
    master          = true
    # maximum number of worker processes
    processes       = 10
    # the socket (use the full path to be safe
    socket          = /path/to/your/project/mysite.sock
    # ... with appropriate permissions - may be needed
    # chmod-socket = 664
    # clear environment on exit
    vacuum          = true

    将服务器启动起来:

     uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

    再次测试,如果出现Django的欢迎界面,说明所有配置项均配置成功。

原文链接:https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html









以上是关于Linux下 Nginx-uWSGI-Django 搭建的主要内容,如果未能解决你的问题,请参考以下文章

linux下home目录下中文名怎么改成英文

linux下配置mysql 以及 linux下开启守护进程

linux下安装svn

arm-linux下如何安装GDB?pc-linux下如何升级GDB?

怎样在linux下进行java编程

linux 文字终端下输入汉字