{"id":1543,"date":"2025-11-02T12:56:22","date_gmt":"2025-11-02T04:56:22","guid":{"rendered":"https:\/\/www.chinaenow.com\/blog\/?p=1543"},"modified":"2025-11-02T12:56:22","modified_gmt":"2025-11-02T04:56:22","slug":"%e5%a4%a7%e5%b9%b3%e5%8f%b0%ef%bc%9a%e5%a6%82%e4%bd%95%e9%83%a8%e7%bd%b2%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84web%e5%ba%94%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.chinaenow.com\/blog\/1543.html","title":{"rendered":"\u5927\u5e73\u53f0\uff1a\u5982\u4f55\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\uff1f"},"content":{"rendered":"<p><p>\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u5728\u5927\u5e73\u53f0\u4e0a\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\u3002\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684Flask\u6846\u67b6\u6765\u521b\u5efa\u5e94\u7528\uff0c\u5e76\u4f7f\u7528Nginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b9e\u64cd\u6b65\u9aa4\uff0c\u60a8\u5c06\u5b66\u4f1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u53ef\u8bbf\u95ee\u7684Web\u670d\u52a1\u3002<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.chinaenow.com\/blog\/wp-content\/uploads\/2025\/10\/MzCECy29H.jpg\" alt=\"\u5927\u5e73\u53f0\uff1a\u5982\u4f55\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\uff1f\" title=\"\u5927\u5e73\u53f0\uff1a\u5982\u4f55\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\uff1f\"><\/p>\n<\/p>\n<p><h2>\u64cd\u4f5c\u524d\u7684\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u6709\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e00\u53f0\u914d\u7f6e\u4e86Linux\u64cd\u4f5c\u7cfb\u7edf\u7684\u670d\u52a1\u5668\u6216VPS\u3002<\/li>\n<li>Python\u73af\u5883\u5df2\u5b89\u88c5\u3002<\/li>\n<li>Git\u5ba2\u6237\u7aef\u5df2\u5b89\u88c5\u3002<\/li>\n<li>\u8bbf\u95ee\u670d\u52a1\u5668\u6216VPS\u7684SSH\u6743\u9650\u3002<\/li>\n<\/ul>\n<p><h2>\u5b89\u88c5Flask<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u670d\u52a1\u5668\u4e0a\u5b89\u88c5Flask\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u6b65\u9aa41\uff1a\u521b\u5efa\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><pre><code>python3 -m venv venv<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a`venv`\u7684\u865a\u62df\u73af\u5883\u3002<\/p>\n<\/p>\n<p><h3>\u6b65\u9aa42\uff1a\u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u6839\u636e\u60a8\u7684Linux\u53d1\u884c\u7248\uff0c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\u7684\u547d\u4ee4\u53ef\u80fd\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code>source venv\/bin\/activate   \u5bf9\u4e8e\u57fa\u4e8eBash\u7684shell<\/p>\r\n<p>source venv\/Scripts\/activate   \u5bf9\u4e8eWindows\u7684PowerShell<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa43\uff1a\u5b89\u88c5Flask<\/h3>\n<\/p>\n<p><pre><code>pip install Flask<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199Flask\u5e94\u7528\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u521b\u5efaFlask\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982`app.py`\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code>from flask import Flask<\/p>\r\n\r\n<p>app = Flask(__name__)<\/p>\r\n\r\n<p>@app.route('\/')<\/p>\r\n<p>def hello():<\/p>\r\n<p>    return 'Hello, World!'<\/p>\r\n\r\n<p>if __name__ == '__main__':<\/p>\r\n<p>    app.run(host='0.0.0.0', port=8080)<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6761\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Flask\u5e94\u7528\uff0c\u5f53\u8bbf\u95ee\u6839\u76ee\u5f55\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u201cHello, World!\u201d\u3002<\/p>\n<\/p>\n<p><h2>\u914d\u7f6eNginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6eNginx\u6765\u4ee3\u7406Flask\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u6b65\u9aa41\uff1a\u5b89\u88c5Nginx<\/h3>\n<\/p>\n<p><pre><code>yum install nginx   \u5bf9\u4e8e\u57fa\u4e8eYum\u7684Linux\u53d1\u884c\u7248<\/p>\r\n<p>apt-get install nginx   \u5bf9\u4e8e\u57fa\u4e8eDebian\u7684Linux\u53d1\u884c\u7248<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6b65\u9aa42\uff1a\u521b\u5efaNginx\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code>sudo nano \/etc\/nginx\/sites-available\/myapp<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>server {<\/p>\r\n<p>    listen 80;<\/p>\r\n<p>    server_name yourdomain.com;<\/p>\r\n\r\n<p>    location \/ {<\/p>\r\n<p>        proxy_pass http:\/\/localhost:8080;<\/p>\r\n<p>        proxy_set_header Host $host;<\/p>\r\n<p>        proxy_set_header X-Real-IP $remote_addr;<\/p>\r\n<p>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\r\n<p>        proxy_set_header X-Forwarded-Proto $scheme;<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u5c06`yourdomain.com`\u66ff\u6362\u4e3a\u60a8\u7684\u57df\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u6b65\u9aa43\uff1a\u542f\u7528\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code>sudo ln -s \/etc\/nginx\/sites-available\/myapp \/etc\/nginx\/sites-enabled\/<\/p>\r\n<p>sudo nginx -t   \u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6<\/p>\r\n<p>sudo systemctl reload nginx   \u91cd\u65b0\u52a0\u8f7dNginx<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u6d4b\u8bd5\u60a8\u7684Web\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u60a8\u7684\u57df\u540d\uff0c\u60a8\u5e94\u8be5\u770b\u5230\u201cHello, World!\u201d\u7684\u6d88\u606f\u3002\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u60a8\u7684Flask\u5e94\u7528\u5df2\u7ecf\u6210\u529f\u90e8\u7f72\u5728\u5927\u5e73\u53f0\u4e0a\u4e86\u3002<\/p>\n<\/p>\n<p><h2>\u6ce8\u610f\u4e8b\u9879\u548c\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u60a8\u7684\u670d\u52a1\u5668\u9632\u706b\u5899\u5141\u8bb8HTTP\u548cHTTPS\u6d41\u91cf\u3002<\/li>\n<li>\u5982\u679c\u60a8\u7684\u670d\u52a1\u5668\u4f7f\u7528SSL\u8bc1\u4e66\uff0c\u8bf7\u786e\u4fddNginx\u914d\u7f6e\u6b63\u786e\u3002<\/li>\n<li>\u5728\u90e8\u7f72\u751f\u4ea7\u73af\u5883\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u7684\u5e94\u7528\u7ecf\u8fc7\u4e86\u5145\u5206\u7684\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u5728\u5927\u5e73\u53f0\u4e0a\u90e8\u7f72\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\u3002\u6211\u4eec\u5c06\u4f7f\u7528Python\u7684Flask\u6846\u67b6\u6765\u521b\u5efa\u5e94\u7528\uff0c\u5e76\u4f7f\u7528Nginx\u4f5c\u4e3a\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u3002\u901a\u8fc7\u672c\u6587\u7684\u5b9e\u64cd\u6b65\u9aa4\uff0c\u60a8\u5c06\u5b66\u4f1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u53ef\u8bbf\u95ee\u7684Web\u670d\u52a1\u3002 \u64cd\u4f5c\u524d\u7684\u51c6\u5907 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u60a8\u6709&#8230;<\/p>\n","protected":false},"author":2,"featured_media":1544,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[345,300,344],"topic":[],"class_list":["post-1543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-usdt","tag-flask","tag-web","tag-344"],"_links":{"self":[{"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/posts\/1543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/comments?post=1543"}],"version-history":[{"count":1,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"predecessor-version":[{"id":1546,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/posts\/1543\/revisions\/1546"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/media\/1544"}],"wp:attachment":[{"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/tags?post=1543"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.chinaenow.com\/blog\/wp-json\/wp\/v2\/topic?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}