1

我正在尝试部署一个简单的 Django 项目,但总是失败。我正在按照这里的说明进行操作

当我使用时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/

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

在此处输入图像描述

然后运行:

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

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

在此处输入图像描述

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

4

1 回答 1

0

最后,我通过使用 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>

<chdir>/home/parallels/books/mysite/mysite</chdir>只是指向,因为<module>wsgi</module>有一个wsgi.pyin /home/parallels/books/mysite/mysite/wsgi.py

然后我运行以下命令:

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

现在可以访问该网站:

在此处输入图像描述

但我无法使用uwsgi.ini.

于 2016-04-20T12:00:18.753 回答