nginx加uwsgi部署django

在部署uwsgi加nginx时踩了无数坑,网上写的记录大多没反应正确的问题,在此记录一下,也方便以后自己观看
我的目录结构,项目名store_contraller02,app的名称mysql_opt,静态文件在static里面

/home/sir/wwwroot/store/
├── manage.py
├── mysql_opt
├── __pycache__
├── static
├── store_contraller02
├── templates

坑1:

		大部分Linux系统都是多个python版本共存的,在多个python版本共存的情况下,一定要用python虚拟环境去搭建。
		yum -y install virtualenv
		virtualenv  /path/to/env_name  --python=python3 //指明python版本
		source /path/to/env_name/bin activate //启动虚拟环境
		pip3 install django //虚拟环境启动后在任何路径下运行都可以,文件会被安装到env_name/lib/python3/sitepakegs内
		(ubuntu版本用apt-get install安装)

坑2:

		pip3 install uwsgi //安装之前请确认安装gcc和python3-dev(用yum search去找apt-cache search)否则后报错

坑3:

[uwsgi]
#chidr用于指定自己的网站根目录(自行更改)
chdir = /home/sir/wwwroot/store/
#必须这一句才能找到app
module= store_contraller02.wsgi:application
callable = app
#python的虚拟环境,即我们最初创建的虚拟环境位置(自行更改)
virtualenv = /home/sir/store
pythonpath = /home/sir/store/lib/python3.6/site-packages
#master不用更改
master = true
#socket用于指定端口号,可以不更改
socket = :8000
#http = :8000
#以下两个可以不用更改
chmod-socket = 666
vacuum = true
wsgi-file = /home/sir/wwwroot/store/store_contraller02/urls.py
daemonize = /var/log/uwsgi/store.log
processes = 1
buffer-size = 32768
pidfile = /home/sir/uwsgi.pid

这里有两个坑:1.module= store_contraller02.wsgi:application //"store_contraller02"这个名字一定要和你的项,目名一样,就是你setting.py的父目录(前提时你使用django3.0以上版本,会自动给你配置wsgi文件),不然会包no app load.错误
2.socket = :8000 //实际使用,直接访问uwsgi会爆skip错误
#http = :8000 //测试uwsgi是否可用

坑4:

nginx在代理django时静态文件会报403错误,权限不足,为了安全,最好不要使用root,改成你运行uwsgi的用户就可以了
修改/etc/nginx/nginx.conf

user  sir;
worker_processes  1;

还用就是uwsgi_params文件,也会权限不足,原因时selinux,使用chcon --reference=nginx.conf xxxx,去更改文件的selinux属性,或者setenfoce 0 关闭selinux(暂时关闭,不需要重启,如果要永久关闭需要更改selinux文件)
最后是我的配置文件
uwsgi_nginx.conf

# for django project store
upstream store {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 10.8.0.3:8000; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name .store.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    #location /media  {
    #    alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    #}

    location /static {
        alias /home/sir/wwwroot/store/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  store;
        include     /etc/nginx/conf.d/uwsgi_params; # the uwsgi_params file you installed
    }
}

uwsgi_params

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_ADDR $server_addr;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值