美文网首页
Centos7 + Django + Nginx +Uwsgi

Centos7 + Django + Nginx +Uwsgi

作者: manbug | 来源:发表于2017-02-23 17:39 被阅读0次

    参考https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7

    base_environment: CentOS7

    1.安装uwsgi

    yum install gcc python-devel  
    pip install uwsgi
    

    2.项目中写wsgi.py文件(放在manage.py同级目录下)

    # coding: utf-8
    import os
    
    # We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
    # if running multiple sites in the same mod_wsgi process. To fix this, use
    # mod_wsgi daemon mode with each site in its own daemon process, or use
    # os.environ["DJANGO_SETTINGS_MODULE"] = "forum.settings"
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "QCOMnewslettermanager.settings")
    
    # This application object is used by any WSGI server configured to use this
    # file. This includes Django's development server, if the WSGI_APPLICATION
    # setting points here.
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    

    3.测试uwsgi是否安装成功

    uwsgi --http :8200 --chdir /root/QCOM-newsletter-manager/QCOMnewslettermanager/ --wsgi-file wsgi.py
    

    4.创建ini文件配置uwsgi

    mkdir -p /etc/uwsgi/sites
    mkdir /run/uwsgi
    mkdir /var/log/uwsgi
    cd /etc/uwsgi/sites
    vim xx.ini
    
    [uwsgi]
    
    chdir = /var/project/QCOM-newsletter-manager/QCOMnewslettermanager/
    module = wsgi:application
    
    master = true
    processes = 5
    
    uid = nginx
    socket = /run/uwsgi/QCOMnewslettermanager.sock
    chown-socket = root:nginx
    chmod-socket = 660
    vacuum = true
    daemonize = /var/log/uwsgi/newsletter.log
    pidfile = /run/uwsgi/newsletter.pid
    

    if error:

    plugins = python 
    

    5.创建uwsgi系统启动文件

    vim /etc/systemd/system/uwsgi.service

    [Unit]
    Description=uWSGI Emperor service
    
    [Service]
    ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown user:nginx /run/uwsgi
    ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
    Restart=always
    KillSignal=SIGQUIT
    Type=notify
    NotifyAccess=all
    
    [Install]
    WantedBy=multi-user.target
    

    6.安装nginx

    yum install nginx
    

    7.配置nginx

    vim /etc/nginx/nginx.conf
    
    server {  
        listen 80;  
        server_name 120.27.123.32;  
        location /static/ {  
            root /var/project/QCOM-newsletter-manager;  
        }  
        location / {  
            include uwsgi_params;  
            uwsgi_pass unix:/run/uwsgi/QCOMnewslettermanager.sock;  
        }  
    }  
    

    8.启动

    sudo systemctl start nginx  
    sudo systemctl start uwsgi(或者单独启动文件uwsgi --ini xx.ini)
    

    9.QUESTION

    • admin不加载css,js
      修改settings.py中static的ROOT路径,改为绝对路径后,python manage.py collectstatic
    • 配置后无法提交图片
      修改用户组
    chown -R nginx QCOM-newsletter-manager/
    chgrp -R nginx QCOM-newsletter-manager/
    
    • 报错:

    uwsgi: option '--http' is ambiguous; possibilities: '--http-socket' '--https-socket-modifier2' '--https-socket-modifier1' '--https-socket' '--http-socket-modifier2' '--http-socket-modifier1' getopt_long() error

    yum install uwsgi-plugin-python
    plugins = python (加在ini配置文件中)
    

    相关文章

      网友评论

          本文标题:Centos7 + Django + Nginx +Uwsgi

          本文链接:https://www.haomeiwen.com/subject/aovbwttx.html