django nginx uwsgi deploying fail

删除回忆录丶 提交于 2019-12-14 03:11:39

问题


I am trying to deploy a simple Django project, but fail all the time. I am following the instruction here.

When I use python manage.py runserver 0.0.0.0:8000 I can see that my site is running:

In the virtualenv I've installed uWSGI:

pip install uwsgi

and wrote the 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

When running uwsgi --http :8000 --wsgi-file test.py I can see

I then installed en started Nginx:

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

I can see

and then wrote 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
    }
}

because it is just a simple site I commented out the media and static paths, and 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;

the directory:

and run:

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

I can see mysite_nginx.conf within /etc/nginx/sites-enabled/:

and then run:

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

I see nothing in my webbrowser:

I do not know why, whenever I try deploying a Django site the socket part fails.


回答1:


Finally I made it by using XML to configure uWSGI and deploying Django project successfully. However, this time I do not use a virtualenv.

I created the virtual host configuration /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;
    }

}

Then created the /home/parallels/books/mysite/django.xml file, note that the name of the file can be anything but should end with *.xml:

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

the <chdir>/home/parallels/books/mysite/mysite</chdir> just point to <module>wsgi</module> because there is a wsgi.py in /home/parallels/books/mysite/mysite/wsgi.py.

I then run the following commands:

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

now the website is reachable:

But I fail to configure and deploying Django using uwsgi.ini.



来源:https://stackoverflow.com/questions/36733722/django-nginx-uwsgi-deploying-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!