python+nginx+gunicorn 部署django小記
對于用django框架開發的項目的部署,本人是用nginx+gunicorn部署的,是一個前輩推薦的,部署了之后感覺確實簡單,在此記錄,以供交流學習 部署環境
-
Ubuntu 12.04 LTS
</li> -
django 1.6.2
</li> -
nginx 1.1.19
</li> -
guniron 19.3.0
</li> </ol>
安裝配置各項
-
安裝django:
pip install django==1.6.2
</li> -
裝nginx,此步適用ubuntu:
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;
}
}
</li> -
安裝gunicorn:
Gunicorn(gunicorn.org)是一個Python WSGI UNIX的HTTP服務器。,從Ruby的獨角獸(Unicorn)項目移植。它與很多種不同的web框架兼容,實現很簡單,輕量級,響應速度非常快。
pip insatll gunicorn
安裝好之后,可以創建django項目和django app
django-admin.py startproject projectname
</li> </ol>
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 reloadnginx restart
訪問ip:9600即可
來自:http://my.oschina.net/u/1241293/blog/383850
</span>
-