環境:Ubuntu18.04LTS+Pycharm18.03Pro+ 虛擬環境Python3.6
1、uwsgi+Nginx作爲Django的web服務器
1、 安裝
pip install uwsgi
2、更改Django中的settings.py文件
設置調試模式爲False和設置ALLOWED_HOSTS :
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
3、項目中建立一個uwsgi.ini文件
[uwsgi]
# 使用nginx連接時使用
# socket=127.0.0.1:8090
# 直接做web服務器使用 python manage.py runserver
http=127.0.0.1:8090
# 項目目錄
chdir=/home/toohoo/PycharmProjects/mydailyfresh
# 項目中的wsgi.py文件的目錄, 相當於項目目錄, 裏面有項目的入口
wsgi-file=mydailyfresh/wsgi.py
# 指定啓動的工作進程數
processes=4
# 指定工作進程中的線程數
threads=2
master=True
# 保存啓動之後主進程的pid
pidfile=uwsgi.pid
# 設置uwsgi後臺運行,uwsgi.log保存日誌信息
daemonize=uwsgi.log
# 設置虛擬環境的路徑
virtualenv=/home/toohoo/PycharmProjects/mydailyfresh/venv
4、uwsgi的啓動和停止
啓動:uwsgi --ini 配置文件路徑 例如:uwsgi --ini uwsgi.ini
停止:uwsgi --stop uwsgi.pid 路徑 例如:uwsgi --stop uwsgi.pid
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ps aux|grep uwsgi
toohoo 35637 0.0 0.0 21548 1080 pts/0 S+ 18:02 0:00 grep --color=auto uwsgi
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ls
apps celery_tasks data db db.sqlite3 deploy manage.py mydailyfresh __pycache__ README.md requirement static templates utils uwsgi.ini venv
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ps aux|grep uwsgi
toohoo 35647 21.5 0.5 132688 41540 ? S 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35649 0.0 0.4 206420 32796 ? Sl 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35651 0.0 0.4 206420 32796 ? Sl 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35652 0.0 0.4 206420 32796 ? Sl 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35655 0.0 0.4 206420 32796 ? Sl 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35656 0.0 0.5 140884 38584 ? S 18:03 0:00 uwsgi --ini uwsgi.ini
toohoo 35659 0.0 0.0 21548 1108 pts/0 S+ 18:03 0:00 grep --color=auto uwsgi
可以看到服務已經在後臺啓動起來了。
5、訪問
瀏覽器訪問:http://127.0.0.1:8090/的時候,首頁顯示成功,但是靜態文件加載不出來,因爲當設置DEBUG = False
的時候,Django自身也不能幫助我們顯示靜態文件了。因此使用Nginx來幫助我們。
6、停止
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ls
apps data db.sqlite3 manage.py __pycache__ requirement templates uwsgi.ini uwsgi.pid
celery_tasks db deploy mydailyfresh README.md static utils uwsgi.log venv
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ uwsgi --stop uwsgi.pid
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ps aux|grep uwsgi
toohoo 35941 0.0 0.0 21548 1152 pts/0 S+ 18:15 0:00 grep --color=auto uwsgi
可以看到停止了uwsgi。
上面的是最基礎的服務器配置:原理框圖:
可見上面的基礎框架並不能滿足我們的訪問需求,還要用Nginx來加上一層靜態文件的處理:
7、Nginx和uwsgi對接:
配置文件的更改:
[uwsgi]
# 使用nginx連接時使用
# socket=127.0.0.1:8090
# 直接做web服務器使用 python manage.py runserver
http=127.0.0.1:8090
...
改成,在地址的前面去掉http
加上socket
:
[uwsgi]
# 使用nginx連接時使用
socket=127.0.0.1:8090
# 直接做web服務器使用 python manage.py runserver
# http=127.0.0.1:8090
...
8、配置Nginx
首先是配置所有的請求都通過Nginx發送給uwsgi:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 默認的配置註釋掉
#location / {
# root html;
# index index.html index.htm;
#}
# 加上這個配置
location / {
# 包含uwsgi的請求參數
include uwsgi_params;
# 轉交請求給uwsgi
uwsgi_pass 127.0.0.1:8090;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
保存退出,啓動Nginx:
toohoo@ubuntu:/usr/local/nginx$ sudo sbin/nginx
ngx_http_fastdfs_set pid=36187
toohoo@ubuntu:/usr/local/nginx$ ps aux |grep nginx
root 36188 0.0 0.0 38100 684 ? Ss 18:48 0:00 nginx: master process sbin/nginx
nobody 36189 1.0 0.0 43040 4172 ? S 18:48 0:00 nginx: worker process
toohoo 36192 0.0 0.0 21544 1120 pts/3 S+ 18:49 0:00 grep --color=auto nginx
同時啓動uwsgi:
$ uwsgi --ini uwsgi.ini
...
9、訪問
瀏覽器打開:http://127.0.0.1,默認是訪問80端口,得到的顯示的結果沒有變更,靜態文件沒有加載。但是實現了第7步的Nginx與uwsgi的對接。
10、現在的問題是什麼請求都會交給uwsgi處理,因此需要設置Nginx幫助處理靜態文件,新建存放靜態文件的目錄,並設置讀取目錄的權限
$ sudo mkdir -p /var/www/mydailyfresh/static
$ sudo chmod 777 /var/www/mydailyfresh/static/
在Nginx裏面配置指定靜態文件存放的目錄:
location / {
# 包含uwsgi的請求參數
include uwsgi_params;
# 轉交請求給uwsgi
uwsgi_pass 127.0.0.1:8090;
}
# 靜態文件目錄配置
location /static {
# 指定靜態文件存放的目錄
alias /var/www/mydailyfresh/static/;
}
11、將靜態文件收集到指定的目錄
Django項目中的settings.py裏面指定靜態文件的位置:
# 指定收集靜態文件的路徑
STATIC_ROOT = '/var/www/mydailyfresh/static'
配置完成之後使用Django的命令在虛擬環境終端項目主目錄收集靜態文件:
$ python manage.py collectstatic
1756 static files copied to '/var/www/mydailyfresh/static'.
查看,除了css,js,images等靜態文件之外,還有admin需要用到的文件:
toohoo@ubuntu:/var/www/mydailyfresh/static$ ls
admin css django_tinymce index1.html js login.html register.html tiny_mce user_center_order.html
cart.html detail.html images index.html list.html place_order.html test.html user_center_info.html user_center_site.html
12、重啓Nginx,再次訪問:
toohoo@ubuntu:/usr/local/nginx$ sudo ./sbin/nginx -s reload
ngx_http_fastdfs_set pid=38419
成功加載出靜態文件,頁面顯示正常。
======== 上面的已經完成, 下面的是擴展 優化配置 ===================
2、網站優化:Nginx轉交請求給其他的地址
(環境:Ubuntu18.04, 一臺虛擬機上做的, 一個Nginx當多個用,IP都一樣,就是端口不同得了)
13、Nginx轉交請求給其他的地址
原理圖:
配置Nginx精確訪問" / "時候,反向代理去訪問celery生成的靜態首頁文件:
# = 表示精確匹配
location = / {
# 傳遞請求給靜態文件服務器的Nginx, 反向代理
proxy_pass http://192.168.0.102:8080;
}
其中生成的靜態首頁文件之前配置好的是:
server {
listen 8080;
server_name localhost;
location /static {
alias /home/toohoo/PycharmProjects/mydailyfresh/static/;
}
location / {
#root html;
root /home/toohoo/PycharmProjects/mydailyfresh/static/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
修改重啓Nginx之後得到:
$ sudo ./sbin/nginx -s reload
ngx_http_fastdfs_set pid=38966
14、訪問
①瀏覽器直接訪問IP:127.0.0.1,後面什麼都沒輸入,默認訪問的就是“ / ”,監聽的端口是80,訪問的就是celery幫助異步生成的靜態頁面,顯示正常靜態頁面。
②瀏覽器訪問IP:127.0.0.1/index/,訪問的是Django網站返回的給用戶的首頁,頁面正常顯示。
3、Nginx實現負載均衡
15、Nginx實現負載均衡
原理圖:
拷貝項目中的uwsgi.ini,並重命名爲uwsgi2.ini,更改文件內容並保存:
[uwsgi]
# 使用nginx連接時使用
socket=127.0.0.1:8091
# 直接做web服務器使用 python manage.py runserver
# http=127.0.0.1:8090
# 項目目錄
chdir=/home/toohoo/PycharmProjects/mydailyfresh
# 項目中的wsgi.py文件的目錄, 相當於項目目錄, 裏面有項目的入口
wsgi-file=mydailyfresh/wsgi.py
# 指定啓動的工作進程數
processes=4
# 指定工作進程中的線程數
threads=2
master=True
# 保存啓動之後主進程的pid
pidfile=uwsgi2.pid
# 設置uwsgi後臺運行,uwsgi.log保存日誌信息
daemonize=uwsgi2.log
# 設置虛擬環境的路徑
virtualenv=/home/toohoo/PycharmProjects/mydailyfresh/venv
保存之後並在虛擬環境終端啓動第二個uwsgi服務器,配合Django運行代碼。
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ uwsgi --ini uwsgi2.ini
[uWSGI] getting INI configuration from uwsgi2.ini
(venv) toohoo@ubuntu:~/PycharmProjects/mydailyfresh$ ps aux|grep uwsgi
toohoo 38675 0.0 0.5 132808 41388 ? S 21:45 0:01 uwsgi --ini uwsgi.ini
toohoo 38684 0.1 2.4 395480 175952 ? Sl 21:45 0:03 uwsgi --ini uwsgi.ini
toohoo 38686 0.0 0.4 206540 32940 ? Sl 21:45 0:00 uwsgi --ini uwsgi.ini
toohoo 38687 0.1 2.4 395476 175952 ? Sl 21:45 0:03 uwsgi --ini uwsgi.ini
toohoo 38690 0.1 2.4 395732 176412 ? Sl 21:45 0:03 uwsgi --ini uwsgi.ini
toohoo 39280 25.2 0.5 132808 41620 ? S 22:27 0:01 uwsgi --ini uwsgi2.ini
toohoo 39282 0.0 0.4 206540 32900 ? Sl 22:27 0:00 uwsgi --ini uwsgi2.ini
toohoo 39283 0.0 0.4 206540 32900 ? Sl 22:27 0:00 uwsgi --ini uwsgi2.ini
toohoo 39286 0.0 0.4 206540 32900 ? Sl 22:27 0:00 uwsgi --ini uwsgi2.ini
toohoo 39287 0.0 0.4 206540 32900 ? Sl 22:27 0:00 uwsgi --ini uwsgi2.ini
toohoo 39291 0.0 0.0 21548 1080 pts/0 S+ 22:27 0:00 grep --color=auto uwsgi
可以看到啓動了兩個uwsgi服務器。
16、配置Nginx的配置文件,實現負載均衡:
在監聽80端口的server外面配置上游服務器,server裏面原來的配置註釋掉,調用upstream中配置的服務器:
# 注意端口號要不同:
upstream mydailyfresh {
server 127.0.0.1:8090;
server 127.0.0.1:8091;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 默認的配置
#location / {
# root html;
# index index.html index.htm;
#}
location / {
# 包含uwsgi的請求參數
include uwsgi_params;
# 轉交請求給uwsgi
# uwsgi_pass 127.0.0.1:8090;
# 對應上面的upstream的名字,請求過來,會對請求進行一個轉發
uwsgi_pass mydailyfresh;
}
location /static {
# 指定靜態文件存放的目錄
alias /var/www/mydailyfresh/static/;
}
......
}
重啓Nginx:
$ sudo ./sbin/nginx -s reload
ngx_http_fastdfs_set pid=39421
使用命令 $tail -f 兩份日誌,在兩個終端分別打開,觀察點擊頁面請求的均衡負載處理:
$ tail -f uwsgi.log
$ tail -f uwsgi2.log
注: 均衡負載可以選擇多臺服務器,不僅僅侷限於兩臺服務器。
本博客參照代碼及項目URL:
https://github.com/too-hoo/mydailyfresh/blob/master/deploy/nginx/nginx.conf