1、騰訊雲
第一步肯定是去注冊並買一個IP地址雲服務器。
操作系統
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:~#
2、Mysql
1、安裝mysqly ,需要y確認
apt-get install mysql-server
設置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個回車 ok
mysql_secure_installation
啟動mysql服務
root@VM-0-9-ubuntu:~# service mysql start
配置字符集,
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服務
root@VM-0-9-ubuntu:~# service mysql restart
2、測試數據庫
創建項目的數據庫 # 我的數據庫
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)
3、python3 +django2.0
1、Ubuntu自帶python3.5版本
2、安裝pip3
root@VM-0-9-ubuntu:~# apt-get install python3-pip
測試pip3
root@VM-0-9-ubuntu:~# pip3
3、安裝django2.0
root@VM-0-9-ubuntu:~# pip3 install django==2.0
測試
4、 uWSGI,uwsgi,WSGI
1、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.
2、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
5、上傳本地django項目,測試
1、用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
樹狀結構:

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
2、配置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
3、啟動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
這樣雖然可以運行了,但是靜態文件沒有加載出來。
4、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文件夾內的。
5、至此項目確認沒有問題。
5、nginx
1、什么是 Nginx?
Nginx (engine x) 是一款輕量級的 Web 服務器 、反向代理服務器及電子郵件(IMAP/POP3)代理服務器。
http://www.cnblogs.com/venicid/p/8440576.html
2、什么是反向代理
反向代理(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)穩定性高:用於反向代理,宕機的概率微乎其微
4、安裝
apt-get install nginx
5、測試
url中輸入你的IP地址
6、nginx命令以及配置文件
https://www.cnblogs.com/jingmoxukong/p/5945200.html
6、nginx與uwsgi通信
通過 uWSGI 啟動 django 的web服務,nginx 將請求中轉給 uWSGI 然后返回。
1、配置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

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
2、配置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; } }
如圖。
3、啟動uwsgi和nginx服務.
其中uwsgi使用自定義位置配置文件bbs.ini
切換到/home/Ubuntu/cnblog/目錄下運行: >> uwsgi --ini bbs.ini >> /etc/init.d/nginx restart
4、瀏覽器訪問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
2、訪問 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; }
2、項目更新
項目有更新的時候,需要先關閉uwsgi然后重啟即可,關閉wsgi依然可以用一招解決輸入:
>> killall -9 uwsgi
重啟
>> uwsgi --ini bbs.ini
>> /etc/init.d/nginx restart
Nginx服務器重新加載,以使Nginx的配置生效。
nginx -s reload
