1. 程式人生 > 實用技巧 >redis叢集節點新增與刪除

redis叢集節點新增與刪除

Django部署

部署

Nginx

nginx操作

nginx基本配置

nginx基本操作

nginx遇到的問題

nginx對接uwsgi

nginx對接不同的伺服器

nginx負載均衡

簡介

配置

使用案例

簡單測試

部署

django中自帶開發者伺服器

runserver

路由處理功能,動態資源處理

如果是debug的話,具有靜態資源處理的功能

功能健壯,效能比較低,僅僅使用於開發

部署不會使用單一伺服器

Apache

Nginx

HTTP伺服器

處理靜態資源

反向代理

uWSGI HTTP伺服器

gunicorn HTTP伺服器

郵件伺服器

流媒體伺服器

Nginx

中文文件

淘寶網頁書籍、安裝https://www.cnblogs.com/fengff/p/8892590.html

nginx操作

// 預設是以後臺程序的方式啟動
sudo nginx
// 如果是編譯安裝
// sudo /opt/soft/nginx/start/nginx

// 檢視啟動程序id
ps -ef|grep nginx

// 和諧關閉
sudo nginx -s stop

// nginx根據指定配置檔案啟動專案,一般先測試,測試通過在啟動部署
sudo nginx -t -c ***/nginx.conf

// nginx啟動專案,此時只能訪問靜態資源(nginx充當HTTP伺服器),
// 要想訪問動態資源還需配置uwsgi(nginx充當代理伺服器)
sudo nginx 
-c ***/nginx.config

nginx基本配置

// nginx配置檔案nginx.conf,放在專案根目錄下

# nginx使用者名稱
user www-data;
# nginx工作的程序數量
worker_processes auto;
# nginx執行時儲存自己pid的檔案。nginx是通過將自己的pid記錄下來的方式,然後當給nginx發訊號時,
# 它會去nginx.pid檔案內找啟動的程序號,然後給當前nginx啟動的程序號發訊號
pid /run/nginx.pid;

