{"id":44046,"date":"2024-10-06T12:24:13","date_gmt":"2024-10-06T04:24:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/44046\/"},"modified":"2024-10-06T12:24:13","modified_gmt":"2024-10-06T04:24:13","slug":"centos-%e9%83%a8%e7%bd%b2-nginx-uwsgi-django","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/44046\/","title":{"rendered":"CentOS \u90e8\u7f72 Nginx + uWSGI + Django"},"content":{"rendered":"<h1 id=\"centos-%e9%83%a8%e7%bd%b2-nginx-uwsgi-django-onBsIhWDCY\">CentOS \u90e8\u7f72 Nginx + uWSGI + Django<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cDjango \u662f\u4e00\u500b\u975e\u5e38\u6d41\u884c\u7684 Python \u7db2\u9801\u6846\u67b6\uff0c\u56e0\u5176\u7c21\u55ae\u6613\u7528\u548c\u5f37\u5927\u7684\u529f\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u70ba\u4e86\u6709\u6548\u5730\u90e8\u7f72 Django \u61c9\u7528\uff0cNginx \u548c uWSGI \u662f\u5169\u500b\u5e38\u7528\u7684\u7d44\u4ef6\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 CentOS \u7cfb\u7d71\u4e0a\u90e8\u7f72 Nginx\u3001uWSGI \u548c Django\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-onBsIhWDCY\">\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\u3001pip \u548c\u865b\u64ec\u74b0\u5883\u5de5\u5177\uff1a<\/p>\n<pre><code>sudo yum install python3 python3-pip python3-venv -y<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-django-onBsIhWDCY\">\u5b89\u88dd Django<\/h2>\n<p>\u9996\u5148\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684\u865b\u64ec\u74b0\u5883\u4f86\u5b89\u88dd Django\uff1a<\/p>\n<pre><code>mkdir ~\/myproject\ncd ~\/myproject\npython3 -m venv venv\nsource venv\/bin\/activate<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u4f7f\u7528 pip \u5b89\u88dd Django\uff1a<\/p>\n<pre><code>pip install django<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684 Django \u9805\u76ee\uff1a<\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-uwsgi-onBsIhWDCY\">\u5b89\u88dd uWSGI<\/h2>\n<p>\u5728\u865b\u64ec\u74b0\u5883\u4e2d\u5b89\u88dd uWSGI\uff1a<\/p>\n<pre><code>pip install uwsgi<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b uWSGI \u914d\u7f6e\u6587\u4ef6\uff0c\u547d\u540d\u70ba <code>myproject.ini<\/code>\uff1a<\/p>\n<pre><code>[uwsgi]\nchdir = \/home\/yourusername\/myproject\/myproject\nmodule = myproject.wsgi:application\nhome = \/home\/yourusername\/myproject\/venv\nmaster = true\nprocesses = 5\nsocket = myproject.sock\nchmod-socket = 660\nvacuum = true\ndie-on-term = true<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-onBsIhWDCY\">\u5b89\u88dd Nginx<\/h2>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Nginx\uff1a<\/p>\n<pre><code>sudo yum install epel-release -y\nsudo yum install nginx -y<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u555f\u52d5 Nginx \u4e26\u8a2d\u7f6e\u70ba\u958b\u6a5f\u81ea\u555f\uff1a<\/p>\n<pre><code>sudo systemctl start nginx\nsudo systemctl enable nginx<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-onBsIhWDCY\">\u914d\u7f6e Nginx<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684 Nginx \u914d\u7f6e\u6587\u4ef6\uff0c\u4f4d\u65bc <code>\/etc\/nginx\/conf.d\/myproject.conf<\/code>\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;  # \u66ff\u63db\u70ba\u4f60\u7684\u57df\u540d\n\n    location = \/favicon.ico { access_log off; log_not_found off; }\n    location \/static\/ {\n        root \/home\/yourusername\/myproject;\n    }\n\n    location \/ {\n        include uwsgi_params;\n        uwsgi_pass unix:\/home\/yourusername\/myproject\/myproject.sock;\n    }\n}<\/code><\/pre>\n<p>\u6aa2\u67e5 Nginx \u914d\u7f6e\u662f\u5426\u6b63\u78ba\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u6709\u932f\u8aa4\uff0c\u91cd\u65b0\u52a0\u8f09 Nginx\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx<\/code><\/pre>\n<h2 id=\"%e5%95%9f%e5%8b%95-uwsgi-onBsIhWDCY\">\u555f\u52d5 uWSGI<\/h2>\n<p>\u6700\u5f8c\uff0c\u555f\u52d5 uWSGI \u670d\u52d9\uff1a<\/p>\n<pre><code>uwsgi --ini myproject.ini<\/code><\/pre>\n<p>\u70ba\u4e86\u8b93 uWSGI \u5728\u7cfb\u7d71\u555f\u52d5\u6642\u81ea\u52d5\u904b\u884c\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 systemd \u5275\u5efa\u4e00\u500b\u670d\u52d9\u55ae\u5143\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-onBsIhWDCY\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u6210\u529f\u5730\u5728 CentOS \u4e0a\u90e8\u7f72\u4e86 Nginx\u3001uWSGI \u548c Django\u3002\u9019\u7a2e\u7d44\u5408\u4e0d\u50c5\u80fd\u5920\u63d0\u4f9b\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u9084\u80fd\u5920\u8f15\u9b06\u5730\u64f4\u5c55\u61c9\u7528\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to deploy Nginx with uWSGI and Django on CentOS for a robust web application setup. Step-by-step guide included.<\/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-44046","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44046","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=44046"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44046\/revisions"}],"predecessor-version":[{"id":44047,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/44046\/revisions\/44047"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=44046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=44046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=44046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}