美文网首页程序员
Django + nginx + uwsgi 部署(linux)

Django + nginx + uwsgi 部署(linux)

作者: single430 | 来源:发表于2017-02-03 09:27 被阅读430次

首先确保你的系统有安装django,uwsgi,nginx,具体如何安装网上的教程一大把,这里我就写几个自己遇到的问题


* 首先

在测试uwsgi是否工作是我们都会写一个xxx.py文件,如下

def application(env, start_responce):
    start_responce("200 OK", [('Content-Type', 'text/html')])
    return [b"Hello World"] # python3
    # return ["Hello World"] # python2

运行: uwsgi --http :8000 --wsgi-file xxx.py 后,登录http:127.0.0.1:8000


* 再者

就是uwsgi.ini 和 nginx.conf 的配置了
下面我贴上自己的配置文件:

uwsgi.ini(下面xxx为你的django项目名, you是你自己的/home/you/)
[uwsgi]
# Django-related settings
# socket = :8000
socket = /home/you/xxx/xxx.sock
chmod-socket = 666
uid = you
gid = you
# the base directory (full path)
chdir = /home/zbl/xxx
# Django s wsgi file
module = xxx.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# clear environment on exit
vacuum = true

xxx_nginx.conf(下面xxx为你的django项目名, you是你自己的/home/you/)
# xxx_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/you/xxx/xxx.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      80;
    # the domain name it will serve for
    server_name www.example.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/you/xxx/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     /home/you/xxx/uwsgi_params; # the uwsgi_params file you installed
    }
}

最后

  • 就是要注意上面所有路径的权限,尤其是工程目录权限
  • 127.0.0.1 www.example.com 加入/etc/hosts
  • 权限运行 ln -s /you/xxx_nginx.conf /etc/nginx/sites-enabled/nginx
  • cp /etc/nginx/uwsgi_params /home/you/xxx
  • 重启命令 /etc/init.d/nginx restart
  • uwsgi 命令 uwsgi uwsgi.ini 之后访问 http:127.0.0.1应该出现的就是你项目的首页
  • uwsgi 安装: ***sudo apt-get install libpcre3 libpcre3-dev ***
    sudo pip install uwsgi
  • nginx 安装(网上教程需要安装许多依赖): ***sudo apt-get install nginx *** 通过访问 http://localhost:80 测试

自己遇到的一些问题,写的不好,还望指教
By: single430

相关文章

网友评论

    本文标题:Django + nginx + uwsgi 部署(linux)

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