{"id":2896,"date":"2025-03-18T00:10:35","date_gmt":"2025-03-17T16:10:35","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2896"},"modified":"2025-03-18T00:10:35","modified_gmt":"2025-03-17T16:10:35","slug":"python-uwsgi-%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/18\/python-uwsgi-%e5%ae%89%e8%a3%85%e4%b8%8e%e9%85%8d%e7%bd%ae%e6%8c%87%e5%8d%97\/","title":{"rendered":"Python uWSGI \u5b89\u88c5\u4e0e\u914d\u7f6e\u6307\u5357"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. uWSGI \u7b80\u4ecb<\/h2>\n\n\n\n<p>uWSGI \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u670d\u52a1\u5668\uff0c\u652f\u6301 WSGI\u3001FastCGI\u3001uWSGI \u534f\u8bae\uff0c\u5e38\u7528\u4e8e Python Web \u5e94\u7528\u7684\u90e8\u7f72\uff0c\u5982 Flask \u548c Django\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u5b89\u88c5 uWSGI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u4f7f\u7528 pip \u5b89\u88c5\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install uwsgi<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u4ece\u6e90\u7801\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/unbit\/uwsgi.git\ncd uwsgi\npython setup.py install<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 Flask \u5e94\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from flask import Flask\napp = Flask(__name__)\n\n@app.route('\/')\ndef hello():\n    return \"Hello, uWSGI!\"\n\nif __name__ == '__main__':\n    app.run()<\/code><\/pre>\n\n\n\n<p>\u4fdd\u5b58\u4e3a <code>app.py<\/code>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4f7f\u7528 uWSGI \u8fd0\u884c Flask \u5e94\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>uwsgi --http :5000 --wsgi-file app.py --callable app<\/code><\/pre>\n\n\n\n<p>\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--http :5000<\/code> \u76d1\u542c 5000 \u7aef\u53e3<\/li>\n\n\n\n<li><code>--wsgi-file app.py<\/code> \u6307\u5b9a WSGI \u6587\u4ef6<\/li>\n\n\n\n<li><code>--callable app<\/code> \u6307\u5b9a\u5e94\u7528\u5b9e\u4f8b<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u914d\u7f6e uWSGI<\/h2>\n\n\n\n<p>\u521b\u5efa <code>uwsgi.ini<\/code> \u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;uwsgi]\nmodule = app:app\nmaster = true\nprocesses = 4\nsocket = uwsgi.sock\nchmod-socket = 660\nvacuum = true\ndaemonize = uwsgi.log<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uwsgi --ini uwsgi.ini<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u914d\u5408 Nginx \u8fd0\u884c uWSGI<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">6.1 \u5b89\u88c5 Nginx<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install nginx<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6.2 \u914d\u7f6e Nginx<\/h3>\n\n\n\n<p>\u7f16\u8f91 <code>\/etc\/nginx\/sites-available\/default<\/code>\uff0c\u6dfb\u52a0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        include uwsgi_params;\n        uwsgi_pass unix:\/path\/to\/uwsgi.sock;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u91cd\u542f Nginx\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart nginx<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u5e38\u89c1\u95ee\u9898\u4e0e\u4f18\u5316<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">7.1 \u542f\u52a8\u5931\u8d25\u95ee\u9898<\/h3>\n\n\n\n<p>\u68c0\u67e5\u65e5\u5fd7 <code>uwsgi.log<\/code>\uff0c\u5e38\u89c1\u9519\u8bef\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ModuleNotFoundError<\/code>: \u786e\u4fdd Python \u865a\u62df\u73af\u5883\u5df2\u6fc0\u6d3b<\/li>\n\n\n\n<li><code>Permission denied<\/code>: \u786e\u4fdd sock \u6587\u4ef6\u6709\u6b63\u786e\u7684\u6743\u9650<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7.2 \u6027\u80fd\u4f18\u5316\u5efa\u8bae<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u589e\u52a0 <code>processes<\/code> \u548c <code>threads<\/code> \u4ee5\u5145\u5206\u5229\u7528\u591a\u6838 CPU<\/li>\n\n\n\n<li>\u542f\u7528 <code>lazy-apps = true<\/code> \u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>cache2<\/code> \u63d0\u4f9b\u5185\u5b58\u7f13\u5b58<\/li>\n<\/ul>\n\n\n\n<p><strong>\u53c2\u8003\u94fe\u63a5<\/strong><\/p>\n\n\n\n<p>Nginx \u5b98\u65b9\u6587\u6863:\u00a0<a>https:\/\/nginx.org\/en\/docs\/<\/a><\/p>\n\n\n\n<p>uWSGI \u5b98\u65b9\u6587\u6863:\u00a0<a href=\"https:\/\/uwsgi-docs.readthedocs.io\/\">https:\/\/uwsgi-docs.readthedocs.io<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. uWSGI \u7b80\u4ecb uWSGI \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u670d\u52a1\u5668\uff0c\u652f\u6301 WSGI\u3001FastCGI\u3001uWSGI \u534f\u8bae [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2897,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2896","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2896"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2896\/revisions"}],"predecessor-version":[{"id":2898,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2896\/revisions\/2898"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2897"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}