{"id":52315,"date":"2024-10-09T01:36:14","date_gmt":"2024-10-08T17:36:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/52315\/"},"modified":"2024-11-14T10:39:27","modified_gmt":"2024-11-14T02:39:27","slug":"ubuntu-%e4%b8%8b%e9%80%9a%e9%81%8e-nginx-%e9%83%a8%e7%bd%b2-django","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/52315\/","title":{"rendered":"Ubuntu \u4e0b\u901a\u904e Nginx \u90e8\u7f72 Django"},"content":{"rendered":"<h1 id=\"ubuntu-%e4%b8%8b%e9%80%9a%e9%81%8e-nginx-%e9%83%a8%e7%bd%b2-django-oFYBrTGfnr\">Ubuntu \u4e0b\u901a\u904e Nginx \u90e8\u7f72 Django<\/h1>\n<p>Django \u662f\u4e00\u500b\u9ad8\u6548\u80fd\u7684 Python \u7db2\u9801\u6846\u67b6\uff0c\u5ee3\u6cdb\u7528\u65bc\u958b\u767c\u5404\u7a2e web \u61c9\u7528\u7a0b\u5f0f\u3002\u5c07 Django \u90e8\u7f72\u5728 Ubuntu \u7cfb\u7d71\u4e0a\uff0c\u4e26\u4f7f\u7528 Nginx \u4f5c\u70ba\u53cd\u5411\u4ee3\u7406\u4f3a\u670d\u5668\uff0c\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u548c\u5b89\u5168\u6027\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 Ubuntu \u74b0\u5883\u4e2d\u901a\u904e Nginx \u90e8\u7f72 Django \u61c9\u7528\u3002<\/p>\n<h2 id=\"%e5%89%8d%e6%9c%9f%e6%ba%96%e5%82%99-oFYBrTGfnr\">\u524d\u671f\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u8acb\u78ba\u4fdd\u60a8\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u8edf\u9ad4\uff1a<\/p>\n<ul>\n<li>Ubuntu \u4f3a\u670d\u5668<\/li>\n<li>Python 3<\/li>\n<li>Pip<\/li>\n<li>Virtualenv<\/li>\n<li>Nginx<\/li>\n<\/ul>\n<p>\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Nginx\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e8%a3%9d-django-oFYBrTGfnr\">\u5b89\u88dd Django<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5275\u5efa\u4e00\u500b\u865b\u64ec\u74b0\u5883\u4f86\u5b89\u88dd Django\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u8207\u7cfb\u7d71\u5176\u4ed6 Python \u5305\u7684\u885d\u7a81\u3002<\/p>\n<pre><code>sudo apt install python3-venv\nmkdir myproject\ncd myproject\npython3 -m venv venv\nsource venv\/bin\/activate\npip install django<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u65b0\u7684 Django \u5c08\u6848\uff1a<\/p>\n<pre><code>django-admin startproject myproject<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-django-oFYBrTGfnr\">\u914d\u7f6e Django<\/h2>\n<p>\u5728 Django \u5c08\u6848\u4e2d\uff0c\u60a8\u9700\u8981\u4fee\u6539 `settings.py` \u6587\u4ef6\uff0c\u4ee5\u4fbf\u5141\u8a31\u4f86\u81ea Nginx \u7684\u8acb\u6c42\u3002\u627e\u5230 `ALLOWED_HOSTS` \u4e26\u6dfb\u52a0\u60a8\u7684\u57df\u540d\u6216 IP \u5730\u5740\uff1a<\/p>\n<pre><code>ALLOWED_HOSTS = ['your_domain.com', 'your_server_ip']<\/code><\/pre>\n<h2 id=\"%e9%81%8b%e8%a1%8c-django-%e9%96%8b%e7%99%bc%e4%bc%ba%e6%9c%8d%e5%99%a8-oFYBrTGfnr\">\u904b\u884c Django \u958b\u767c\u4f3a\u670d\u5668<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Django \u81ea\u5e36\u7684\u958b\u767c\u4f3a\u670d\u5668\u4f86\u6e2c\u8a66\u61c9\u7528\uff1a<\/p>\n<pre><code>python manage.py runserver 0.0.0.0:8000<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u60a8\u7684 Django \u61c9\u7528\u5c31\u53ef\u4ee5\u901a\u904e `http:\/\/your_server_ip:8000` \u8a2a\u554f\u4e86\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-gunicorn-%e4%bd%9c%e7%82%ba-wsgi-%e4%bc%ba%e6%9c%8d%e5%99%a8-oFYBrTGfnr\">\u4f7f\u7528 Gunicorn \u4f5c\u70ba WSGI \u4f3a\u670d\u5668<\/h2>\n<p>\u70ba\u4e86\u5728\u751f\u7522\u74b0\u5883\u4e2d\u904b\u884c Django\uff0c\u5efa\u8b70\u4f7f\u7528 Gunicorn \u4f5c\u70ba WSGI \u4f3a\u670d\u5668\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88dd Gunicorn\uff1a<\/p>\n<pre><code>pip install gunicorn<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 Gunicorn\uff1a<\/p>\n<pre><code>gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-oFYBrTGfnr\">\u914d\u7f6e Nginx<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u914d\u7f6e Nginx \u4ee5\u53cd\u5411\u4ee3\u7406\u5230 Gunicorn\u3002\u5275\u5efa\u4e00\u500b\u65b0\u7684 Nginx \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>sudo nano \/etc\/nginx\/sites-available\/myproject<\/code><\/pre>\n<p>\u5728\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com your_server_ip;\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 proxy_params;\n        proxy_pass http:\/\/127.0.0.1:8000;\n    }\n}<\/code><\/pre>\n<p>\u4fdd\u5b58\u4e26\u9000\u51fa\u7de8\u8f2f\u5668\uff0c\u7136\u5f8c\u555f\u7528\u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/myproject \/etc\/nginx\/sites-enabled<\/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\u555f\u52d5 Nginx\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-oFYBrTGfnr\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5df2\u7d93\u6210\u529f\u5728 Ubuntu \u4e0a\u901a\u904e Nginx \u90e8\u7f72\u4e86 Django \u61c9\u7528\u3002\u9019\u7a2e\u914d\u7f6e\u4e0d\u50c5\u63d0\u9ad8\u4e86\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u589e\u5f37\u4e86\u5b89\u5168\u6027\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u5c0b\u627e\u5408\u9069\u7684\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4f86\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Ubuntu \u4e0a\u901a\u904e Nginx \u90e8\u7f72 Django \u61c9\u7528\uff0c\u5be6\u73fe\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u7684\u7db2\u9801\u670d\u52d9\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-52315","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52315","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=52315"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52315\/revisions"}],"predecessor-version":[{"id":52316,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/52315\/revisions\/52316"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=52315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=52315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=52315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}