Django 自帶的Socket Server 性能較差 真實的生產環境中不會使用這樣的wsgi javascript
爲解決這一問題 首先使用性能較優的uWSGI+Django 部署css
1.安裝html
pip3 install uwsgijava
使用豆瓣源 pip3 install -i https://pypi.doubanio.com/simple/ uwsgipython
在centos7安裝時若是提示 SSLError("Can't connect to HTTPS URL because the SSL module is not available.mysql
建議檢查 rpm -aq|grep openssl 是否缺乏 openssl-devel包linux
安裝 yum install openssl-devel -ynginx
使用find / -name 'python*' 找到當初下載安裝文件的目錄sql
執行makevim
執行make altinstall
不用重寫配置文件 由於setup中包含了自動尋找sll的方法 這裏使用的時3.6.5 以前的版本可能會須要修改配置文件
Successfully installed uwsgi-2.0.17.1
2.啓動
2.1直接啓動
進入到項目目錄中 (是部署的項目目錄 不是uwsgi安裝目錄)
uwsgi --http 192.168.31.93:8080 --file nb_CRM/wsgi.py --static-map=/static=static
2.2使用配置文件啓動
在項目文件夾所在的同級目錄建立script目錄用來保存ini文件
uwsgi --ini uwsgi.ini
中止
uwsgi --stop uswgi.pid
從新載入配製文件
uwsgi --reload uwsgi.pid
[uwsgi]
#項目目錄
chdir=/opt/nb_CRM/
#啓動uwsgi的用戶名和用戶組
uid=root
gid=root
#指定項目的application
module=nb_CRM.wsgi:application
#指定sock的文件路徑
socket=/opt/script/uwsgi.sock
#啓用主進程
master=true
#進程個數
workers=5
#pid文件路徑
pidfile=/opt/script/uwsgi.pid
#自動移除unix Socket和pid文件 當服務中止的時候
vacuum=true
#序列化接收的內容
thunder-lock=true
#啓用線程
enable-threads=true
#設置自動中斷時間
harakiri=30
#設置緩衝
post-buffering=4096
#設置日誌目錄
daemonize=/opt/script/uwsgi.log
#指定ip端口
http=192.168.31.93:8080
#指定靜態文件
static-map=/static=/opt/nb_CRM/static
由於uwsgi處理動態請求(form表單 動態渲染html mysql親求)較強 而靜態較弱 因此將靜態請求(html css 圖片)交由Nginx來處理
安裝Nginx
配置yum源
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
安裝
yum -y install nginx
啓動 nginx
/etc/init.d/nginx start
在centos 7 下安裝的nginx啓動命令
啓動命令 service nginx start (當 /etc/init.d/nginx不存在時)
中止命令 service nginx stop
重啓命令 service nginx restart
給nginx添加配置文件
cd /etc/nginx/conf.d
當提示80端口被佔用 nginx啓動失敗的時候
sudo fuser -k 80/tcp 幹掉佔用端口的進程
配置文件
cd /etc/nginx/conf.d/
若是cat /var/log/nginx/error.log 提示connect() to unix:/opt/script/uwsgi.sock failed (13: Permission denied) while connecting to upstream
首先給權限nginx能夠訪問 .sock 若是不行給目錄chown 用戶和用戶組
再不行看是否被SELINUX阻止
如下方法 能夠直接禁用規則 可是會有安全隱患 仍是建議有空的時候配置好訪問規則https://linuxtoy.org/archives/selinux-introduction.html
SELinux 能夠阻止程序訪問未被受權的目錄 緩解 0 day漏洞的攻擊
vi /etc/selinux/config
#SELINUX=enforcing #註釋掉
#SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增長
:wq! #保存退出
setenforce 0 #使配置當即生效