{"id":44164,"date":"2024-10-06T13:24:14","date_gmt":"2024-10-06T05:24:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/44164\/"},"modified":"2024-10-06T13:24:14","modified_gmt":"2024-10-06T05:24:14","slug":"centos-%e4%b8%8b-uwsgi-django-%e6%88%90%e5%8a%9f%e6%90%ad%e5%bb%ba%e8%a8%98%e9%8c%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/44164\/","title":{"rendered":"CentOS \u4e0b uWSGI + Django \u6210\u529f\u642d\u5efa\u8a18\u9304"},"content":{"rendered":"<h1 id=\"centos-%e4%b8%8b-uwsgi-django-%e6%88%90%e5%8a%9f%e6%90%ad%e5%bb%ba%e8%a8%98%e9%8c%84-fzcbQPlekk\">CentOS \u4e0b uWSGI + Django \u6210\u529f\u642d\u5efa\u8a18\u9304<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cDjango \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684 Python \u7db2\u9801\u6846\u67b6\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u800c uWSGI \u4f5c\u70ba\u4e00\u500b\u5f37\u5927\u7684\u61c9\u7528\u4f3a\u670d\u5668\uff0c\u80fd\u5920\u6709\u6548\u5730\u5c07 Django \u61c9\u7528\u90e8\u7f72\u5230\u751f\u7522\u74b0\u5883\u4e2d\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 CentOS \u7cfb\u7d71\u4e0a\u6210\u529f\u642d\u5efa uWSGI \u548c Django \u7684\u74b0\u5883\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-fzcbQPlekk\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u7684 CentOS \u7cfb\u7d71\u5df2\u7d93\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u66f4\u65b0\uff1a<\/p>\n<pre><code>sudo yum update -y<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5b89\u88dd\u5fc5\u8981\u7684\u4f9d\u8cf4\u5305\uff0c\u5305\u62ec Python 3 \u548c pip\uff1a<\/p>\n<pre><code>sudo yum install python3 python3-pip -y<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-django-fzcbQPlekk\">\u5b89\u88dd Django<\/h2>\n<p>\u4f7f\u7528 pip \u5b89\u88dd Django\u3002\u53ef\u4ee5\u9078\u64c7\u5b89\u88dd\u7279\u5b9a\u7248\u672c\uff0c\u9019\u88e1\u4ee5\u6700\u65b0\u7248\u672c\u70ba\u4f8b\uff1a<\/p>\n<pre><code>pip3 install django<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5275\u5efa\u4e00\u500b\u65b0\u7684 Django \u9805\u76ee\uff1a<\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<p>\u9032\u5165\u9805\u76ee\u76ee\u9304\uff1a<\/p>\n<pre><code>cd myproject<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-uwsgi-fzcbQPlekk\">\u5b89\u88dd uWSGI<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5b89\u88dd uWSGI\u3002\u53ef\u4ee5\u4f7f\u7528 pip \u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip3 install uwsgi<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-uwsgi-fzcbQPlekk\">\u914d\u7f6e uWSGI<\/h2>\n<p>\u5728\u9805\u76ee\u6839\u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u540d\u70ba <code>myproject_uwsgi.ini<\/code> \u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5167\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>[uwsgi]\nmodule = myproject.wsgi:application\nmaster = true\nprocesses = 5\nsocket = myproject.sock\nchmod-socket = 660\nvacuum = true\ndie-on-term = true\n<\/code><\/pre>\n<p>\u9019\u500b\u914d\u7f6e\u6587\u4ef6\u544a\u8a34 uWSGI \u5982\u4f55\u555f\u52d5 Django \u61c9\u7528\u3002\u4f60\u53ef\u4ee5\u6839\u64da\u9700\u8981\u8abf\u6574\u9032\u7a0b\u6578\u91cf\u548c\u5176\u4ed6\u53c3\u6578\u3002<\/p>\n<h2 id=\"%e5%95%9f%e5%8b%95-uwsgi-fzcbQPlekk\">\u555f\u52d5 uWSGI<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 uWSGI\uff1a<\/p>\n<pre><code>uwsgi --ini myproject_uwsgi.ini<\/code><\/pre>\n<p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0cuWSGI \u61c9\u8a72\u6703\u555f\u52d5\u4e26\u904b\u884c Django \u61c9\u7528\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-fzcbQPlekk\">\u5b89\u88dd Nginx<\/h2>\n<p>\u70ba\u4e86\u80fd\u5920\u901a\u904e HTTP \u8a2a\u554f Django \u61c9\u7528\uff0c\u6211\u5011\u9700\u8981\u5b89\u88dd Nginx \u4f5c\u70ba\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>sudo yum install nginx -y<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-fzcbQPlekk\">\u914d\u7f6e Nginx<\/h2>\n<p>\u5728 Nginx \u7684\u914d\u7f6e\u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u65b0\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982 <code>\/etc\/nginx\/conf.d\/myproject.conf<\/code>\uff0c\u5167\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;\n\n    location = \/favicon.ico { access_log off; log_not_found off; }\n    location \/static\/ {\n        root \/path\/to\/your\/static\/files;\n    }\n\n    location \/ {\n        include uwsgi_params;\n        uwsgi_pass unix:\/path\/to\/your\/myproject.sock;\n    }\n}<\/code><\/pre>\n<p>\u8a18\u5f97\u5c07 <code>your_domain.com<\/code> \u548c <code>\/path\/to\/your\/static\/files<\/code> \u66ff\u63db\u70ba\u5be6\u969b\u7684\u57df\u540d\u548c\u975c\u614b\u6587\u4ef6\u8def\u5f91\u3002<\/p>\n<h2 id=\"%e5%95%9f%e5%8b%95-nginx-fzcbQPlekk\">\u555f\u52d5 Nginx<\/h2>\n<p>\u5b8c\u6210\u914d\u7f6e\u5f8c\uff0c\u555f\u52d5 Nginx \u4f3a\u670d\u5668\uff1a<\/p>\n<pre><code>sudo systemctl start nginx<\/code><\/pre>\n<p>\u4e26\u8a2d\u7f6e Nginx \u958b\u6a5f\u81ea\u555f\uff1a<\/p>\n<pre><code>sudo systemctl enable nginx<\/code><\/pre>\n<h2 id=\"%e6%b8%ac%e8%a9%a6%e6%87%89%e7%94%a8-fzcbQPlekk\">\u6e2c\u8a66\u61c9\u7528<\/h2>\n<p>\u73fe\u5728\uff0c\u4f60\u53ef\u4ee5\u5728\u700f\u89bd\u5668\u4e2d\u8a2a\u554f\u4f60\u7684\u57df\u540d\uff0c\u61c9\u8a72\u80fd\u5920\u770b\u5230 Django \u7684\u6b61\u8fce\u9801\u9762\uff0c\u8868\u793a\u61c9\u7528\u5df2\u7d93\u6210\u529f\u642d\u5efa\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-fzcbQPlekk\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 CentOS \u7cfb\u7d71\u4e0a\u6210\u529f\u642d\u5efa uWSGI \u548c Django \u7684\u74b0\u5883\uff0c\u4e26\u901a\u904e Nginx \u9032\u884c\u53cd\u5411\u4ee3\u7406\u3002\u9019\u6a23\u7684\u67b6\u69cb\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u80fd\u5920\u66f4\u597d\u5730\u8655\u7406\u4e26\u767c\u8acb\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u4f86\u90e8\u7f72\u4f60\u7684\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6210\u529f\u642d\u5efa uWSGI \u548c Django \u5728 CentOS \u4e0a\u7684\u8a73\u7d30\u8a18\u9304\uff0c\u63d0\u4f9b\u6b65\u9a5f\u548c\u6280\u5de7\uff0c\u52a9\u4f60\u8f15\u9b06\u90e8\u7f72\u9ad8\u6548\u7684 web \u61c9\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-44164","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=44164"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44164\/revisions"}],"predecessor-version":[{"id":44165,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44164\/revisions\/44165"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=44164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=44164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=44164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}