{"id":2722,"date":"2016-06-07T20:30:20","date_gmt":"2016-06-07T12:30:20","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2722"},"modified":"2016-06-07T20:30:20","modified_gmt":"2016-06-07T12:30:20","slug":"%e5%9f%ba%e4%ba%8enginx%e5%92%8cuwsgi%e5%9c%a8centos%e4%b8%8a%e9%83%a8%e7%bd%b2django","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2722.html","title":{"rendered":"\u57fa\u4e8eNginx\u548cuWSGI\u5728CentOS\u4e0a\u90e8\u7f72Django"},"content":{"rendered":"<p>=Start=<\/p>\n<h5>\u7f18\u7531\uff1a<\/h5>\n<p>Django\u5176\u5b9e\u81ea\u5e26\u4e86\u4e00\u4e2aWeb\u670d\u52a1\u5668\uff0c\u4f46\u662f\u5efa\u8bae\u53ea\u5728\u5f00\u53d1\/\u6d4b\u8bd5\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5728\u4fee\u6539Python\u4ee3\u7801\u540e\u4f1a\u81ea\u52a8\u91cd\u542f\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u5f00\u53d1\uff0c\u4f46\u662f\u7531\u4e8e\u6027\u80fd\u95ee\u9898\uff0c\u5efa\u8bae\u53ea\u7528\u6765\u6d4b\u8bd5\uff0c\u4e0d\u8981\u7528\u5728\u751f\u4ea7\u73af\u5883\u3002<\/p>\n<p>\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4e00\u822c\u662f\u4f7f\u7528Apache\u6216Nginx\u4f5c\u4e3aWeb\u670d\u52a1\u5668\uff0c\u6240\u4ee5\uff0c\u8fd9\u91cc\u6211\u4eec\u8bb2\u4e00\u8bb2\u5982\u4f55\u57fa\u4e8eNginx\u548cuWSGI\u5728CentOS\u4e0a\u90e8\u7f72Django\u3002<\/p>\n<h5>\u6b63\u6587\uff1a<\/h5>\n<p>\u4e3b\u8981\u53c2\u8003\u6587\u7ae0\u300c<a href=\"http:\/\/www.jianshu.com\/p\/e6ff4a28ab5a\">\u57fa\u4e8enginx\u548cuWSGI\u5728Ubuntu\u4e0a\u90e8\u7f72Django<\/a>\u300d\u8fdb\u884c\u914d\u7f6e\uff0c<span style=\"color: #ff0000;\"><strong>\u671f\u95f4\u78b0\u5230\u4e86\u5f88\u591a\u95ee\u9898\uff0c\u4e3b\u8981\u662f\u6743\u9650\u95ee\u9898<\/strong><\/span>\u2026\u2026<\/p>\n<h6>1.\u5b89\u88c5Nginx<\/h6>\n<p>CentOS 6.5\u9ed8\u8ba4\u7684yum\u6e90\u4e2dNginx\u7684\u7248\u672c\u8f83\u8001\uff0c\u800c\u4e14\uff0c\u4e3a\u4e86\u4f7f\u7528\u4e00\u4e9b\u65b0\u7684\u7279\u6027\uff0c\u6211\u8fd9\u91cc\u7528OpenResty\u66ff\u4ee3Nginx\uff0c\u4e0b\u9762\u662f\u5b89\u88c5\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true \">$ sudo yum install readline-devel pcre-devel openssl-devel perl\n$ wget https:\/\/openresty.org\/download\/openresty-1.9.7.3.tar.gz\n$ tar zxf openresty-1.9.7.3.tar.gz\n$ cd openresty-1.9.7.3\n\n$ .\/configure --prefix=\/opt\/openresty \\\n              --with-http_stub_status_module \\\n              --with-http_iconv_module\n\n$ gmake\n$ sudo gmake install\n\nexport PATH=\/opt\/openresty\/nginx\/sbin:$PATH<\/pre>\n<h6>2.\u5b89\u88c5uWSGI\u548csupervisor<\/h6>\n<pre class=\"lang:default decode:true \">$ sudo -H pip install uwsgi supervisor<\/pre>\n<h6>3.\u57fa\u4e8e uWSGI \u548c Nginx \u90e8\u7f72 Django<\/h6>\n<p>\u539f\u7406\uff1a<\/p>\n<p>the web client &lt;-&gt; the web server(nginx) &lt;-&gt; the socket &lt;-&gt; uwsgi &lt;-&gt; Django<\/p>\n<p>\u57fa\u672c\u5355\u9879\u6d4b\u8bd5\uff1a<\/p>\n<ol>\n<li>\u6d4b\u8bd5 uWSGI \u662f\u5426\u6b63\u5e38 [the web client &lt;-&gt; uWSGI &lt;-&gt; Python]<\/li>\n<li>\u6d4b\u8bd5 Django \u9879\u76ee\u662f\u5426\u6b63\u5e38 [the web client &lt;-&gt; Django]<\/li>\n<li>\u6d4b\u8bd5 uWSGI + Django \u9879\u76ee\u662f\u5426\u6b63\u5e38 [the web client &lt;-&gt; uWSGI &lt;-&gt; Django]<\/li>\n<li>\u6d4b\u8bd5 Nginx \u662f\u5426\u6b63\u5e38 [the web client &lt;-&gt; the web server(nginx)]<\/li>\n<\/ol>\n<p>\u7ec4\u5408\u9879\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#test.py\u7684\u5185\u5bb9\ndef application(env, start_response):\n    start_response('200 OK', [('Content-Type', 'text\/html')])\n    return [\"Hello World\"]\n\n#nginx.conf\u7247\u6bb5\nserver {\n    listen      80;\n    server_name localhost;\n\n    charset     utf-8;\n    client_max_body_size 75M;\n\n    location \/assets {\n        alias \/opt\/learn_django\/mysite\/assets;\n    }\n\n    location \/ {\n        uwsgi_pass  unix:\/\/\/tmp\/mysite.sock;\n        #uwsgi_pass  127.0.0.1:8001;\n        include     \/etc\/nginx\/uwsgi_params;\n    }\n}<\/pre>\n<pre class=\"lang:default decode:true\">\u4e00\u3001Nginx and uWSGI and test.py\n    \u7f16\u8f91 nginx.conf \u4f7f\u5176\u652f\u6301\n        \u7aef\u53e3\u8f6c\u53d1   [uwsgi_pass 127.0.0.1:8001;]\n    \u6216\n        socket    [uwsgi_pass unix:\/\/\/tmp\/mysite.sock;]\n\n    \u6d4b\u8bd5 Nginx \u914d\u7f6e\u8bed\u6cd5\u95ee\u9898\n        sudo \/etc\/init.d\/nginx configtest\n        sudo \/etc\/init.d\/nginx reload\n        sudo \/etc\/init.d\/nginx restart\n\nuwsgi --socket :8001 --wsgi-file test.py\nuwsgi --socket mysite.sock --wsgi-file test.py\nuwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666\nuwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664\n\n    \u5982\u679c\u6ca1\u6709\u6210\u529f\uff1a\n        \u68c0\u67e5 Nginx \u9519\u8bef\u65e5\u5fd7(\/var\/log\/nginx\/error.log)\n\n\n\u4e8c\u3001Nginx and uWSGI and Django application\nuwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664\n\n# \u521b\u5efa uwsgi.ini \u7b80\u5316\u542f\u52a8\u547d\u4ee4\n\u5728application\u76ee\u5f55\u4e0b\u521b\u5efa\u6587\u4ef6 mysite_uwsgi.ini \uff0c\u6309\u9700\u4fee\u6539\u5185\u5bb9\u540e\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u8fdb\u884c\u542f\u52a8\uff1a\nuwsgi --ini mysite_uwsgi.ini<\/pre>\n<h6>4.\u4f7f\u7528supervisor\u6765\u7ba1\u7406 uWSGI \u8fdb\u7a0b<\/h6>\n<pre class=\"lang:default decode:true \">(sudo) echo_supervisord_conf &gt; \/etc\/supervisord.conf\n\u6253\u5f00 supervisor.conf \u5728\u6700\u5e95\u90e8\u6dfb\u52a0\uff08\u6bcf\u4e00\u884c\u524d\u9762\u4e0d\u8981\u6709\u7a7a\u683c\uff0c\u9632\u6b62\u62a5\u9519\uff09\uff1a\n[program:mysite]\ncommand=\/path\/to\/uwsgi --ini mysite_uwsgi.ini\ndirectory=\/path\/to\/mysite\nstartsecs=0\nstopwaitsecs=0\nautostart=true\nautorestart=true\n\n\u542f\u52a8 supervisor\n(sudo) supervisord -c \/etc\/supervisord.conf\n\n\u91cd\u542f supervisor\n(sudo) supervisorctl -c \/etc\/supervisord.conf restart mysite\n\u6216\n(sudo) supervisorctl -c \/etc\/supervisord.conf restart all<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.ziqiangxuetang.com\/django\/django-nginx-deploy.html\">Django \u90e8\u7f72(Nginx)<\/a><\/li>\n<li><a href=\"http:\/\/www.jianshu.com\/p\/e6ff4a28ab5a\">\u57fa\u4e8enginx\u548cuWSGI\u5728Ubuntu\u4e0a\u90e8\u7f72Django<\/a><\/li>\n<li><a href=\"http:\/\/uwsgi-docs.readthedocs.io\/en\/latest\/tutorials\/Django_and_nginx.html\">Setting up Django and your web server with uWSGI and nginx<\/a><\/li>\n<li>#<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/28732692\/nginxdjangouwsgi-static-files-403-forbidden\">http:\/\/stackoverflow.com\/questions\/28732692\/nginxdjangouwsgi-static-files-403-forbidden<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/23834368\/403-forbidden-nginx-not-serving-statics-in-django-admin-panel\">http:\/\/stackoverflow.com\/questions\/23834368\/403-forbidden-nginx-not-serving-statics-in-django-admin-panel<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/20182329\/nginx-is-throwing-an-403-forbidden-on-static-files\">http:\/\/stackoverflow.com\/questions\/20182329\/nginx-is-throwing-an-403-forbidden-on-static-files<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a Django\u5176\u5b9e\u81ea\u5e26\u4e86\u4e00\u4e2aWeb\u670d\u52a1\u5668\uff0c\u4f46\u662f\u5efa\u8bae\u53ea\u5728\u5f00\u53d1\/\u6d4b\u8bd5\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u5728\u4fee\u6539Py [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,12],"tags":[627,75,628,629],"class_list":["post-2722","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-tools","tag-django","tag-nginx","tag-supervisor","tag-uwsgi"],"views":8885,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2722"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2722\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}