python+nginx+gunicorn 部署django小记

jopen 10年前

对于用django框架开发的项目的部署,本人是用nginx+gunicorn部署的,是一个前辈推荐的,部署了之后感觉确实简单,在此记录,以供交流学习

部署环境

  1. Ubuntu 12.04 LTS

  2. django 1.6.2

  3. nginx 1.1.19

  4. guniron 19.3.0


安装配置各项

  1. 安装django:

    pip install django==1.6.2

  2. 装nginx,此步适用ubuntu:

     apt-get install nginx </p>

    其它: 推荐源码安装

    nginx命令:

    启动:  /etc/init.d/nginx start

    停止: /etc/init.d/nginx stop

    测试配置文件是否正确: nginx -t

    配置proxy功能:

    1.1 在/etc/nginx/site-available/目录下创建  test文件

    server {

            listen 9600;监听端口号,此处的端口号是服务器上可用的端口号,不然会失败

           server_name localhost;

            server_name 127.0.0.1;此处应该改为公网 IP地址

            access_log /opt/logs/nginx/wasp_ticket_stat.log;

            error_log /opt/logs/nginx/wasp_ticket_stat_error.log;

            location / {

                proxy_pass http://127.0.0.1:9090;此处填写转发到的gunicorn绑定的服务器端口

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP $http_x_real_ip;

                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          }

    }

    </li>
  3. 安装gunicorn:

    Gunicorn(gunicorn.org)是一个Python WSGI UNIX的HTTP服务器。,从Ruby的独角兽(Unicorn)项目移植。它与很多种不同的web框架兼容,实现很简单,轻量级,响应速度非常快。

    pip insatll gunicorn

    安装好之后,可以创建django项目和django app

    django-admin.py startproject projectname
    python manages.py startapp appname

  4. </ol>

            然后cd appname,gunicorn appname.wsgi:application --bind 0.0.0.0:9090,绑定在本机上的9090端口

        此时可以用netstat -lpnt查看端口占用情况

        然后用curl 127.0.0.1:9090去测试,如果出现以下情况,说明成功了:

    <!DOCTYPE html>

    <html><head>

      <meta http-equiv="content-type" content="text/html; charset=utf-8">

      <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title>

      <style type="text/css">

        html * { padding:0; margin:0; }

        body * { padding:10px 20px; }

        body * * { padding:0; }

        body { font:small sans-serif; }

        body>div { border-bottom:1px solid #ddd; }

        h1 { font-weight:normal; }

        h2 { margin-bottom:.8em; }

        h2 span { font-size:80%; color:#666; font-weight:normal; }

        h3 { margin:1em 0 .5em 0; }

        h4 { margin:0 0 .5em 0; font-weight: normal; }

        table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }

        tbody td, tbody th { vertical-align:top; padding:2px 3px; }

        thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }

        tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }

        #summary { background: #e0ebff; }

        #summary h2 { font-weight: normal; color: #666; }

        #explanation { background:#eee; }

        #instructions { background:#f6f6f6; }

        #summary table { border:none; background:transparent; }

      </style>

    </head>


    <body>

    <div id="summary">

      <h1>It worked!</h1>

      <h2>Congratulations on your first Django-powered page.</h2>

    </div>


    <div id="instructions">

      <p>

        Of course, you haven't actually done any work yet.

        Next, start your first app by running <code>python manage.py startapp [appname]</code>.

      </p>

    </div>


    <div id="explanation">

      <p>

        You're seeing this message because you have <code>DEBUG = True</code> in your

        Django settings file and you haven't configured any URLs. Get to work!

      </p>

    </div>

    </body></html>


    重启nginx:nginx -s reload

    nginx restart

    访问ip:9600即可

       
    来自:http://my.oschina.net/u/1241293/blog/383850

    </span>