【原创】django配置nginx+uwsgi

  • 前置条件:/root/mysite目录下有个名为mhwl的django项目
  • 复制/etc/nginx/目录下的uwsgi_params到站点目录下 sudo cp /etc/nginx/uwsgi_params /root/mysite/mhwl/
  • 在 /root/mysite/mhwl/ 下创建uwsgi.ini,并输入内容
[uwsgi]
socket=mhwl.sock
chdir=/root/mysite/mhwl
wsgi-file=mhwl/wsgi.py
process=4
threads=2
master=True
vacuum=True
pidfile=uwsgi.pid
daemonize=uwsgi.log
plugins = python3

  • 在/root/mysite/mhwl/ 下创建mhwl_nginx.conf,输入内容(也可以修改/etc/nginx/nginx.conf,这里选择了更酷一点的,在项目文件夹下创建nginx配置文件mhwl_nginx.conf)
upstream django {
    server unix:///root/mysite/mhwl/mhwl.sock; # for a file socket
    #server 127.0.0.1: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      81;
    # the domain name it will serve for
    server_name localhost; # substitute your machine's IP address or FQDN
    charset     utf-8;

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

    location /static {
        alias /root/mysite/mhwl/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /root/mysite/mhwl/uwsgi_params; # the uwsgi_params file you installed
    }
}

  • 创建mhwl_nginx.conf的软链接,使其能够为nginx所用,sudo ln -s /root/mysite/mhwl/mhwl_nginx.conf /etc/nginx/sites-enabled/

  • 启动nginx sudo service nginx start 启动uwsgi uwsgi --ini /root/mysite/mhwl/uwsgi.ini,以上设置都正确的话,访问http://localhost:81就可以看到页面了

  • 如果有错误,看页面打印信息或者错误日志 nginx错误日志 /var/log/nginx/error.log,uwsgi日志 /root/mysite/mhwl/uwsgi.log(此例中uwsgi.log所在的路径是我指定的,具体的路径还应结合自身的情况)

你可能感兴趣的:(【原创】django配置nginx+uwsgi)