聲明:css
一、本篇文章是我邊寫命令邊寫的,請尊重個人勞動成果,轉載請加上連接。html
二、我既然公開寫出來,是但願你們遇到問題的時候有個參考,因此,你們能夠免費轉載,使用該文章python
三、可是,若是你要用這篇文章來賺錢,sorry,你至少得分我點吧。nginx
使用gunicorn 來部署djangodjango
django寫代碼簡單,但部署是個難題,今天終於發現gunicorn 這個好東西,與你們一塊兒分享。ubuntu
環境:ubuntu 14.04 64bit + django 1.6.5 + virtualenv + gunicornvim
1、建立虛擬環境session
root@recall:/home/www# pwd /home/www root@recall:/home/www# virtualenv bugenv New python executable in bugenv/bin/python Installing setuptools, pip...done. root@recall:/home/www# ll total 16 drwxr-xr-x 4 root root 4096 Jul 21 15:04 ./ drwxr-xr-x 5 root root 4096 Jul 21 12:34 ../ drwxr-xr-x 6 root root 4096 Jul 21 15:05 bugenv/ ....................................(這一行是私人數據,這裏不影響,因此不顯示了)
2、進入虛擬環境app
root@recall:/home/www# cd bugenv/ root@recall:/home/www/bugenv# source bin/activate (bugenv)root@recall:/home/www/bugenv#
3、下載django和gunicorn
pip install django==1.6.5
pip install gunicornpython2.7
1 (bugenv)root@recall:/home/www/bugenv# pip install django==1.6.5 2 Downloading/unpacking django==1.6.5 3 Downloading Django-1.6.5-py2.py3-none-any.whl (6.7MB): 6.7MB downloaded 4 Installing collected packages: django 5 Successfully installed django 6 Cleaning up... 7 (bugenv)root@recall:/home/www/bugenv# pip install gunicorn 8 Downloading/unpacking gunicorn 9 Downloading gunicorn-19.0.0.tar.gz (382kB): 382kB downloaded 10 Running setup.py (path:/home/www/bugenv/build/gunicorn/setup.py) egg_info for package gunicorn 11 12 warning: no previously-included files matching '*.pyc' found under directory 'docs' 13 warning: no previously-included files matching '*.pyo' found under directory 'docs' 14 warning: no previously-included files matching '*.pyc' found under directory 'tests' 15 warning: no previously-included files matching '*.pyo' found under directory 'tests' 16 warning: no previously-included files matching '*.pyc' found under directory 'examples' 17 warning: no previously-included files matching '*.pyo' found under directory 'examples' 18 Installing collected packages: gunicorn 19 Running setup.py install for gunicorn 20 21 warning: no previously-included files matching '*.pyc' found under directory 'docs' 22 warning: no previously-included files matching '*.pyo' found under directory 'docs' 23 warning: no previously-included files matching '*.pyc' found under directory 'tests' 24 warning: no previously-included files matching '*.pyo' found under directory 'tests' 25 warning: no previously-included files matching '*.pyc' found under directory 'examples' 26 warning: no previously-included files matching '*.pyo' found under directory 'examples' 27 File "/home/www/bugenv/lib/python2.7/site-packages/gunicorn/workers/gaiohttp.py", line 67 28 yield from self.wsgi.close() 29 ^ 30 SyntaxError: invalid syntax 31 32 Installing gunicorn_paster script to /home/www/bugenv/bin 33 Installing gunicorn script to /home/www/bugenv/bin 34 Installing gunicorn_django script to /home/www/bugenv/bin 35 Successfully installed gunicorn 36 Cleaning up... 37 (bugenv)root@recall:/home/www/bugenv#
4、建立django項目和django app
django-admin.py startproject bugproject
python manages.py startapp bugapp
1 (bugenv)root@recall:/home/www/bugenv# ls 2 bin include lib local 3 (bugenv)root@recall:/home/www/bugenv# mkdir djcode 4 (bugenv)root@recall:/home/www/bugenv# cd djcode/ 5 (bugenv)root@recall:/home/www/bugenv/djcode# django-admin.py startproject bugproject 6 (bugenv)root@recall:/home/www/bugenv/djcode# cd bugproject/ 7 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# python manage.py startapp bugapp
5、設置django 的settings.py文件
添加app,最後結果以下:
1 INSTALLED_APPS = ( 2 'django.contrib.admin', 3 'django.contrib.auth', 4 'django.contrib.contenttypes', 5 'django.contrib.sessions', 6 'django.contrib.messages', 7 'django.contrib.staticfiles', 8 # add app 9 'bugapp', 10 'gunicorn', 11 )
6、運行gunicorn
1 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# pwd 2 /home/www/bugenv/djcode/bugproject 3 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# gunicorn bugproject.wsgi:application -b 127.0.0.1:1010
若是沒有問題,程序就掛起了。
若是你要操做其它東西,你最好使用nohup或screen
7、測試
7.1 我這裏用的是nohup運行的(也就是說,上面六的最後一行命令變成了下面這一行)
1 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# nohup gunicorn bugproject.wsgi:application -b 127.0.0.1:1010&
而後用tail查看nohup.out,沒有任何信息就對了
7.2 查看端口
1 (bugenv)root@recall:/home/www/bugenv/djcode/bugproject# netstat -lpnt 2 Active Internet connections (only servers) 3 Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name 4 --------------------------------------------------------------------------------------------- 5 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 772/nginx 6 tcp 0 0 127.0.0.1:1010 0.0.0.0:* LISTEN 9842/python 7 tcp 0 0 0.0.0.0:****** 0.0.0.0:* LISTEN 8387/python 8 tcp 0 0 0.0.0.0:***** 0.0.0.0:* LISTEN 689/python 9 tcp 0 0 0.0.0.0:**** 0.0.0.0:* LISTEN 1013/python
咱們使用的1010端口已經被佔用了
7.3 使用curl 進行測試
root@recall:~# curl 127.0.0.1:1010
結果以下:
1 <!DOCTYPE html> 2 <html lang="en"><head> 3 <meta http-equiv="content-type" content="text/html; charset=utf-8"> 4 <meta name="robots" content="NONE,NOARCHIVE"><title>Welcome to Django</title> 5 <style type="text/css"> 6 html * { padding:0; margin:0; } 7 body * { padding:10px 20px; } 8 body * * { padding:0; } 9 body { font:small sans-serif; } 10 body>div { border-bottom:1px solid #ddd; } 11 h1 { font-weight:normal; } 12 h2 { margin-bottom:.8em; } 13 h2 span { font-size:80%; color:#666; font-weight:normal; } 14 h3 { margin:1em 0 .5em 0; } 15 h4 { margin:0 0 .5em 0; font-weight: normal; } 16 table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; } 17 tbody td, tbody th { vertical-align:top; padding:2px 3px; } 18 thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; } 19 tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; } 20 #summary { background: #e0ebff; } 21 #summary h2 { font-weight: normal; color: #666; } 22 #explanation { background:#eee; } 23 #instructions { background:#f6f6f6; } 24 #summary table { border:none; background:transparent; } 25 </style> 26 </head> 27 28 <body> 29 <div id="summary"> 30 <h1>It worked!</h1> 31 <h2>Congratulations on your first Django-powered page.</h2> 32 </div> 33 34 <div id="instructions"> 35 <p> 36 Of course, you haven't actually done any work yet. 37 Next, start your first app by running <code>python manage.py startapp [appname]</code>. 38 </p> 39 </div> 40 41 <div id="explanation"> 42 <p> 43 You're seeing this message because you have <code>DEBUG = True</code> in your 44 Django settings file and you haven't configured any URLs. Get to work! 45 </p> 46 </div> 47 </body></html>
8、配置nginx
8.1 只有127.0.0.1:1010能訪問時不夠的,咱們還須要配置nginx
root@recall:~# vim /usr/local/nginx/nginx.conf
1 server{ 2 listen 80; 3 resolver 8.8.8.8; 4 server_name www.xxoo.com; 5 6 location / { 7 proxy_pass http://127.0.0.1:1010; 8 proxy_set_header Host $host; 9 proxy_set_header X-Real-IP $remote_addr; 10 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 11 } 12 }
至於靜態文件,本身百度、谷歌配置就行了。
8.2 測試配置文件
1 root@recall:~# /usr/local/nginx/nginx -t 2 nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok 3 nginx: configuration file /usr/local/nginx/nginx.conf test is successful
8.3 nginx 從新加載配置文件root@recall:~# /usr/local/nginx/nginx -s reload