uwsgi+nginx部署Django

Posted 123why

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uwsgi+nginx部署Django相关的知识,希望对你有一定的参考价值。

使防火墙firewall开放80端口
    firewall-cmd --permanent --add-port=80/tcp --zone=public

配置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  
    yum update   时间很长
安装nginx
    yum -y install nginx
查看nginx进程
    ps -ef | grep nginx
未启动时
    root      36614   2296  0 15:59 pts/0    00:00:00 grep --color=auto nginx
启动时
    root      36644      1  0 15:59 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    nginx     36645  36644  0 15:59 ?        00:00:00 nginx: worker process
    root      36647   2296  0 15:59 pts/0    00:00:00 grep --color=auto nginx

创建快捷方式
    systemctl enable nginx
启动nginx
    systemctl start nginx
默认配置文件  /etc/nginx/conf.d/defaut
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the php scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }
修改为
    server {
        listen       8080;
        server_name  47.111.118.54;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            include /etc/nginx/uwsgi_params;
            uwsgi_connect_timeout 30;
            # uwsgi的IP和端口
            uwsgi_pass 192.168.145.137:8080;   # 这里是私网IP
        }
        # 指定静态文件路径
        location /static/ {
            alias  /opt/uwsgi_test/static/;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ .php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ .php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache‘s document root
        # concurs with nginx‘s one
        #
        #location ~ /.ht {
        #    deny  all;
        #}
    }


豆瓣镜像源-i http://pypi.douban.com/simple --trusted-host pypi.douban.com

安装uwsgi
    pip3 install uwsgi -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

    pip install jupyter_contrib_nbextensions -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    pip install jupyter_nbextensions_configurator -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
    软连接
    ln -s /usr/local/python3/bin/uwsgi /usr/sbin/uwsgi

将项目传输到服务器
进入项目根目录,创建script文件夹
在文件夹中创建文件uwsgi.ini
    [uwsgi]
    # 项目目录
     socket=192.168.145.137:8080
     chdir=/opt/uwsgi_test/
     # 启动uwsgi的用户名和用户组
     uid=root
     gid=root
     # 指定项目的application
     module=uwsgi_test.wsgi:application
     # 指定sock的文件路径
     socket=/opt/uwsgi_test/script/uwsigi.sock      # sock文件是由uwsgi.ini文件启动>之后自动生成
     # 启用主进程
     master=true
     # 进程个数
     workers=5
     pidfile=/opt/uwsgi_test/script/uwsigi.pid
     # 自动移除unix Socket和pid文件当服务停止的时候
     vacuum=true
     # 序列化接受的内容,如果可能的话
     thunder-lock=true
     # 启用线程
     enable-threads=true
     # 设置自中断时间
     harakiri=30
     # 设置缓冲
     post-buffering=4096
     # 设置日志目录
     daemonize=/opt/uwsgi_test/script/uwsgi.log


通过配置文件启动
    uwsgi --ini uwsgi.ini
    停止
    uwsgi --stop uwsigi.pid
    重载
    uwsgi --reload uwsgi.ini


    报错Invalid HTTP_HOST header: 192.168.145.137. You may need to add 192.168.145.137 to ALLOWED_HOSTS
    解决:
        修改配置文件setting.py
                - ALLOWED_HOSTS = [*]


iptables安装配置
    1.关闭firewall
        systemctl stop firewalld.service;
        systemctl disable firewalld.service;
        systemctl mask firewalld.service;
    2.安装iptables 
        yum -y install iptables-services
    3.启动
        systemctl enable iptables;
        systemctl start iptables;
    4.编辑防火墙端口
        vim /etc/sysconfig/iptables    
    5.添加端口    
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
        -A INPUT -m state --state NEW -m tcp -p tcp --dport 8090 -j ACCEPT
    6.使配置生效
        systemctl restart iptables.service
    7.设置开机启动    
        systemctl enable iptables.service

        
        
        
        

 

以上是关于uwsgi+nginx部署Django的主要内容,如果未能解决你的问题,请参考以下文章

nginx + uwsgi 部署 Django+Vue项目

debian完整部署 Nginx + uWSGI + Django

Django + Uwsgi + Nginx 实现生产环境部署

centos7下部署Django(nginx+uWSGI+Python3+Django)

django+nginx+uwsgi的生产环境部署(Ubuntu16.04)

Django部署——uwsgi+Nginx(超详细)