{"id":185,"date":"2026-06-17T19:44:26","date_gmt":"2026-06-17T11:44:26","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=185"},"modified":"2026-06-17T19:44:26","modified_gmt":"2026-06-17T11:44:26","slug":"flask-%e9%83%a8%e7%bd%b2","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/06\/17\/flask-%e9%83%a8%e7%bd%b2\/","title":{"rendered":"Flask \u90e8\u7f72"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5c06 Flask \u5e94\u7528\u4ece\u672c\u5730\u5f00\u53d1\u73af\u5883\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\uff0c\u662f\u9879\u76ee\u4e0a\u7ebf\u7684\u6700\u540e\u4e00\u6b65\uff0c\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u4e00\u6b65\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5f00\u53d1\u65f6\uff0c\u6211\u4eec\u4f7f\u7528 <code>app.run(debug=True)<\/code> \u542f\u52a8\u670d\u52a1\u5668\u3002\u4f46 <strong>Flask \u5185\u7f6e\u7684\u5f00\u53d1\u670d\u52a1\u5668\uff08Werkzeug\uff09\u7edd\u5bf9\u4e0d\u80fd\u7528\u4e8e\u751f\u4ea7\u73af\u5883<\/strong>\uff0c\u56e0\u4e3a\u5b83\u6027\u80fd\u4f4e\u4e0b\u3001\u4e0d\u652f\u6301\u9ad8\u5e76\u53d1\uff0c\u4e14\u5b58\u5728\u4e25\u91cd\u7684\u5b89\u5168\u9690\u60a3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u6807\u51c6\u7684 Flask \u90e8\u7f72\u67b6\u6784\u901a\u5e38\u662f\uff1a<strong>Nginx (Web \u670d\u52a1\u5668) + Gunicorn (WSGI \u670d\u52a1\u5668) + Flask (\u5e94\u7528)<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfd7\ufe0f 1. \u751f\u4ea7\u73af\u5883\u67b6\u6784\u89e3\u6790<\/h3>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">\u5ba2\u6237\u7aef (\u6d4f\u89c8\u5668\/App)\n      \u2193 (HTTP\/HTTPS \u8bf7\u6c42)\n[Nginx] (Web \u670d\u52a1\u5668)\n  \u251c\u2500 \u5904\u7406\u9759\u6001\u6587\u4ef6 (CSS\/JS\/\u56fe\u7247) -&gt; \u76f4\u63a5\u8fd4\u56de\uff0c\u4e0d\u7ecf\u8fc7 Python\n  \u251c\u2500 \u5904\u7406 SSL\/TLS (HTTPS \u8bc1\u4e66)\n  \u2514\u2500 \u53cd\u5411\u4ee3\u7406\u52a8\u6001\u8bf7\u6c42 -&gt; \u8f6c\u53d1\u7ed9 Gunicorn\n      \u2193\n[Gunicorn] (WSGI \u670d\u52a1\u5668)\n  \u251c\u2500 \u7ba1\u7406\u591a\u4e2a Python \u8fdb\u7a0b\/Worker\n  \u2514\u2500 \u5c06\u8bf7\u6c42\u5206\u53d1\u7ed9 Flask \u5e94\u7528\n      \u2193\n[Flask App] (\u4f60\u7684 Python \u4ee3\u7801)<\/code><\/pre><\/div><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Nginx<\/strong>\uff1a\u8d1f\u8d23\u6321\u5728\u6700\u524d\u9762\uff0c\u5904\u7406\u6162\u8bf7\u6c42\u3001\u9759\u6001\u6587\u4ef6\u3001HTTPS\uff0c\u5e76\u628a\u9700\u8981 Python \u5904\u7406\u7684\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u540e\u7aef\u3002<\/li>\n\n\n\n<li><strong>Gunicorn<\/strong>\uff1a\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 Python WSGI HTTP \u670d\u52a1\u5668\uff0c\u8d1f\u8d23\u8fd0\u884c\u4f60\u7684 Flask \u4ee3\u7801\uff0c\u5904\u7406\u5e76\u53d1\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udee0\ufe0f 2. \u90e8\u7f72\u5b9e\u6218\u6b65\u9aa4 (Linux \u73af\u5883)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u4f60\u6709\u4e00\u53f0 Ubuntu \u670d\u52a1\u5668\uff0c\u9879\u76ee\u4ee3\u7801\u5df2\u7ecf\u901a\u8fc7 Git \u62c9\u53d6\u5230\u4e86 <code>\/var\/www\/myflaskapp<\/code> \u76ee\u5f55\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b2c\u4e00\u6b65\uff1a\u51c6\u5907 Python \u73af\u5883<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u670d\u52a1\u5668\u4e0a\u521b\u5efa\u865a\u62df\u73af\u5883\u5e76\u5b89\u88c5\u4f9d\u8d56\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">cd \/var\/www\/myflaskapp\n\n# \u521b\u5efa\u5e76\u6fc0\u6d3b\u865a\u62df\u73af\u5883\npython3 -m venv venv\nsource venv\/bin\/activate\n\n# \u5b89\u88c5\u9879\u76ee\u4f9d\u8d56\npip install -r requirements.txt\n\n# \u5b89\u88c5 Gunicorn\npip install gunicorn<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b2c\u4e8c\u6b65\uff1a\u6d4b\u8bd5 Gunicorn \u542f\u52a8<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd0\u884c\u4e4b\u524d\uff0c\u5148\u786e\u4fdd Gunicorn \u80fd\u6b63\u5e38\u542f\u52a8\u4f60\u7684\u5e94\u7528\u3002<br>\u5047\u8bbe\u4f60\u7684\u5165\u53e3\u6587\u4ef6\u662f <code>run.py<\/code>\uff0c\u91cc\u9762\u6709\u4e00\u4e2a <code>app<\/code> \u5b9e\u4f8b\uff08\u6216\u8005\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f <code>create_app()<\/code>\uff09\u3002<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\"># \u57fa\u7840\u542f\u52a8\u547d\u4ee4 (\u7ed1\u5b9a\u5230\u672c\u5730 8000 \u7aef\u53e3\uff0c\u542f\u52a8 3 \u4e2a worker \u8fdb\u7a0b)\ngunicorn --bind 127.0.0.1:8000 --workers 3 run:app\n\n# \u5982\u679c\u662f\u5e94\u7528\u5de5\u5382\u6a21\u5f0f (create_app)\ngunicorn --bind 127.0.0.1:8000 --workers 3 &quot;run:create_app()&quot;<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>(\u6ce8\uff1a<code>workers<\/code> \u6570\u91cf\u901a\u5e38\u8bbe\u7f6e\u4e3a <code>(2 x CPU\u6838\u5fc3\u6570) + 1<\/code>)<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u770b\u5230\u7c7b\u4f3c <code>Listening at: http:\/\/127.0.0.1:8000<\/code> \u7684\u8f93\u51fa\uff0c\u8bf4\u660e Gunicorn \u914d\u7f6e\u6210\u529f\u3002\u6309 <code>Ctrl+C<\/code> \u505c\u6b62\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b2c\u4e09\u6b65\uff1a\u914d\u7f6e Systemd (\u8fdb\u7a0b\u5b88\u62a4)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u8ba9 Gunicorn \u5728\u540e\u53f0\u8fd0\u884c\uff0c\u5e76\u4e14\u5f00\u673a\u81ea\u542f\u3001\u5d29\u6e83\u81ea\u52a8\u91cd\u542f\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a Systemd \u670d\u52a1\u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u6587\u4ef6\uff1a<code>sudo nano \/etc\/systemd\/system\/myflaskapp.service<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u586b\u5165\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><span>25<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">[Unit]\nDescription=Gunicorn instance to serve myflaskapp\nAfter=network.target\n\n[Service]\n# \u8fd0\u884c\u8be5\u670d\u52a1\u7684\u7cfb\u7edf\u7528\u6237 (\u5efa\u8bae\u4e0d\u8981\u7528 root)\nUser=www-data\nGroup=www-data\n\n# \u9879\u76ee\u76ee\u5f55\nWorkingDirectory=\/var\/www\/myflaskapp\n# \u865a\u62df\u73af\u5883\u8def\u5f84\nEnvironment=&quot;PATH=\/var\/www\/myflaskapp\/venv\/bin&quot;\n# \u73af\u5883\u53d8\u91cf\u6587\u4ef6 (\u5b58\u653e SECRET_KEY, \u6570\u636e\u5e93\u5bc6\u7801\u7b49)\nEnvironmentFile=\/var\/www\/myflaskapp\/.env\n\n# \u542f\u52a8\u547d\u4ee4\nExecStart=\/var\/www\/myflaskapp\/venv\/bin\/gunicorn --workers 3 --bind unix:myflaskapp.sock -m 007 run:app\n\n# \u8fdb\u7a0b\u7ba1\u7406\nRestart=always\nRestartSec=3\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><em>\u6ce8\u610f\uff1a\u8fd9\u91cc\u6211\u4eec\u5c06 Gunicorn \u7ed1\u5b9a\u5230\u4e86 <code>unix:myflaskapp.sock<\/code> (Unix \u5957\u63a5\u5b57)\uff0c\u8fd9\u6bd4 TCP \u7aef\u53e3\u901a\u4fe1\u6027\u80fd\u66f4\u9ad8\uff0c\u4e14\u4ec5\u9650\u4e8e\u672c\u673a\u8bbf\u95ee\uff0c\u66f4\u5b89\u5168\u3002<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u542f\u52a8\u5e76\u542f\u7528\u670d\u52a1\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">sudo systemctl start myflaskapp\nsudo systemctl enable myflaskapp\nsudo systemctl status myflaskapp  # \u68c0\u67e5\u72b6\u6001<\/code><\/pre><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u7b2c\u56db\u6b65\uff1a\u914d\u7f6e Nginx \u53cd\u5411\u4ee3\u7406<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5 Nginx\uff1a<code>sudo apt install nginx<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa Nginx \u914d\u7f6e\u6587\u4ef6\uff1a<code>sudo nano \/etc\/nginx\/sites-available\/myflaskapp<\/code><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><span>17<\/span><span>18<\/span><span>19<\/span><span>20<\/span><span>21<\/span><span>22<\/span><span>23<\/span><span>24<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">server {\n    listen 80;\n    server_name your_domain.com; # \u66ff\u6362\u4e3a\u4f60\u7684\u57df\u540d\u6216\u670d\u52a1\u5668 IP\n\n    # 1. \u5904\u7406\u9759\u6001\u6587\u4ef6 (\u6781\u5927\u51cf\u8f7b Python \u8d1f\u62c5)\n    location \/static\/ {\n        alias \/var\/www\/myflaskapp\/app\/static\/; # \u6307\u5411\u4f60\u7684 static \u76ee\u5f55\n        expires 30d; # \u5f00\u542f\u6d4f\u89c8\u5668\u7f13\u5b58\n        add_header Cache-Control &quot;public, immutable&quot;;\n    }\n\n    # 2. \u5904\u7406\u52a8\u6001\u8bf7\u6c42 (\u53cd\u5411\u4ee3\u7406\u7ed9 Gunicorn)\n    location \/ {\n        include proxy_params;\n        # \u6307\u5411 Gunicorn \u521b\u5efa\u7684 sock \u6587\u4ef6\n        proxy_pass http:\/\/unix:\/var\/www\/myflaskapp\/myflaskapp.sock;\n\n        # \u4f20\u9012\u771f\u5b9e\u7684\u5ba2\u6237\u7aef IP \u7ed9 Flask\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u542f\u7528\u914d\u7f6e\u5e76\u91cd\u542f Nginx\uff1a<\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">sudo ln -s \/etc\/nginx\/sites-available\/myflaskapp \/etc\/nginx\/sites-enabled\nsudo nginx -t  # \u6d4b\u8bd5\u914d\u7f6e\u8bed\u6cd5\nsudo systemctl restart nginx<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\uff0c\u8bbf\u95ee <code>http:\/\/your_domain.com<\/code>\uff0cNginx \u4f1a\u5c06\u8bf7\u6c42\u5b8c\u7f8e\u5730\u8f6c\u53d1\u7ed9\u4f60\u7684 Flask \u5e94\u7528\u4e86\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udc33 3. \u73b0\u4ee3\u66ff\u4ee3\u65b9\u6848\uff1aDocker \u5bb9\u5668\u5316<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b9e\u9645\u7684\u4f01\u4e1a\u7ea7\u5f00\u53d1\u4e2d\uff0c\u624b\u52a8\u914d\u7f6e Nginx \u548c Systemd \u6bd4\u8f83\u7e41\u7410\u3002\u73b0\u5728\u66f4\u4e3b\u6d41\u7684\u505a\u6cd5\u662f\u4f7f\u7528 <strong>Docker<\/strong> \u5c06\u5e94\u7528\u53ca\u5176\u8fd0\u884c\u73af\u5883\u6253\u5305\u6210\u955c\u50cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Dockerfile \u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<div class=\"chp-code-block chp-theme-github-dark chp-wrap-on\" data-chp-language=\"plaintext\" data-chp-line-start=\"1\" data-chp-highlight=\"\"><div class=\"chp-toolbar\"><div class=\"chp-toolbar-left\"><span class=\"chp-dots\"><span><\/span><span><\/span><span><\/span><\/span><span class=\"chp-language-label\">\u7eaf\u6587\u672c<\/span><\/div><div class=\"chp-toolbar-right\"><button type=\"button\" class=\"chp-copy-btn\" aria-label=\"\u590d\u5236\u4ee3\u7801\"><svg class=\"chp-icon-copy\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\"><path fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\/><\/svg><svg class=\"chp-icon-check\" viewBox=\"0 0 24 24\" width=\"14\" height=\"14\" aria-hidden=\"true\" style=\"display:none;\"><path fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\/><\/svg><span class=\"chp-copy-text\">\u590d\u5236<\/span><\/button><\/div><\/div><div class=\"chp-code-wrapper\"><div class=\"chp-line-numbers\" aria-hidden=\"true\"><span>1<\/span><span>2<\/span><span>3<\/span><span>4<\/span><span>5<\/span><span>6<\/span><span>7<\/span><span>8<\/span><span>9<\/span><span>10<\/span><span>11<\/span><span>12<\/span><span>13<\/span><span>14<\/span><span>15<\/span><span>16<\/span><\/div><pre class=\"chp-pre\" style=\"font-size:14px;\"><code class=\"language-plaintext\">FROM python:3.10-slim\n\nWORKDIR \/app\n\n# \u5b89\u88c5\u7cfb\u7edf\u4f9d\u8d56 (\u5982 Nginx, \u5982\u679c\u9700\u8981\u7684\u8bdd)\n# \u8fd9\u91cc\u6211\u4eec\u53ea\u6253\u5305 Python \u5e94\u7528\nCOPY requirements.txt .\nRUN pip install --no-cache-dir -r requirements.txt gunicorn\n\nCOPY . .\n\n# \u66b4\u9732\u7aef\u53e3\nEXPOSE 8000\n\n# \u542f\u52a8\u547d\u4ee4\nCMD [&quot;gunicorn&quot;, &quot;--bind&quot;, &quot;0.0.0.0:8000&quot;, &quot;--workers&quot;, &quot;3&quot;, &quot;run:app&quot;]<\/code><\/pre><\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u5408 <code>docker-compose.yml<\/code>\uff0c\u4f60\u53ef\u4ee5\u4e00\u952e\u62c9\u8d77 Flask\u3001Redis\u3001MySQL \u548c Nginx\uff0c\u5b9e\u73b0\u201c\u4e00\u6b21\u6784\u5efa\uff0c\u5230\u5904\u8fd0\u884c\u201d\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2601\ufe0f 4. \u5176\u4ed6\u90e8\u7f72\u65b9\u5f0f (PaaS)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u60f3\u7ba1\u7406\u670d\u52a1\u5668\uff08\u8fd0\u7ef4\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 PaaS\uff08\u5e73\u53f0\u5373\u670d\u52a1\uff09\u5e73\u53f0\uff0c\u53ea\u9700\u63a8\u9001\u4ee3\u7801\u5373\u53ef\u90e8\u7f72\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u56fd\u5185<\/strong>\uff1a\u963f\u91cc\u4e91 (\u8ba1\u7b97\u5de2\/\u4e91\u6548)\u3001\u817e\u8baf\u4e91 (Web \u5e94\u7528\u6258\u7ba1)\u3001\u534e\u4e3a\u4e91\u3002<\/li>\n\n\n\n<li><strong>\u56fd\u5916<\/strong>\uff1aRender, Heroku, Railway, Vercel (\u9002\u5408\u524d\u7aef+Serverless API)\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f 5. \u751f\u4ea7\u73af\u5883\u4e0a\u7ebf Checklist (\u907f\u5751\u6307\u5357)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u70b9\u51fb\u201c\u4e0a\u7ebf\u201d\u6309\u94ae\u524d\uff0c\u8bf7\u52a1\u5fc5\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5173\u95ed Debug \u6a21\u5f0f<\/strong>\uff1a\u786e\u4fdd <code>app.run(debug=False)<\/code> \u6216\u5728\u914d\u7f6e\u4e2d\u8bbe\u7f6e <code>DEBUG = False<\/code>\u3002Debug \u6a21\u5f0f\u4f1a\u66b4\u9732\u6e90\u7801\u548c\u5806\u6808\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>\u66f4\u6539 SECRET_KEY<\/strong>\uff1a\u7edd\u5bf9\u4e0d\u8981\u4f7f\u7528\u9ed8\u8ba4\u7684\u6216\u786c\u7f16\u7801\u7684 <code>SECRET_KEY<\/code>\u3002\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6ce8\u5165\u4e00\u4e2a\u590d\u6742\u7684\u968f\u673a\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u5e93\u5207\u6362<\/strong>\uff1a\u5c06 SQLite \u5207\u6362\u4e3a <strong>PostgreSQL<\/strong> \u6216 <strong>MySQL<\/strong>\u3002SQLite \u4e0d\u652f\u6301\u9ad8\u5e76\u53d1\u5199\u5165\u3002<\/li>\n\n\n\n<li><strong>\u73af\u5883\u53d8\u91cf\u7ba1\u7406<\/strong>\uff1a\u4f7f\u7528 <code>.env<\/code> \u6587\u4ef6\u6216\u4e91\u5e73\u53f0\u7684\u5bc6\u94a5\u7ba1\u7406\u670d\u52a1\u5b58\u653e\u6570\u636e\u5e93\u5bc6\u7801\u3001API Key \u7b49\u654f\u611f\u4fe1\u606f\uff0c<strong>\u7edd\u4e0d\u63d0\u4ea4\u5230 Git<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e HTTPS<\/strong>\uff1a\u4f7f\u7528 Let&#8217;s Encrypt (Certbot) \u4e3a\u4f60\u7684\u57df\u540d\u914d\u7f6e\u514d\u8d39\u7684 SSL \u8bc1\u4e66\uff0cNginx \u914d\u7f6e\u4e2d\u76d1\u542c 443 \u7aef\u53e3\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u6536\u96c6<\/strong>\uff1a\u914d\u7f6e Gunicorn \u7684 <code>accesslog<\/code> \u548c <code>errorlog<\/code>\uff0c\u6216\u8005\u4f7f\u7528 Python \u7684 <code>logging<\/code> \u6a21\u5757\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u6587\u4ef6\/ELK \u7cfb\u7edf\uff0c\u65b9\u4fbf\u6392\u67e5\u7ebf\u4e0a\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u8de8\u57df\u4e0e CORS<\/strong>\uff1a\u5982\u679c\u662f\u524d\u540e\u7aef\u5206\u79bb\uff0c\u786e\u4fdd\u6b63\u786e\u914d\u7f6e\u4e86 <code>Flask-CORS<\/code>\uff0c\u4e14\u53ea\u5141\u8bb8\u53d7\u4fe1\u4efb\u7684\u524d\u7aef\u57df\u540d\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udf89 \u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flask \u90e8\u7f72\u7684\u6838\u5fc3\u5728\u4e8e<strong>\u804c\u8d23\u5206\u79bb<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Nginx<\/strong> \u8d1f\u8d23\u201c\u810f\u6d3b\u7d2f\u6d3b\u201d\uff08\u9759\u6001\u6587\u4ef6\u3001HTTPS\u3001\u9632\u653b\u51fb\uff09\u3002<\/li>\n\n\n\n<li><strong>Gunicorn<\/strong> \u8d1f\u8d23\u201c\u5e76\u53d1\u8c03\u5ea6\u201d\uff08\u7ba1\u7406 Python \u8fdb\u7a0b\uff09\u3002<\/li>\n\n\n\n<li><strong>Flask<\/strong> \u4e13\u6ce8\u201c\u4e1a\u52a1\u903b\u8f91\u201d\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1\u4e86\u8fd9\u5957\u67b6\u6784\uff0c\u4f60\u7684 Flask \u5e94\u7528\u5c31\u5177\u5907\u4e86\u652f\u6491\u6570\u4e07\u65e5\u6d3b\u7528\u6237\u7684\u5de5\u4e1a\u7ea7\u80fd\u529b\u3002\u606d\u559c\u4f60\uff0c\u81f3\u6b64\u4f60\u5df2\u7ecf\u5b8c\u6574\u638c\u63e1\u4e86 Flask \u4ece\u5165\u95e8\u5230\u4e0a\u7ebf\u7684\u5168\u6d41\u7a0b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06 Flask \u5e94\u7528\u4ece\u672c\u5730\u5f00\u53d1\u73af\u5883\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\uff0c\u662f\u9879\u76ee\u4e0a\u7ebf&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-flask-"],"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/185\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}