{"id":49808,"date":"2024-10-08T07:40:16","date_gmt":"2024-10-07T23:40:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/49808\/"},"modified":"2024-10-08T07:40:16","modified_gmt":"2024-10-07T23:40:16","slug":"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%a8%ad%e7%bd%ae-uwsgi-%e5%bf%ab%e5%8f%96%e4%bb%a5%e9%80%b2%e8%a1%8c-python-%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/49808\/","title":{"rendered":"Nginx \u6280\u5de7\uff1a\u8a2d\u7f6e Uwsgi \u5feb\u53d6\u4ee5\u9032\u884c Python \u61c9\u7528"},"content":{"rendered":"<h1 id=\"nginx-%e6%8a%80%e5%b7%a7%ef%bc%9a%e8%a8%ad%e7%bd%ae-uwsgi-%e5%bf%ab%e5%8f%96%e4%bb%a5%e9%80%b2%e8%a1%8c-python-%e6%87%89%e7%94%a8-hiwiVVJjMW\">Nginx \u6280\u5de7\uff1a\u8a2d\u7f6e Uwsgi \u5feb\u53d6\u4ee5\u9032\u884c Python \u61c9\u7528<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u6bcf\u500b\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u6240\u8ffd\u6c42\u7684\u76ee\u6a19\u3002\u5c0d\u65bc\u4f7f\u7528 Python \u958b\u767c\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cNginx \u548c uWSGI \u7684\u7d44\u5408\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u8a2d\u7f6e uWSGI \u5feb\u53d6\uff0c\u4ee5\u63d0\u9ad8 Python \u61c9\u7528\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-uwsgi%ef%bc%9f-hiwiVVJjMW\">\u4ec0\u9ebc\u662f uWSGI\uff1f<\/h2>\n<p>uWSGI \u662f\u4e00\u500b\u61c9\u7528\u4f3a\u670d\u5668\uff0c\u5c08\u70ba\u904b\u884c Python \u61c9\u7528\u800c\u8a2d\u8a08\u3002\u5b83\u652f\u6301 WSGI\uff08Web Server Gateway Interface\uff09\u5354\u8b70\uff0c\u4e26\u80fd\u5920\u8207 Nginx \u7b49\u7db2\u9801\u4f3a\u670d\u5668\u9032\u884c\u9ad8\u6548\u7684\u901a\u4fe1\u3002uWSGI \u7684\u4e00\u500b\u91cd\u8981\u7279\u6027\u662f\u5176\u9748\u6d3b\u7684\u914d\u7f6e\u9078\u9805\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u9069\u61c9\u4e0d\u540c\u7684\u61c9\u7528\u9700\u6c42\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e5%bf%ab%e5%8f%96%ef%bc%9f-hiwiVVJjMW\">\u70ba\u4ec0\u9ebc\u9700\u8981\u5feb\u53d6\uff1f<\/h2>\n<p>\u5feb\u53d6\u662f\u4e00\u7a2e\u63d0\u9ad8\u61c9\u7528\u6027\u80fd\u7684\u6280\u8853\uff0c\u901a\u904e\u5b58\u5132\u5e38\u7528\u6578\u64da\u4ee5\u6e1b\u5c11\u91cd\u8907\u8a08\u7b97\u548c\u6578\u64da\u5eab\u67e5\u8a62\u7684\u6b21\u6578\u3002\u5c0d\u65bc Python \u61c9\u7528\uff0c\u5feb\u53d6\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u97ff\u61c9\u6642\u9593\uff0c\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u91cf\u8acb\u6c42\u6642\u3002<\/p>\n<h2 id=\"%e8%a8%ad%e7%bd%ae-nginx-%e5%92%8c-uwsgi-%e5%bf%ab%e5%8f%96-hiwiVVJjMW\">\u8a2d\u7f6e Nginx \u548c uWSGI \u5feb\u53d6<\/h2>\n<p>\u4ee5\u4e0b\u662f\u8a2d\u7f6e Nginx \u548c uWSGI \u5feb\u53d6\u7684\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d-nginx-%e5%92%8c-uwsgi-hiwiVVJjMW\">1. \u5b89\u88dd Nginx \u548c uWSGI<\/h3>\n<pre><code>sudo apt update\nsudo apt install nginx\nsudo apt install uwsgi uwsgi-plugin-python3<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae-uwsgi-hiwiVVJjMW\">2. \u914d\u7f6e uWSGI<\/h3>\n<p>\u9996\u5148\uff0c\u5275\u5efa\u4e00\u500b uWSGI \u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982 <code>\/etc\/uwsgi\/apps-available\/myapp.ini<\/code>\uff1a<\/p>\n<pre><code>[uwsgi]\nmodule = wsgi:application\nmaster = true\nprocesses = 5\nsocket = \/tmp\/myapp.sock\nchmod-socket = 660\nvacuum = true\ndie-on-term = true<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u555f\u7528\u8a72\u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo ln -s \/etc\/uwsgi\/apps-available\/myapp.ini \/etc\/uwsgi\/apps-enabled\/<\/code><\/pre>\n<h3 id=\"3-%e9%85%8d%e7%bd%ae-nginx-hiwiVVJjMW\">3. \u914d\u7f6e Nginx<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5275\u5efa\u4e00\u500b Nginx \u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982 <code>\/etc\/nginx\/sites-available\/myapp<\/code>\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name your_domain.com;\n\n    location \/ {\n        include uwsgi_params;\n        uwsgi_pass unix:\/tmp\/myapp.sock;\n        uwsgi_cache my_cache;\n        uwsgi_cache_key $uri;\n        uwsgi_cache_valid 200 1h;\n    }\n\n    location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {\n        expires 30d;\n        access_log off;\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u8a2d\u7f6e\u4e86\u5feb\u53d6\uff0c\u4e26\u6307\u5b9a\u4e86\u5feb\u53d6\u7684\u6709\u6548\u6642\u9593\u3002\u63a5\u4e0b\u4f86\uff0c\u555f\u7528\u8a72\u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/<\/code><\/pre>\n<h3 id=\"4-%e5%89%b5%e5%bb%ba%e5%bf%ab%e5%8f%96%e5%8d%80%e5%9f%9f-hiwiVVJjMW\">4. \u5275\u5efa\u5feb\u53d6\u5340\u57df<\/h3>\n<p>\u5728 Nginx \u914d\u7f6e\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u5feb\u53d6\u5340\u57df\u3002\u5728 <code>\/etc\/nginx\/nginx.conf<\/code> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>http {\n    ...\n    uwsgi_cache_path \/tmp\/uwsgi_cache levels=1:2 keys_zone=my_cache:10m inactive=60m;\n    ...\n}<\/code><\/pre>\n<h3 id=\"5-%e9%87%8d%e5%95%9f%e6%9c%8d%e5%8b%99-hiwiVVJjMW\">5. \u91cd\u555f\u670d\u52d9<\/h3>\n<p>\u6700\u5f8c\uff0c\u91cd\u555f Nginx \u548c uWSGI \u670d\u52d9\u4ee5\u61c9\u7528\u66f4\u6539\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx\nsudo systemctl restart uwsgi<\/code><\/pre>\n<h2 id=\"%e6%b8%ac%e8%a9%a6%e5%bf%ab%e5%8f%96%e6%95%88%e6%9e%9c-hiwiVVJjMW\">\u6e2c\u8a66\u5feb\u53d6\u6548\u679c<\/h2>\n<p>\u5b8c\u6210\u4e0a\u8ff0\u6b65\u9a5f\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982 <code>curl<\/code> \u6216\u700f\u89bd\u5668\u4f86\u6e2c\u8a66\u61c9\u7528\u7684\u97ff\u61c9\u6642\u9593\u3002\u901a\u904e\u89c0\u5bdf\u5feb\u53d6\u7684\u547d\u4e2d\u7387\uff0c\u60a8\u53ef\u4ee5\u8a55\u4f30\u5feb\u53d6\u8a2d\u7f6e\u7684\u6709\u6548\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-hiwiVVJjMW\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u8a2d\u7f6e Nginx \u548c uWSGI \u5feb\u53d6\uff0c\u60a8\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8 Python \u61c9\u7528\u7684\u6027\u80fd\u3002\u9019\u4e0d\u50c5\u80fd\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\uff0c\u9084\u80fd\u63d0\u5347\u7528\u6236\u9ad4\u9a57\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\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Nginx \u4e2d\u8a2d\u7f6e uWSGI \u5feb\u53d6\uff0c\u4ee5\u512a\u5316 Python \u61c9\u7528\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\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-49808","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49808","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=49808"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49808\/revisions"}],"predecessor-version":[{"id":49809,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/49808\/revisions\/49809"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=49808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=49808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=49808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}