uWSGI部署Django项目

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。WSGI是一种Web服务器网关接口。它是一个Web服务器(如nginx,uWSGI等服务器)与web应用(如用Django、Flask框架写的程序)通信的一种规范。

Windows环境安装uWSGI

 1、uWSGI包下载

直接在cmd中使用pip install uwsgi会报错,AttributeError: module 'os' has no attribute 'uname'

#离线下载uWSGI地址
https://pypi.org/project/uWSGI/#files

2、修改uwsgiconfig.py文件

#修改导入包
import platform
#将os.uname() 全部替换为platform.uname()

3、执行setup.py

#进入文件目录后再运行
python setup.py install

  发现还是报错了,原因是没有C语言编译 Exception: you need a C compiler to build uWSGI 

4、下载安装MinGW

#参考安装MinGW
https://jingyan.baidu.com/article/0320e2c11564ca1b87507b8f.html

5、再次运行setup.py

      安装成功

  

Centos7环境安装uWSGI

#安装python环境
yum install -y python3
#安装编译环境
yum install -y python3-devel gcc
#安装uWSGI
pip3 install uWSGI

uWSGI启动Django

1、创建django项目

django-admin startproject testproject

  

2、启动django 的配置

在与settings.py 同级目录下新建wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "projectName.settings")

application = get_wsgi_application() 

  

在与manager.py同级目录下创建uwsgiConfig/uwsgiConfig.ini

[uwsgi]
#项目目录
chdir = /usr/local/src/sykj-portal/sykj_website
# 虚拟环境
home=/usr/local/src/pyENV/sykj_website_ENV/
PYTHONHOME=/usr/local/src/pyENV/sykj_website_ENV//bin/
#启动uwsgi的用户名和用户组
uid = root
gid = root
#指定项目的application
module = sykj_website.wsgi:application
#启用主进程
master = True
#进程个数
processes = 2
# 使用进程数  threads = 2 使用的线程数
harakiri = 60
# 最大超时时间
max-requests = 5000
# 最大请求数,到了后就会自动重启
#指定socket
#socket = 127.0.0.1:3000  
#指定http
http = 0.0.0.0:3000
socket = /usr/local/src/sykj-portal/sykj_website/uwsgi/sock/supercrm.sock
#配置pid文件位置
pidfile = /usr/local/src/sykj-portal/sykj_website/uwsgi/master.pid
# 配置日志文件位置
daemonize = /usr/local/src/sykj-portal/sykj_website/uwsgi/log/uwsgi.log
# 服务退出或重启,自动删除pid和socket文件
vacuum = True
#序列化接受的内容,如果可能的话
thunder-lock = True
#启用线程
enable-threads = True
#设置自动中断时间
harakiri = 30
#设置缓冲
post-buffering = 4096
#项目中静态文件static的路径
static-map = /static=/usr/local/src/sykj-portal/sykj_website/sykj-static

  

nginx配置

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /var/log/nginx/access.log  main;
    
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    server {
        listen       16129;
        server_name  _;
        
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        
        location / {
        include uwsgi_params;
        uwsgi_pass unix:/usr/local/src/sykj-portal/sykj_website/uwsgi/sock/supercrm.sock;
        }
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

 

设置项目自动启动

#赋予rc.local可执行权限
chmod +x /etc/rc.d/rc.local

#在rc.local添加启动脚本路径
/usr/local/src/.../start_project.sh

#使rc.local文件生效
source /etc/rc.d/rc.local

 

posted @ 2022-11-11 15:32  QiuPing  阅读(1565)  评论(0编辑  收藏  举报