使用雲服務器部署Django項目
一、阿里雲
第一步確定是去註冊並買一個IP地址雲服務器。php
https://promotion.aliyun.com/ntms/act/qwbk.html?spm=5176.8112568.738194.1.1f619ed5yjHLrJ&userCode=asvrgb2icss
操做系統html
Ubuntu Server 16.04.1 LTS 64位
獲取root權限前端
ubuntu@VM-0-9-ubuntu:~$ sudo passwd root Enter new UNIX password: root Retype new UNIX password: passwd: password updated successfully ubuntu@VM-0-9-ubuntu:~$ ubuntu@VM-0-9-ubuntu:~$ su - Password: root@VM-0-9-ubuntu:~#
二、Mysql
一、安裝mysqly ,須要y確認
apt-get install mysql-server
設置MySQL密碼 ,輸入兩次密碼,回車便可python
查看MySQL版本命令mysql
root@VM-0-9-ubuntu:~# mysql --version mysql Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using EditLine wrapper
運行數據庫Mysql安全配置嚮導: 輸入root密碼,4個回車 okjquery
mysql_secure_installation
啓動mysql服務nginx
root@VM-0-9-ubuntu:~# service mysql start
配置字符集,web
root@VM-0-9-ubuntu:~# vim /etc/mysql/my.cnf # 添加以下代碼,保存退出
[client] port = 3306 socket = /var/lib/mysql/mysql.sock default-character-set=utf8 [mysqld] port = 3306 socket = /var/lib/mysql/mysql.sock character-set-server=utf8 [mysql] no-auto-rehash default-character-set=utf8
重啓mysql服務ajax
root@VM-0-9-ubuntu:~# service mysql restart
二、測試數據庫
建立項目的數據庫 # 個人數據庫
root@VM-0-16-ubuntu:~# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | cnblog | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
三、python3 +django2.0
一、Ubuntu自帶python3.5版本
二、安裝pip3
root@VM-0-9-ubuntu:~# apt-get install python3-pip
跟新pip的話
將當前使用的pip3文件按其所寫以下修改,位置是 /usr/bin/pip3,此操做須要管理員權限。
測試pip3
root@VM-0-9-ubuntu:~# pip3
三、安裝django2.0
root@VM-0-9-ubuntu:~# pip3 install django==2.0
測試
四、 uWSGI,uwsgi,WSGI
一、uWSGI介紹
uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的做用是與uWSGI服務器進行交換。
要注意 WSGI / uwsgi / uWSGI 這三個概念的區分。
- WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通訊的一種規範。
- uwsgi是一種線路協議而不是通訊協議,在此經常使用於在uWSGI服務器與其餘網絡服務器的數據通訊。
- 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
- uwsgi協議是一個uWSGI服務器自有的協議,它用於定義傳輸信息的類型(type of information),每個uwsgi packet前4byte爲傳輸信息類型描述,它與WSGI相比是兩樣東西
uWSGI的主要特色以下
- 超快的性能
- 低內存佔用(實測爲apache2的mod_wsgi的一半左右)
- 多app管理(終於不用左思右想下個app用哪一個端口比較好了-.-)
- 詳盡的日誌功能(能夠用來分析app性能和瓶頸)
- 高度可定製(內存大小限制,服務必定次數後重啓等)
總而言之uwgi是個部署用的好東東,正如uWSGI做者所吹噓的:
If you are searching for a simple wsgi-only server, uWSGI is not for you, but if you are building a real (production-ready) app that need to be rock-solid, fast and easy to distribute/optimize for various load-average, you will pathetically and morbidly fall in love (we hope) with uWSGI.
二、uwsgi模塊安裝
root@VM-0-9-ubuntu:~# pip3 install uwsgi Collecting uwsgi
測試:新建test.py 輸入如下內容
root@VM-0-9-ubuntu:~# vim test.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
uwsgi啓動8000端口,瀏覽器訪問你的ip:8000
root@VM-0-9-ubuntu:~# uwsgi --http :8000 --wsgi-file test.py
五、上傳本地django項目,測試
一、用rz上傳zip打包的django項目
home下的ubuntu目錄下
root@VM-0-9-ubuntu:/home/ubuntu# pwd /home/ubuntu
進行unzip的解壓。
安裝tree命令,查看目錄結構
root@VM-0-9-ubuntu:/home/ubuntu# apt install tree
root@VM-0-9-ubuntu:/home/ubuntu# tree
樹狀結構:
![](http://static.javashuo.com/static/loading.gif)
root@VM-0-16-ubuntu:/home/ubuntu# tree . ├── cnblog │ ├── bbs │ │ ├── __init__.py │ │ ├── __pycache__ │ │ │ ├── __init__.cpython-35.pyc │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── settings.cpython-35.pyc │ │ │ ├── settings.cpython-36.pyc │ │ │ ├── urls.cpython-35.pyc │ │ │ ├── urls.cpython-36.pyc │ │ │ ├── wsgi.cpython-35.pyc │ │ │ └── wsgi.cpython-36.pyc │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── bbs.ini │ ├── blog │ │ ├── admin.py │ │ ├── apps.py │ │ ├── forms.py │ │ ├── __init__.py │ │ ├── migrations │ │ │ ├── 0001_initial.py │ │ │ ├── 0002_auto_20180524_1227.py │ │ │ ├── 0003_auto_20180903_2151.py │ │ │ ├── 0004_auto_20180930_1412.py │ │ │ ├── __init__.py │ │ │ └── __pycache__ │ │ │ ├── 0001_initial.cpython-35.pyc │ │ │ ├── 0001_initial.cpython-36.pyc │ │ │ ├── 0002_auto_20180524_1227.cpython-35.pyc │ │ │ ├── 0002_auto_20180524_1227.cpython-36.pyc │ │ │ ├── 0003_auto_20180903_2151.cpython-35.pyc │ │ │ ├── 0003_auto_20180903_2151.cpython-36.pyc │ │ │ ├── 0004_auto_20180930_1412.cpython-35.pyc │ │ │ ├── 0004_auto_20180930_1412.cpython-36.pyc │ │ │ ├── __init__.cpython-35.pyc │ │ │ └── __init__.cpython-36.pyc │ │ ├── models.py │ │ ├── __pycache__ │ │ │ ├── admin.cpython-35.pyc │ │ │ ├── admin.cpython-36.pyc │ │ │ ├── apps.cpython-35.pyc │ │ │ ├── apps.cpython-36.pyc │ │ │ ├── forms.cpython-35.pyc │ │ │ ├── forms.cpython-36.pyc │ │ │ ├── __init__.cpython-35.pyc │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── models.cpython-35.pyc │ │ │ ├── models.cpython-36.pyc │ │ │ ├── urls.cpython-35.pyc │ │ │ ├── urls.cpython-36.pyc │ │ │ ├── views.cpython-35.pyc │ │ │ └── views.cpython-36.pyc │ │ ├── templatetags │ │ │ ├── __init__.py │ │ │ ├── my_tags.py │ │ │ └── __pycache__ │ │ │ ├── __init__.cpython-35.pyc │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── my_tags.cpython-35.pyc │ │ │ └── my_tags.cpython-36.pyc │ │ ├── tests.py │ │ ├── urls.py │ │ └── views.py │ ├── cnblog.ini │ ├── manage.py │ ├── media │ │ ├── add_article_img │ │ │ ├── 1.jpg │ │ │ ├── 5.jpg │ │ │ ├── 6.jpg │ │ │ └── icon_form.gif │ │ └── avatars │ │ ├── 1.jpg │ │ ├── 6.jpg │ │ ├── default.jpg │ │ ├── default.png │ │ └── hmbb.png │ ├── static │ │ ├── bootstrap │ │ │ ├── css │ │ │ │ └── bootstrap.min.css │ │ │ ├── fonts │ │ │ │ ├── glyphicons-halflings-regular.eot │ │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ │ └── glyphicons-halflings-regular.woff2 │ │ │ └── js │ │ │ └── bootstrap.min.js │ │ ├── font │ │ │ └── kumo.ttf │ │ ├── fontawesome │ │ │ ├── css │ │ │ │ ├── font-awesome.css │ │ │ │ └── font-awesome.min.css │ │ │ └── fonts │ │ │ ├── FontAwesome.otf │ │ │ ├── fontawesome-webfont.eot │ │ │ ├── fontawesome-webfont.svg │ │ │ ├── fontawesome-webfont.ttf │ │ │ ├── fontawesome-webfont.woff │ │ │ └── fontawesome-webfont.woff2 │ │ ├── img │ │ │ ├── ad_1.jpg │ │ │ ├── ChMkJ1oJW4WIFWWcAAs8tHMGUwUAAiH8gG5hqkACzzM660.jpg │ │ │ ├── cr.png │ │ │ ├── default.png │ │ │ ├── downdown.gif │ │ │ ├── f556c8938b3b4338ace292154f7c059d.jpg │ │ │ ├── hmbb.png │ │ │ ├── icon_form.gif │ │ │ ├── tt.png │ │ │ ├── upup.gif │ │ │ └── valid_code.png │ │ ├── jquery-3.3.1.js │ │ ├── kindeditor │ │ │ ├── asp │ │ │ │ ├── demo.asp │ │ │ │ ├── file_manager_json.asp │ │ │ │ ├── JSON_2.0.4.asp │ │ │ │ ├── UpLoad_Class.asp │ │ │ │ └── upload_json.asp │ │ │ ├── asp.net │ │ │ │ ├── bin │ │ │ │ │ └── LitJSON.dll │ │ │ │ ├── demo.aspx │ │ │ │ ├── file_manager_json.ashx │ │ │ │ ├── README.txt │ │ │ │ └── upload_json.ashx │ │ │ ├── jsp │ │ │ │ ├── demo.jsp │ │ │ │ ├── file_manager_json.jsp │ │ │ │ ├── lib │ │ │ │ │ ├── commons-fileupload-1.2.1.jar │ │ │ │ │ ├── commons-io-1.4.jar │ │ │ │ │ └── json_simple-1.1.jar │ │ │ │ ├── README.txt │ │ │ │ └── upload_json.jsp │ │ │ ├── kindeditor-all.js │ │ │ ├── kindeditor-all-min.js │ │ │ ├── lang │ │ │ │ ├── ar.js │ │ │ │ ├── en.js │ │ │ │ ├── ko.js │ │ │ │ ├── ru.js │ │ │ │ ├── zh-CN.js │ │ │ │ └── zh-TW.js │ │ │ ├── license.txt │ │ │ ├── php │ │ │ │ ├── demo.php │ │ │ │ ├── file_manager_json.php │ │ │ │ ├── JSON.php │ │ │ │ └── upload_json.php │ │ │ ├── plugins │ │ │ │ ├── anchor │ │ │ │ │ └── anchor.js │ │ │ │ ├── autoheight │ │ │ │ │ └── autoheight.js │ │ │ │ ├── baidumap │ │ │ │ │ ├── baidumap.js │ │ │ │ │ ├── index.html │ │ │ │ │ └── map.html │ │ │ │ ├── clearhtml │ │ │ │ │ └── clearhtml.js │ │ │ │ ├── code │ │ │ │ │ ├── code.js │ │ │ │ │ ├── prettify.css │ │ │ │ │ └── prettify.js │ │ │ │ ├── emoticons │ │ │ │ │ ├── emoticons.js │ │ │ │ │ └── images │ │ │ │ │ ├── 0.gif │ │ │ │ │ ├── 100.gif │ │ │ │ │ ├── 101.gif │ │ │ │ │ ├── 102.gif │ │ │ │ │ ├── 103.gif │ │ │ │ │ ├── 104.gif │ │ │ │ │ ├── 105.gif │ │ │ │ │ ├── 106.gif │ │ │ │ │ ├── 107.gif │ │ │ │ │ ├── 108.gif │ │ │ │ │ ├── 109.gif │ │ │ │ │ ├── 10.gif │ │ │ │ │ ├── 110.gif │ │ │ │ │ ├── 111.gif │ │ │ │ │ ├── 112.gif │ │ │ │ │ ├── 113.gif │ │ │ │ │ ├── 114.gif │ │ │ │ │ ├── 115.gif │ │ │ │ │ ├── 116.gif │ │ │ │ │ ├── 117.gif │ │ │ │ │ ├── 118.gif │ │ │ │ │ ├── 119.gif │ │ │ │ │ ├── 11.gif │ │ │ │ │ ├── 120.gif │ │ │ │ │ ├── 121.gif │ │ │ │ │ ├── 122.gif │ │ │ │ │ ├── 123.gif │ │ │ │ │ ├── 124.gif │ │ │ │ │ ├── 125.gif │ │ │ │ │ ├── 126.gif │ │ │ │ │ ├── 127.gif │ │ │ │ │ ├── 128.gif │ │ │ │ │ ├── 129.gif │ │ │ │ │ ├── 12.gif │ │ │ │ │ ├── 130.gif │ │ │ │ │ ├── 131.gif │ │ │ │ │ ├── 132.gif │ │ │ │ │ ├── 133.gif │ │ │ │ │ ├── 134.gif │ │ │ │ │ ├── 13.gif │ │ │ │ │ ├── 14.gif │ │ │ │ │ ├── 15.gif │ │ │ │ │ ├── 16.gif │ │ │ │ │ ├── 17.gif │ │ │ │ │ ├── 18.gif │ │ │ │ │ ├── 19.gif │ │ │ │ │ ├── 1.gif │ │ │ │ │ ├── 20.gif │ │ │ │ │ ├── 21.gif │ │ │ │ │ ├── 22.gif │ │ │ │ │ ├── 23.gif │ │ │ │ │ ├── 24.gif │ │ │ │ │ ├── 25.gif │ │ │ │ │ ├── 26.gif │ │ │ │ │ ├── 27.gif │ │ │ │ │ ├── 28.gif │ │ │ │ │ ├── 29.gif │ │ │ │ │ ├── 2.gif │ │ │ │ │ ├── 30.gif │ │ │ │ │ ├── 31.gif │ │ │ │ │ ├── 32.gif │ │ │ │ │ ├── 33.gif │ │ │ │ │ ├── 34.gif │ │ │ │ │ ├── 35.gif │ │ │ │ │ ├── 36.gif │ │ │ │ │ ├── 37.gif │ │ │ │ │ ├── 38.gif │ │ │ │ │ ├── 39.gif │ │ │ │ │ ├── 3.gif │ │ │ │ │ ├── 40.gif │ │ │ │ │ ├── 41.gif │ │ │ │ │ ├── 42.gif │ │ │ │ │ ├── 43.gif │ │ │ │ │ ├── 44.gif │ │ │ │ │ ├── 45.gif │ │ │ │ │ ├── 46.gif │ │ │ │ │ ├── 47.gif │ │ │ │ │ ├── 48.gif │ │ │ │ │ ├── 49.gif │ │ │ │ │ ├── 4.gif │ │ │ │ │ ├── 50.gif │ │ │ │ │ ├── 51.gif │ │ │ │ │ ├── 52.gif │ │ │ │ │ ├── 53.gif │ │ │ │ │ ├── 54.gif │ │ │ │ │ ├── 55.gif │ │ │ │ │ ├── 56.gif │ │ │ │ │ ├── 57.gif │ │ │ │ │ ├── 58.gif │ │ │ │ │ ├── 59.gif │ │ │ │ │ ├── 5.gif │ │ │ │ │ ├── 60.gif │ │ │ │ │ ├── 61.gif │ │ │ │ │ ├── 62.gif │ │ │ │ │ ├── 63.gif │ │ │ │ │ ├── 64.gif │ │ │ │ │ ├── 65.gif │ │ │ │ │ ├── 66.gif │ │ │ │ │ ├── 67.gif │ │ │ │ │ ├── 68.gif │ │ │ │ │ ├── 69.gif │ │ │ │ │ ├── 6.gif │ │ │ │ │ ├── 70.gif │ │ │ │ │ ├── 71.gif │ │ │ │ │ ├── 72.gif │ │ │ │ │ ├── 73.gif │ │ │ │ │ ├── 74.gif │ │ │ │ │ ├── 75.gif │ │ │ │ │ ├── 76.gif │ │ │ │ │ ├── 77.gif │ │ │ │ │ ├── 78.gif │ │ │ │ │ ├── 79.gif │ │ │ │ │ ├── 7.gif │ │ │ │ │ ├── 80.gif │ │ │ │ │ ├── 81.gif │ │ │ │ │ ├── 82.gif │ │ │ │ │ ├── 83.gif │ │ │ │ │ ├── 84.gif │ │ │ │ │ ├── 85.gif │ │ │ │ │ ├── 86.gif │ │ │ │ │ ├── 87.gif │ │ │ │ │ ├── 88.gif │ │ │ │ │ ├── 89.gif │ │ │ │ │ ├── 8.gif │ │ │ │ │ ├── 90.gif │ │ │ │ │ ├── 91.gif │ │ │ │ │ ├── 92.gif │ │ │ │ │ ├── 93.gif │ │ │ │ │ ├── 94.gif │ │ │ │ │ ├── 95.gif │ │ │ │ │ ├── 96.gif │ │ │ │ │ ├── 97.gif │ │ │ │ │ ├── 98.gif │ │ │ │ │ ├── 99.gif │ │ │ │ │ ├── 9.gif │ │ │ │ │ └── static.gif │ │ │ │ ├── filemanager │ │ │ │ │ ├── filemanager.js │ │ │ │ │ └── images │ │ │ │ │ ├── file-16.gif │ │ │ │ │ ├── file-64.gif │ │ │ │ │ ├── folder-16.gif │ │ │ │ │ ├── folder-64.gif │ │ │ │ │ └── go-up.gif │ │ │ │ ├── fixtoolbar │ │ │ │ │ └── fixtoolbar.js │ │ │ │ ├── flash │ │ │ │ │ └── flash.js │ │ │ │ ├── image │ │ │ │ │ ├── image.js │ │ │ │ │ └── images │ │ │ │ │ ├── align_left.gif │ │ │ │ │ ├── align_right.gif │ │ │ │ │ ├── align_top.gif │ │ │ │ │ └── refresh.png │ │ │ │ ├── insertfile │ │ │ │ │ └── insertfile.js │ │ │ │ ├── lineheight │ │ │ │ │ └── lineheight.js │ │ │ │ ├── link │ │ │ │ │ └── link.js │ │ │ │ ├── map │ │ │ │ │ ├── map.html │ │ │ │ │ └── map.js │ │ │ │ ├── media │ │ │ │ │ └── media.js │ │ │ │ ├── multiimage │ │ │ │ │ ├── images │ │ │ │ │ │ ├── image.png │ │ │ │ │ │ ├── select-files-en.png │ │ │ │ │ │ ├── select-files-zh-CN.png │ │ │ │ │ │ └── swfupload.swf │ │ │ │ │ └── multiimage.js │ │ │ │ ├── pagebreak │ │ │ │ │ └── pagebreak.js │ │ │ │ ├── plainpaste │ │ │ │ │ └── plainpaste.js │ │ │ │ ├── preview │ │ │ │ │ └── preview.js │ │ │ │ ├── quickformat │ │ │ │ │ └── quickformat.js │ │ │ │ ├── table │ │ │ │ │ └── table.js │ │ │ │ ├── template │ │ │ │ │ ├── html │ │ │ │ │ │ ├── 1.html │ │ │ │ │ │ ├── 2.html │ │ │ │ │ │ └── 3.html │ │ │ │ │ └── template.js │ │ │ │ └── wordpaste │ │ │ │ └── wordpaste.js │ │ │ └── themes │ │ │ ├── common │ │ │ │ ├── anchor.gif │ │ │ │ ├── blank.gif │ │ │ │ ├── flash.gif │ │ │ │ ├── loading.gif │ │ │ │ ├── media.gif │ │ │ │ └── rm.gif │ │ │ ├── default │ │ │ │ ├── background.png │ │ │ │ ├── default.css │ │ │ │ └── default.png │ │ │ ├── qq │ │ │ │ ├── editor.gif │ │ │ │ └── qq.css │ │ │ └── simple │ │ │ └── simple.css │ │ ├── mystyle.css │ │ ├── setupajax.js │ │ └── theme │ │ ├── cyy.css │ │ └── jesi.css │ ├── templates │ │ ├── add_article.html │ │ ├── article_detail.html │ │ ├── base.html │ │ ├── home.html │ │ ├── index.html │ │ ├── left_menu.html │ │ ├── login.html │ │ ├── login(舊版).html │ │ └── register.html │ ├── util │ │ ├── __init__.py │ │ ├── page.py │ │ └── __pycache__ │ │ ├── __init__.cpython-35.pyc │ │ ├── __init__.cpython-36.pyc │ │ ├── page.cpython-35.pyc │ │ └── page.cpython-36.pyc │ ├── uwsgi.ini │ ├── uwsgi.log │ ├── uwsgi_params │ └── uwsg.log ├── cnblog.tar ├── cnblog.zip └── test.py
二、配置django項目
安裝依賴包
# 鏈接mysql的模塊 root@VM-0-9-ubuntu:/home/ubuntu# pip3 install PyMySQL==0.8.1 ----下面兩個是我項目須要的模塊--- # 極驗科技的模塊 root@VM-0-9-ubuntu:/home/ubuntu# pip3 install geetest # 防止xss攻擊的 root@VM-0-9-ubuntu:/home/ubuntu# pip3 install bs4==0.0.1
到django項目下,與manage.py同目錄數據表遷移
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# python3 manage.py makemigrations
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# python3 manage.py migrate
三、啓動django項目
到django項目下,與manage.py同目錄,啓動項目
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# python3 manage.py runserver 0.0.0.0:8000
對了這裏須要在django的settings.py中設置
咱們在setting.py裏設置一下
DEBUG = False ALLOWED_HOSTS = ['*']
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# python3 manage.py runserver 0.0.0.0:8000
這樣雖然能夠運行了,可是靜態文件沒有加載出來。
四、uwsgi命令測試啓動
ubuntu@VM-0-9-ubuntu:~/cnblog$ pwd /home/ubuntu/cnblog
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# uwsgi --http 0.0.0.0:8000 --file bbs/wsgi.py --static-map=/static=static
這句話的意思是啓動uwsgi,而且使用bbs目錄下的wsgi文件,靜態資源使用static文件夾內的。
五、至此項目確認沒有問題。
五、nginx
一、什麼是 Nginx?
Nginx (engine x) 是一款輕量級的 Web 服務器 、反向代理服務器及電子郵件(IMAP/POP3)代理服務器。
http://www.cnblogs.com/venicid/p/8440576.html
二、什麼是反向代理
反向代理(Reverse Proxy)方式是指以代理服務器來接受 internet 上的鏈接請求,而後將請求轉發給內部網絡上的服務器,
並將從服務器上獲得的結果返回給 internet 上請求鏈接的客戶端,此時代理服務器對外就表現爲一個反向代理服務器。
3.Nginx的特色
(1)跨平臺:Nginx 能夠在大多數 Unix like OS編譯運行,並且也有Windows的移植版本。(2)配置異常簡單,很是容易上手。配置風格跟程序開發同樣,神通常的配置
(3)非阻塞、高併發鏈接:數據複製時,磁盤I/O的第一階段是非阻塞的。官方測試可以支撐5萬併發鏈接,在實際生產環境中跑到2~3萬併發鏈接數.(這得益於Nginx使用了最新的epoll模型)
(4)事件驅動:通訊機制採用epoll模型,支持更大的併發鏈接。
(5)master/worker結構:一個master進程,生成一個或多個worker進程
(6)內存消耗小:處理大併發的請求內存消耗很是小。在3萬併發鏈接下,開啓的10個Nginx 進程才消耗150M內存(15M*10=150M)
(7)成本低廉:Nginx爲開源軟件,能夠無償使用。而購買F5 BIG-IP、NetScaler等硬件負載均衡交換機則須要十多萬至幾十萬人民幣
(8)內置的健康檢查功能:若是 Nginx Proxy 後端的某臺 Web 服務器宕機了,不會影響前端訪問。
(9)節省帶寬:支持 GZIP 壓縮,能夠添加瀏覽器本地緩存的 Header 頭。
(10)穩定性高:用於反向代理,宕機的機率微乎其微
四、安裝
apt-get install nginx
五、測試
url中輸入你的IP地址
六、nginx命令以及配置文件
http://www.javashuo.com/article/p-usyhmvsx-cr.html
六、nginx與uwsgi通訊
經過 uWSGI 啓動 django 的web服務,nginx 將請求中轉給 uWSGI 而後返回。
一、配置uwsgi
1)複製uwsgi_params文件 ,與manage.py 同目錄中
通常能夠直接使用nginx路徑下的uwsgi_params,路徑通常在 /etc/nginx/uwsgi_params
root@VM-0-9-ubuntu:~# ls -l /etc/nginx/uwsgi_params
-rw-r--r-- 1 root root 664 Aug 9 15:29 /etc/nginx/uwsgi_params
![](http://static.javashuo.com/static/loading.gif)
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
2)新建 bbs.ini文件,與manage.py 同目錄中
[uwsgi] # uwsgi監聽的socket,一下子配置Nginx會用到 socket = 127.0.0.1:8001 # 在app加載前切換到該目錄,設置爲Django項目根目錄 chdir = /home/ubuntu/cnblog # 加載指定的python WSGI模塊,設置爲Django項目的wsgi文件 module = bbs.wsgi # 啓動一個master進程來管理其餘進程 master = true # 工做的進程數 processes = 4 # 每一個進程下的線程數量 threads = 2 # 當服務器退出的時候自動刪除unix socket文件和pid文件 vacuum = true # 使進程在後臺運行,並將日誌打到指定的日誌文件或者udp服務器 daemonize = /home/ubuntu/cnblog/uwsgi.log
建立個uwsgi.log文件
root@VM-0-9-ubuntu:/home/ubuntu/cnblog# touch uwsgi.log
二、配置nginx
打開配置文件,在Http內建立server子項以下:
vim /etc/nginx/nginx.conf
server { listen 80; # 設置監聽端口號 用於http協議 server_name 148.70.61.215; # 設置對外訪問入口,能夠是域名能夠是IP地址,我設置的是IP charset UTF-8; # 設置訪問的語言編碼 access_log /var/log/nginx/cnblog_access.log; # 訪問日誌記錄 error_log /var/log/nginx/cnblog_error.log; # 錯誤日誌記錄 location / { # 設置虛擬主機的基本信息 include uwsgi_params; uwsgi_pass 127.0.0.1:8001; # 剛纔uwsgi設置的socket uwsgi_read_timeout 2; } location /static { # 靜態文件設置,nginx本身處理 expires 7d; # 過時時間 alias /home/ubuntu/bbs/static/; # 項目靜態文件地址 } location /static/admin { #admin的css路徑 root /usr/local/lib/python3.5/dist-packages/django/contrib/admin; } }
如圖。
三、啓動uwsgi和nginx服務.
其中uwsgi使用自定義位置配置文件bbs.ini
切換到/home/Ubuntu/cnblog/目錄下運行: >> uwsgi --ini bbs.ini >> /etc/init.d/nginx restart
四、瀏覽器訪問80端口
頁面到這裏就部署完成了。
# 查看tcp鏈接 root@VM-0-9-ubuntu:/home/ubuntu/cnblog# netstat -tln # 查看8001端口,已經佔用的進程 root@VM-0-9-ubuntu:/home/ubuntu/cnblog# lsof -i:8001 # kill該進程 root@VM-0-9-ubuntu:/home/ubuntu/cnblog# kill -9 19793
二、訪問 django 的 admin 會不顯示圖片
這是因爲 找不到 css 的緣故。須要在 location 裏面加上
root@VM-0-9-ubuntu:~# vim /etc/nginx/nginx.conf
location /static/admin { #admin的css路徑 root /usr/local/lib/python3.5/dist-packages/django/contrib/admin; }
二、項目更新
項目有更新的時候,須要先關閉uwsgi而後重啓便可,關閉wsgi依然能夠用一招解決輸入:
>> killall -9 uwsgi
重啓
>> uwsgi --ini bbs.ini
>> /etc/init.d/nginx restart
Nginx服務器從新加載,以使Nginx的配置生效。
nginx -s reload