# 工作模式
events {
   # 最大連結數
   worker_connections 768;
   
# multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## # 訪問日誌和錯誤日誌,一般越來越大的檔案會放在var目錄下 access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## # 開啟gzip gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; server { # nginx代理服務啟動的ip地址以及埠號(供客戶端訪問的ip埠),default_server預設是localhost listen 80 default_server; listen [::]:80 default_server; # 專案的跟路徑 root /home/liuwei/PycharmProjects/DjangoProDemo/DjangoDemo; # Add index.php to the list if you are using PHP # index index.html index.htm index.nginx-debian.html; server_name _; # 匹配靜態目錄路由 location /static { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; alias /home/liuwei/PycharmProjects/DjangoProDemo/DjangoDemo/static; } # 匹配除了靜態檔案的路由[動態資源需要使用反向解析,指定對接伺服器,和python最和掐的nginx提供了uwsgi] # / 表示匹配所有 location / { # 載入uwsgi的一些引數 include /etc/nginx/uwsgi_params; # nginx將請求轉發給監聽如下地址的伺服器處理,然後nginx將其他伺服器處理的結果在交給客戶端 uwsgi_pass 0.0.0.0:8888; } } }

nginx基本操作

# nginx對接uwsgi
# 安裝uwsgi
sudo apt-get install python3-dev  // 如果不安裝這個直接安裝uwsgi會報錯Command "/home/liuwei/PycharmProjects/DjangoProDemo/venv/bin/python 
pip install uWSGI

# uwsui啟動專案(可以訪問靜態資源,但是不能訪問靜態資源)
uwsgi --ini ****/uwsgi.ini

# 關閉專案
uwsgi --stop uwsgi.pid

# 檢視uwsig 啟動資訊
ps 17331

nginx遇到的問題

# 使用預設方式安裝完系統之後,不要急著裝conda和配置pip映象源,裝了conda就很難在裝上python3-dev
# 先去更新以下 
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-dev
# 先去裝python3-dev,這就沒報錯說安不上python3-dev,少依賴啥的錯誤了。

之前在安裝uwsgi報錯:
Building wheel for uwsgi (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/liuwei/mycode/py/envs/structappenv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-x_v8_6dt/uwsgi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-x_v8_6dt/uwsgi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-9flg0dwl --python-tag cp36
       cwd: /tmp/pip-install-x_v8_6dt/uwsgi/
  Complete output (163 lines):
  /home/liuwei/.conda/envs/py366/lib/python3.6/distutils/dist.py:261: UserWarning: Unknown distribution option: 'descriptions'
    warnings.warn(msg)
  running bdist_wheel
  
  # 自己後來又研究了一下,在虛擬環境virtualenvwrapper下:
          # 不配置pip阿里映象源,直接在虛擬環境裡安裝uwsgi,能成功!(會使用之前安裝的python3-dev去編譯?)
          # 配置pip阿里映象源,安裝uwsgi他會自動下載再次編譯,就會報上面的錯誤。(不會使用之前安裝的python3-dev去編譯?)
          
  # 我在想,如果我不使用conda或許就不會出現這個問題。

nginx對接uwsgi

// uwsgi配置檔案uwsgi.ini,放在專案根目錄下

[uwsgi]

# 如果是使用Nginx連線使用:
socket = 0.0.0.0:8888
# 如果是直接作為web伺服器使用:
# http=127.0.0.1:8080


# 配置工程目錄
chdir=/home/liuwei/PycharmProjects/DjangoProDemo/DjangoDemo
# 配置專案的wsgi目錄,相對於工程目錄。相對路徑相對於chdir目錄
wsgi-file=DjangoDemo/wsgi.py

#配置程序、執行緒資訊,這取決於伺服器的配置
processes=2
threds=2
# 是否開啟多執行緒模式
enable-threads=True
# 是否開啟主從結構
master=True
# 程序id儲存檔案,相對於當前檔案路徑,啟動時會自動建立
pidfile=uwsgi.pid
# 日誌檔案
daemonize=uwsgi.log

先啟動uwsgi後啟動nginx,遇到問題看日誌!

nginx對接不同的伺服器

// nginx 對接uwsgi
location / {
     include /etc/nginx/uwsgi_params;
     uwsgi_pass 0.0.0.0:8888;
}

// nginx也可以直接對接runserver,作為反向代理
 location / {
     # runserver啟動埠http://127.0.0.1:9000
     proxy_pass http://127.0.0.1:9000;
}

nginx負載均衡

簡介

Nginx伺服器單臺最多處理50000併發。負載均衡是有一臺專門的伺服器作請求分發【當然還可以繼續巢狀,一個主負載,多個從負載】,請求分發給多個業務邏輯伺服器去處理業務。

配置

- 負載均衡模組,通過一個簡單的排程演算法來實現客戶ip到後端伺服器的負載均衡

- 配置
    upstream myproject {
        ip_hash;
        server 192.1.10.113:8000 weight=5;
        server 192.1.10.117:8000 down;
        server 192.110.1.118:8000 weight=2;
        server 192.45.178.125:8000 backup;
       # fair;
    }
    
- 負載均衡演算法
    weight 負載權重,權重越高,分的請求越多
    down 當前server不參與負載均衡
    backup 其他機器全down掉或滿載使用此服務
    ip_hash 按每個請求的hash結果來分配,session不會亂
    fair 按後端響應時間來分 (第三方的)

使用案例

http {
  upstream myproject {
      server 127.0.0.1:8000 weight=3;
      server 127.0.0.1:8001;
      server 127.0.0.1:8002;
      server 127.0.0.1:8003;
  }

  server {
      listen 80;
      server_name www.domain.com;
      location / {
          proxy_pass http://myproject;
      }
  }
}

簡單測試

- 安裝ApacheBench
    sudo apt install apache2-utils
    
- 使用ApacheBeanch
    - 檢視使用文件
        ab
    - 訪問網站
        ab http://www.baidu.com/