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

时间: 2024-11-08 21:22:17

Linux下 Nginx-uWSGI-Django 搭建的相关文章

Centos7.6下Nginx+Uwsgi+Django部署

本人服务器是使用腾讯云Centos7.6.以下配置均在Root权限下操作. 1. python3 1.1 安装依赖包 sudo yum -y groupinstall "Development tools" sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-deve

如何将django部署从顶级目录迁移到子目录下(NGINX UWSGI DJANGO)

因为公司网站合并,要将我们的DJANGO项目从IP的顶级目录迁移到域名的二级目录. 以前硬编码的URL可惨了. 还涉及到upload目录,静态目录,websocket目录. 全用{% url %}问题不太大. nginx分前后两级,uwsgi配置要作相应更改,django的setting需要变量登陆网址. 这样,在正式网站访问二级目录,测试环境仍然可以根目录访问. nginx_front: server { listen 80; server_name localhost; location /

Nginx+uWSGI+Django+Python+ MySQL 搭建可靠的Python Web服务器

一.安装所需工具 yum -y install gcc gcc-c++ rpm-build mysql* libtool-ltdl* libtool automake autoconf libtool make setuptool 二.编译安装python2.7.5 1. 下载python2.7.5,保存到 /data/qtongmon/software http://www.python.org/ftp/python/ 2. 解压文件 tar xvf Python-2.7.5.tar.bz2

Ubuntu下nginx+uwsgi+flask的运行环境搭建

选择web framwork是个很艰难的事情, 主要分为轻量级和重量级框架. 由于没有搭建网站这种需要, 所以回避SSH, Django这种框架, 而选择一个轻量级框架. 自己也比较青睐python这门语言, 就选择了flask框架, nginx代理服务器享誉盛名, 所以拿来使用咯. 一. 开发环境搭建 采用离线安装方式, ubuntu开发环境(centos等环境类似) nginx 安装 $ wget http://nginx.org/download/nginx-1.6.0.tar.gz #仅

linux下nginx+php+mysql 自助环境搭建

++++++++++++++++++++++++++++++++++++++++++++++linux下nginx+php+mysql环境搭建++++++++++++++++++++++++++++++++++++++++++++++操作系统 : [CentOS6.0]服务器 : [nginx-1.1.8]PHP : [php-5.2.6]数据库 : [mysql-5.1.59]++++++++++++++++++++++++++++++++++++++++++++++准备安装软件(downlo

linux下nginx+php+mysql环境搭建

linux下nginx+php+mysql环境搭建 ++++++++++++++++++++++++++++++++++++++++++++++ 操作系统 : [CentOS6.0] 服务器 : [nginx-1.1.8] PHP : [php-5.2.6] 数据库 : [mysql-5.1.59] ++++++++++++++++++++++++++++++++++++++++++++++ 准备安装软件(download)  1>[nginx-1.1.8]       http://nginx

使用Nginx+uWSGI+Django方法部署Django程序(下)

在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的,在实际的部署环境中,Nginx是必不可少的工具. 在本篇文章中,我将一直延用“N步法”的风格来阐述如何将uWSGI与Nginx做连接来部署Django程序.并在最后,会较为完整的阐述本社区的部署方法. 本文大纲: 环境介绍 配置uWSGI 配置Nginx Nginx+uWSGI+Django的实现方式 一些建

【转】五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下)

五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(下) By Django中国社区 at 2013-05-18 04:38 在上一篇文章<五步教你实现使用Nginx+uWSGI+Django方法部署Django程序(上)>中,阐述了如何只使用uWSGI来部署Django程序. 当然,单单只有uWSGI是不够的,在实际的部署环境中,Nginx是必不可少的工具. 在本篇文章中,我将一直延用“N步法”的风格来阐述如何将uWSGI与Nginx做连接来部署Django程序.并

debian完整部署 Nginx + uWSGI + Django

debian完整部署 Nginx + uWSGI + Django 手工部署一个Django服务器真心不容易,需要安装很多东西.从头开始搭建服务器,主要是为了梳理一下后续开发中一般为碰到的平台部署.对后续问题的解决有一定帮助. 通常部署有2中方式: 一种是使用现成提供的服务器包用apt-get这种方式安装的.这种方式比较简单,但没有新版本. 另外就是使用源代码自己编译安装,这种比较繁琐,但能选择适合的版本安装. 这里介绍的是第二种,使用源代码编译的版本进行安装部署. 部署测试环境: - wind

Nginx+uWSGI+Django部署web服务器

目录 Nginx+uWSGI+Django部署web服务器 环境说明 前言 搭建项目 Django部署 编辑luffy/luffy/settings.py 编辑luffy/app01/views.py 编辑luffy/luffy/urls.py 运行并测试 uWSGI部署 测试运行uWSGI 使用uWSGI运行django项目 uWSGi热加载Djangoa项目 部署nginx nginx配置uwsgi和django django部署static文件 重新加载nginx进行测试 测试nginx