{"id":2353,"date":"2012-12-09T10:03:43","date_gmt":"2012-12-09T02:03:43","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2353"},"modified":"2012-12-09T10:03:43","modified_gmt":"2012-12-09T02:03:43","slug":"%e5%9c%a8apache%e4%b8%8a%e9%83%a8%e7%bd%b2flask%e6%a1%86%e6%9e%b6%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2353.html","title":{"rendered":"\u5728apache\u4e0a\u90e8\u7f72flask\u6846\u67b6\u7a0b\u5e8f"},"content":{"rendered":"<p><span style=\"font-size: small;\">flask\u662fpython\u7684\u4e00\u4e2aweb\u5f00\u53d1\u6846\u67b6\uff0c\u76f8\u6bd4django\u6765\u8bf4\uff0cflask\u663e\u5f97\u66f4\u52a0\u8f7b\u91cf\u7ea7\uff0c\u4e5f\u6ca1\u6709ORM\u5b9e\u73b0\uff0c\u4e0a\u624b\u5f88\u5feb\u3002\u603b\u4e4b\uff0c\u7528flask\u9650\u5236\u5f88\u5c0f\uff0c\u4f60\u53ef\u4ee5\u81ea\u7531\u53d1\u6325\u5176\u5b83\u7ec4\u4ef6\uff0c\u6bd4\u5982\u6570\u636e\u5e93\uff0cdjango\u539f\u751f\u4e0d\u652f\u6301mongodb\u7684\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5b89\u88c5flask\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install python-flask\r\nsudo apt-get install python-virtualenv\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5efa\u7acb\u4e00\u4e2ahello world\u9879\u76ee\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ncd ~\/www\/\r\nmkdir flaskdemo\r\ncd flaskdemo\r\nvirtualenv venv\r\npip install Flask\r\nvim hello.py\r\n<\/pre>\n<p><span style=\"font-size: small;\">hello.py\u5185\u5bb9\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello():\r\n    return 'hello world'\r\n\r\n@app.route('\/user\/&lt;username&gt;')\r\ndef show_user_profile(username):\r\n    return 'User %s ' % username\r\n\r\n@app.route('\/post\/&lt;int:post_id&gt;')\r\ndef show_post(post_id):\r\n    return 'Post id %d' % post_id\r\n\r\n<\/pre>\n<p><span style=\"font-size: small;\">apache\u4e0b\u5b89\u88c5mod_wsgi\u6a21\u5757\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\napt-get install libapache2-mod-wsgi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u914d\u7f6eapache\uff0c\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u5165\u4e0b\u9762\u914d\u7f6e\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n    WSGIDaemonProcess flaskdemo user=chenming group=sudo threads=5\r\n    WSGIScriptAlias \/flaskdemo \/home\/chenming\/www\/flaskdemo\/hello.wsgi\r\n\r\n    &lt;Directory \/home\/chenming\/www\/flaskdemo&gt;\r\n        WSGIProcessGroup flaskdemo\r\n        WSGIApplicationGroup %{GLOBAL}\r\n        Order deny,allow\r\n        Allow from all\r\n    &lt;\/Directory&gt;\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5176\u4e2d\/home\/chenming\/www\/flaskdemo\/hello.wsgi\u6587\u4ef6\u5185\u5bb9\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport sys\r\nsys.path.append(&quot;\/home\/chenming\/www\/flaskdemo\/&quot;)\r\nfrom hello import app as application\r\n#import app\u5c31\u662f\u6307\u7684app = Flask(__name__)\uff0c\u53c2\u8003http:\/\/stackoverflow.com\/questions\/9680073\/how-do-i-use-flask-routes-with-apache-and-mod-wsgi\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u542f\u52a8apache\uff0c\u7136\u540e\u8bbf\u95ee http:\/\/127.0.0.1\/flaskdemo\/ \u548c http:\/\/127.0.0.1\/flaskdemo\/user\/xxx<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>flask\u662fpython\u7684\u4e00\u4e2aweb\u5f00\u53d1\u6846\u67b6\uff0c\u76f8\u6bd4django\u6765\u8bf4\uff0cflask\u663e\u5f97\u66f4\u52a0\u8f7b\u91cf\u7ea7\uff0c\u4e5f\u6ca1\u6709ORM\u5b9e\u73b0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2353","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2353"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2353\/revisions"}],"predecessor-version":[{"id":2354,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2353\/revisions\/2354"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}