使用軟件html
nginxpython
uWSGImysql
python web服務器開發使用WSGI協議(Web Server Gateway Interface)linux
python web項目默認會生成一個wsgi.py文件,肯定好應用模塊。ios
生產環境中使用的是uWSGI,實現了WSGI全部接口,C語言編寫,效率很高的web服務器。nginx
uWSGI是一個全功能的HTTP服務器,實現了WSGI協議、uwsgi協議、http協議等。它要作的就是把HTTP協議轉化成語言支持的網絡協議。好比把HTTP協議轉化成WSGI協議,讓Python能夠直接使用。web
centos7sql
CRM項目文件shell
virtualenv數據庫
supervisor
方法一:使用xftp工具,進項上傳文件夾,將項目代碼,傳到linux服務器當中
方式2: 使用scp從本地將文件上傳到linux服務器中
scp -r 本地文件夾 遠程用戶名@遠程ip:遠程文件夾/
服務器端安裝mysql(mariadb)數據庫連接:https://www.cnblogs.com/yuncong/p/10253215.html
數據導入導出
在linux服務端,mysql,導入knight的數據 1.mysql數據的導出,與導入 這個命令是在linux/windows中敲的 mysqldump -u root -p --all-databases > knight.dump 2.上傳這個數據文件到linux數據庫中 3.在linux的mysql,導入這個數據文件 mysql -uroot -p < /opt/knight.dump
注意:linux的數據庫,須要對root用戶設置遠程連接的權限
grant all privileges on *.* to root@'%' identified by 'redhat'; # 受權全部的權限,在全部庫,全部表 對 root用戶在全部的主機上, 權限密碼爲redhat, 注意是本身設置的密碼 # 刷新受權表 flush privileges;
注意2:linux的防火牆要給關閉,不然windows去連接linux的3306端口可能被拒絕
centos7默認已經使用firewall做爲防火牆了 1.關閉防火牆 systemctl status firewalld #查看防火牆狀態 systemctl stop firewalld #關閉防火牆 systemctl disable firewalld#關閉防火牆開機啓動 systemctl is-enabled firewalld.service#檢查防火牆是否啓動
這裏使用的是virtualenv進行管理
安裝virualenvwrapper工具連接:https://www.cnblogs.com/yuncong/p/10251899.html
# 建立虛擬環境 mkvirtualenv my_django # 激活該虛擬環境 workon my_diango
激活環境後,cd到項目目錄下
可使用pip freeze模塊遷移
生成項目依賴模塊
# 在本地 pip freeze >requirements.txt
在虛擬環境中
pip install -r C:\Users\Administrator\requirements.txt # pip install -r 路徑
啓動django項目
python3 manage.py runserver 0.0.0.0:8000
啓動項目注意的問題,根據報錯進行下載相應的模塊,
項目中settings.py中倆個關鍵位置記得更改:
# 這個參數 ALLOWED_HOSTS = ['*'] # 數據庫鏈接參數 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': "crm", "HOST": "127.0.0.1", "PORT": 3306, "USER": 'root', "PASSWORD": '123' } }
啓動成功以後退出, 這裏只是爲了測試項目
安裝
pip3 install uwsgi
1 經過uwsgi啓動django項目(注意這裏要在項目目錄下)
uwsgi --http :8000 --module knight.wsgi
2 使用uwsgi.ini配置文件來啓動項目,這個文件要本身建立,隨意放在哪,我這裏是放在項目目錄中的
# 新建uwsgi.ini touch uwsgi.ini # 查看 ls uwsgi.ini
編輯uwsgi.ini,注意編輯中的路徑之類的數據要進行更改
[uwsgi] # Django-related settings # the base directory (full path) #寫上項目的絕對路徑 chdir = /opt/knight # Django's wsgi file #填寫找到django的wsgi文件,填寫相對路徑,以chdir參數爲相對路徑 module = knight.wsgi # the virtualenv (full path) #填寫虛擬環境的絕對路徑 home = /root/Envs/knight/ # process-related settings # master #啓動uwsgi主進程 master = true # maximum number of worker processes processes = 5 #若是你使用了nginx,作反向代理,必須填寫socket連接,而不是http參數 # the socket (use the full path to be safe #socket = 0.0.0.0:8000 #若是你不用nginx,直接使用uwsgi,運行一個http服務端,就用這個http參數 http = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true
指定配置文件啓動uwsgi
uwsgi --ini uwsgi.ini
啓動以後能夠經過網址進行訪問了
設置django的靜態文件目錄,收集一下
修改項目中settings.py,寫下以下參數
STATIC_ROOT= '/opt/static' #該路徑根據實際放置
使用命令收集django的靜態文件
python manage.py collectstatic
查看django的靜態文件收集目錄
ls /opt/static
配置nginx,進行反向代理,找到uwsgi項目,且配置nginx處理uwsgi的靜態文件
編輯nginx.conf
server { listen 80; #域名qishijd.com server_name qishijd.com; #只要用戶訪問qishijd.com:80/ 就走這個location匹配>,反向代理給uwsgi: location / { include uwsgi_params; uwsgi_pass 0.0.0.0:8000; } #當用戶請求是qishijd.com/static/的時候 #就會進入這個location匹配 #經過alias參數進行路徑別名,讓nginx去/opt/static底下去找靜>態資源 location /static { alias /opt/static; } }
配置完成後啓動uwsgi,重啓nginx,
在瀏覽器上輸入網址後,數據不加載,可能緣由
#若是你使用了nginx,作反向代理,必須填寫socket連接,而不是http參數 # the socket (use the full path to be safe #socket = 0.0.0.0:8000 #若是你不用nginx,直接使用uwsgi,運行一個http服務端,就用這個http參數 http = 0.0.0.0:8000
這倆只選一個
本地域名添加
在本地編輯 vim /etc/hosts # 加載你nginx.conf中這個參數對應的域名 # server_name qishijd.com; 格式爲 192.168.11.11 qishijd.com # 保存退出
注意
使用python2的模塊管理工具,去下載supervisor ,注意此時,退出虛擬環境
使用python2的模塊管理工具,去下載supervisor ,注意此時,退出虛擬環境
使用python2的模塊管理工具,去下載supervisor ,注意此時,退出虛擬環境
使用python2的模塊管理工具,去下載supervisor ,注意此時,退出虛擬環境
使用python2的模塊管理工具,去下載supervisor ,注意此時,退出虛擬環境
安裝supervisor
yum install python-setuptools
安裝
easy_install supervisor
使用supervisor命令,生配置文件
echo_supervisord_conf > /etc/supervisor.conf
在這個配置文件中加入咱們想要管理的任務
vim /etc/supervisor.conf # 在底部寫入以下配置 #定義一個任務,名字自定義 #commnad=參數,定義咱們啓動項目的命令 [program:crm_knight] command=/root/Envs/knight/bin/uwsgi /opt/knight/uwsgi.ini stopasgroup=true ;默認爲false,進程被殺死時,是否向這個進程組發送stop信號,包括子進程 killasgroup=true ;默認爲false,向進程組發送kill信號,包括子進程
經過配置文件啓動supervisor服務
supervisord -c /etc/supervisor.conf
啓動了supervisor服務端後,管理任務
supervisorctl -c /etc/supervisor.conf
任務管理命令以下:有兩種,一個是交互式,一個是參數形式
參數形式
supervisorctl -c /etc/supervisor.conf stop/start/restart all supervisorctl -c /etc/supervisor.conf start crm_knight
交互式形式
supervisorctl -c /etc/supervisor.conf