python+nginx+gunicorn 部署django小記

部署環境css

  1. Ubuntu 12.04 LTShtml

  2. django 1.6.2python

  3. nginx 1.1.19nginx

  4. guniron 19.3.0web


安裝配置各項django

  1. 安裝django:ubuntu

    pip install django==1.6.2服務器

  2. 裝nginx,此步適用ubuntu:app

     apt-get install nginx框架

    其它: 推薦源碼安裝

    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;

          }

    }

  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

        而後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便可

相關文章
相關標籤/搜索