Django+nginx+uwsgi+dwebsocket 启动方法

87193750 2020-05-30

Django+nginx+uwsgi+dwebsocket 启动方法

收集静态文件

:~$ cat /app/cmdb/cmdb/settings/dev.py 
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), ‘static‘)

#然后执行收集命令
python3 manage.py collectstatic

安装uwsgi模块

pip3 install uwsgi

#如果安装不上使用编译安装
tar xf uwsgi-latest.tar.gz
cd uwsgi-2.0.18/
sudo python3 uwsgiconfig.py --build
sudo python3 setup.py install

用uwsgi 启动django的dwebsocket需要加格外参数

:~$ cat /app/cmdb/cmdb/settings/dev.py 
WEBSOCKET_FACTORY_CLASS = ‘dwebsocket.backends.uwsgi.factory.uWsgiWebSocketFactory‘

启动uwsgi模块

uwsgi --http 192.168.10.20:8000 --chdir /app/cmdb --wsgi-file cmdb/wsgi.py --http-websockets --http-timeout 300 --async 30 --ugreen

配置nginx

:/server/tools/uwsgi-2.0.18$ cat /etc/nginx/conf.d/default.conf 
server{
    listen  80;
    charset utf-8;
    server_name www.opesn.com;

     location /  {
        proxy_pass http://192.168.10.20:8000;
        proxy_set_header Host $host;
        proxy_http_version 1.1; 
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 60;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
     }

        location /static/ {
            alias /app/cmdb/static/;
        }
}

配置systemd启动uwsgi

:~$ cat /etc/systemd/system/kkbuluo_cmdb.service 
[Unit]
Description=HTTP Interface Server
After=syslog.target

[Service]
KillSignal=SIGQUIT
User=www
Group=www
ExecStart=/usr/local/bin/uwsgi --http 192.168.10.20:8000 --chdir /app/cmdb --wsgi-file cmdb/wsgi.py --logto /app/cmdb/logs/uwsgi.log --master --processes 2 --threads 1 --http-websockets --async 30 --ugreen --http-timeout 300
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog

[Install]
WantedBy=multi-user.target

相关推荐