django nginx uwsgi部署失败

时间:2016-04-20 04:18:04

标签: django ubuntu nginx uwsgi deploying

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

当我使用var exec = require('child_process').exec; var fs = require('fs'); var user_id = process.argv[2]; var path = './'+user_id+'/'; var command = "cd "+path+" & java Main < input.txt"; exec(command ,{maxBuffer:1024*800,timeout:20000}, function( error , stdout , stderr ){ if(error) { if(error.toString().indexOf('Error: stdout maxBuffer exceeded') != -1) { console.log('Error: Stdout maxBuffer exceeded. You might have initialized an infinite loop.'); } else { console.log('Error: Program contained an error while executing'); } } else { fs.writeFile( path + "/out.txt" , stdout , function (err){}); console.log(stdout); } }); 时,我可以看到我的网站正在运行:

enter image description here

在virtualenv中我安装了uWSGI:

python manage.py runserver 0.0.0.0:8000

并撰写了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 时,我可以看到

enter image description here

然后我安装了en启动Nginx:

uwsgi --http :8000 --wsgi-file test.py

我可以看到

enter image description here

然后写了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

目录:

enter image description here

并运行:

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

enter image description here

然后运行:

/etc/nginx/sites-enabled/

我的网络浏览器中没有看到任何内容:

enter image description here

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

1 个答案:

答案 0 :(得分: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.py中有/home/parallels/books/mysite/mysite/wsgi.py

然后我运行以下命令:

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

现在网站可以访问:

enter image description here

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

相关问题