【问题标题】:django nginx uwsgi deploying faildjango nginx uwsgi 部署失败
【发布时间】:2016-08-12 12:53:10
【问题描述】:

我正在尝试部署一个简单的 Django 项目,但总是失败。我是following the instruction here

当我使用python manage.py runserver 0.0.0.0:8000 时,我可以看到我的网站正在运行:

在 virtualenv 中我已经安装了 uWSGI:

pip install uwsgi

并写了test.py

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

运行uwsgi --http :8000 --wsgi-file test.py时可以看到

然后我开始安装 Nginx:

sudo apt-get install nginx
sudo /etc/init.d/nginx start

我可以看到

然后写mysite_nginx.conf:

upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 10.211.55.21; # 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 /path/to/your/mysite/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/parallels/books/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}

因为它只是一个简单的站点,所以我将媒体和静态路径注释掉了,并且uwsgi_params

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

目录:

然后运行:

sudo ln -s ~/home/parallels/books/mysite/mysite_nginx.conf
/etc/nginx/sites-enabled/

我可以在/etc/nginx/sites-enabled/ 中看到mysite_nginx.conf

然后运行:

sudo /etc/init.d/nginx restart
uwsgi --socket :8001 --wsgi-file test.py

我在我的网络浏览器中什么也看不到:

我不知道为什么,每当我尝试部署 Django 站点时,套接字部分都会失败。

【问题讨论】:

  • 你的 NginX 日志文件说什么?
  • 只有访问日志没有错误日志,,,
  • 您可以发布自己的解决方案作为答案并接受它。
  • 不要创建新问题,而是编辑原始问题以提供所有必要信息。

标签: django ubuntu nginx uwsgi deploying


【解决方案1】:

最后我通过使用 XML 配置 uWSGI 并成功部署了 Django 项目。但是,这次我没有使用 virtualenv。

我创建了虚拟主机配置/etc/nginx/sites-enabled/mysite

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name 10.211.55.21;

    access_log /var/log/nginx/mysite-access.log ;
    error_log /var/log/nginx/mysite-error.log ;

    location / {
            uwsgi_pass 127.0.0.1:8630;
            include uwsgi_params;
    }

}

然后创建/home/parallels/books/mysite/django.xml 文件,注意文件名可以是任何名称,但应以*.xml 结尾:

<uwsgi>
    <socket>127.0.0.1:8630</socket>
    <chdir>/home/parallels/books/mysite/mysite</chdir>
    <pythonpath>..</pythonpath>
    <module>wsgi</module>
</uwsgi>

&lt;chdir&gt;/home/parallels/books/mysite/mysite&lt;/chdir&gt; 只是指向&lt;module&gt;wsgi&lt;/module&gt;,因为在/home/parallels/books/mysite/mysite/wsgi.py 中有一个wsgi.py

然后我运行以下命令:

$ sudo /etc/init.d/nginx restart
$ uwsgi -x django.xml

现在网站可以访问了:

但我无法使用 uwsgi.ini 配置和部署 Django。

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2019-04-08
    • 2017-08-31
    • 2014-05-31
    • 2015-06-22
    • 2021-04-02
    相关资源
    最近更新 更多