python django默認啓動
python3 manage.py runserver 0.0.0.0:8000這種方式調用wsgiref單機模塊,性能較低,生產環境不用css
線上使用uwsgi工具(由c語言編寫的工具,性能強悍)啓動django,
使用方式:python
在激活虛擬環境的前提下,使用uwsgi
安裝配置好virtualenvwrapper工具,或者virtualenv皆可nginx
在虛擬環境下安裝uwsgiweb
pip3 install -i https://pypi.douban.com/simple uwsgi
手動建立uwsgi.ini,放在crm項目下,便於管理
touch uwsgi.ini
配置文件uwsgi.ini的內容以下,這就是一個普通文本,裏面定義了功能參數django
[uwsgi] # Django-related settings # the base directory (full path) #項目的絕對路徑,定位到nbcrm的第一層 chdir = /opt/NBcrm # Django's wsgi file # 找到項目第二層的wsgi文件(基於上面的路徑) module = NBcrm.wsgi # the virtualenv (full path) # 找到虛擬環境的絕對路徑 home = /root/Envs/nbcrm # process-related settings # master # 主進程 master = true # maximum number of worker processes # 開啓uwsgi的多進程數,根據cpu核數來定義 processes = 16 # the socket (use the full path to be safe # 基於socket連接運行crm,只有與nginx結合的時候,才使用socket形式 socket = 0.0.0.0:8000 # 當你沒用nginx,調試項目的時候,使用http形式 #http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
【指定一個參數,輸入日誌】 【若是你使用了supervisor,請註釋掉這個參數】 #守護進程在後臺運行,且將日誌信息,輸出到uwsgi.log日誌中 #daemonize = uwsgi.log #守護進程在後臺運行 #daemonize = True
經過uwsgi.ini配置文件,啓動crm後端
# 相對路徑啓動 uwsgi --ini uwsgi.ini
此時後端項目uwsgi已經啓動,可是因爲uwsgi默認不解析django的靜態文件,須要收集django全部靜態文件,而後讓nginx去處理靜態文件
而且使用nginx的反向代理特性.瀏覽器
請求流程:app
用戶發起請求, >>>>>>>>>訪問192.168.16.142(或者訪問域名www.s20crm.com) ,socket
請求走到nginx這一層代理,>>>>>>>>>nginx直接轉發(uwsgi_pass)給後端django的地址
django處理完畢 >>>>>>>響應給nginx >>>>>> nginx返回結果給瀏覽器界面工具
一、修改django的settings.py靜態文件
添加以下參數:
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ #自定義的文件夾,統一管理django全部的靜態文件 STATIC_ROOT='/opt/s20static' STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR,'statics'), ]
二、執行命令,收集crm的靜態文件
python3 /opt/NBcrm/manage.py collectstatic
三、配置nginx的location路徑匹配,找到crm這些靜態文件
# 在nginx.conf中找到server{}標籤,添加以下參數 # 當個人請求url是 192.168.16.142:80/static/xxxxxxxx location /static { alias /opt/s20static/; }
server { listen 80; server_name localhost; #最低級路徑匹配,當請求url是 www.s20crm.com(192.168.16.142/) location / { #include翻譯就是包含的意思,將外部一個文件的內容,包含進來 include uwsgi_params; # 找的是用uwsgi啓動django的主機,nginx和項目是分離的不在一個主機上 uwsgi_pass 127.0.0.1:8000; } #當請求是 192.168.16.142/static/xxxxx #解析NBcrm靜態文件內容,處理css js location /static { alias /opt/s20static/; } }
經過訪問www.crm.com會找到真實的ip,對真實的ip進行資源請求
設置域名:
本地host文件:
在本地構造虛擬網址:
使用supervisor進程管理工具,管理你的項目
其實,supervisor就是在幫你執行命令而已
使用supervisor管理進程,這個進程不得在後臺運行,
退出虛擬環境,在物理環境下安裝supervisor
1.安裝命令 pip3 install -i https://pypi.douban.com/simple supervisor 2.建立supervisor的配置文件 echo_supervisord_conf > /etc/supervisor.conf 3.編輯配置文件,寫入管理nbcrm的任務參數 [program:s20nbcrm] command=/root/Envs/nbcrm/bin/uwsgi --ini uwsgi.ini command=/root/Envs/django01/bin/uwsgi --ini /root/Envs/django01/crm_rbac/homework_crm/uwsgi.ini stopasgroup=true ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程 killasgroup=true ;默認爲false,向進程組發送kill信號,包括子進程 4.啓動supervisor,去管理uwsgi supervisord -c /etc/supervisor.conf #指定配置文件,啓動這個服務 5.經過supervisorctl管理命令,管理uwsgi supervisorctl -c /etc/supervisor.conf
管理uwsgi
# 狀態 status all # 啓動 start s20nbcrm # 中止 stop s20nbcrm # 中止全部 stop all
注意:
若是想要django的admin咱們須要在物理環境下建立超級用戶:
python3 manage.py createsuperuser
在虛擬環境下建立的超級用戶是隔離的,沒法使用