django uWSGI nginx搭建一个web服务器 确定可用

网上的找了很多篇 不知道为什么不行,于是自己搭建了一个可用的Web

大家可按步骤尝试

总结下基于uwsgi+Nginx下django项目生产环境的部署

准备条件:

1.确保有一个能够用runserver正常启动的django项目
2.项目已上传到linuxlinux上已部署好python3环境,且已安装好项目所需的模块
安装uwsgi
pip3 install uwsgi

第一步:进入django项目
第二步:命令测试启动 
  ln -s  /usr/local/python3/bin/uwsgi /usr/bin/

 至此,uwsgi+django就完美结合了,但是,光有uwsgi还不够,uwsgi处理动态请求能力高,但对于静态请求(如static文件,css,js文件等)处理能力差,此时就要结合nginx一起使用

yum -y install nginx  (如果不行,自己更换repo)
然后将nginx放置到/local/bin目录下

nginx: [error] open() "/data/server/nginx/logs/nginx.pid" failed (2: No such file or directory)
[root@localhost s14]# nginx -c /data/server/nginx/conf/nginx.conf


目录如下
ss
|---manage.py
|---templates
|---static
|---db.sqlite3
|---cmdb        
    s14    
    ├── __init__.py
    ├── __pycache__? ??
    ├── settings.py
    ├── urls.py
    └── wsgi.py

mkdir /root/tools/tmp/
cd /root/tools/tmp
vim uwsgi.ini
   [uwsgi]
   http=:8000
   chdir=/root/tools/ss/
   master=true
   processes=4
   threads=2
   #module=s14.wsgi   
   wsgi-file=s14/wsgi.py
   #module 和 wsgi-file二选一就好       
   static-map=/static=/root/tools/ss/static
   daemonize=/root/tools/tmp/uwsgi.log
   
 uwsgi --ini uwsgi.ini

Django需要配置
settings.py  ALLOWED_HOSTS = ['*']

chmod 755 -R 项目路径(/root/tools/ss)
nginx配置如下  主要添加 user root和upstream还有server{内容}  注意下static静态文件 #号里面的内容是网上用的 可是试过 不可行 暂时没找到原因 用proxy_pass吧

user root;
worker_processes  1;  
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; 
    keepalive_timeout  65; 

    upstream django_monitor {
        server 127.0.0.1:8000;
        }   

    server {
        listen       80; 
        server_name  localhost;
        location / { 
            root   html;
            index  index.html index.htm;
        }   
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }   
    }   

    server {
        listen 8008;
        server_name 10.0.18.136;
        charset utf-8;
    
        location / { 
            #uwsgi_pass django_ocean_monitor;
            #uwsgi_pass 127.0.0.1:8000;
            #include /data/server/nginx/conf/uwsgi_params;
            proxy_pass http://django_monitor;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }   
        location /static {
        alias /root/tools/s14/static/;
        }
    }
    server {
        listen 8009;
        server_name 10.0.18.136;
        location / {
            proxy_pass http://django_text;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}