{"id":441,"date":"2019-07-01T10:20:18","date_gmt":"2019-07-01T02:20:18","guid":{"rendered":"http:\/\/www.91tfboys.com\/?p=441"},"modified":"2022-07-18T14:20:44","modified_gmt":"2022-07-18T06:20:44","slug":"flask%e3%80%81python3%e3%80%81apache2-4%e3%80%81ubuntu-%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/www.91tfboys.com\/?p=441","title":{"rendered":"Flask\u3001Python3\u3001Apache2.4\u3001Ubuntu \u914d\u7f6e"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8fd1\u65e5\u5c06\u4e00\u4e9b\u4ee3\u7801\u653e\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u9042\u7528Flask+Python3+Apache2.4+Ubuntu\u7ec4\u5408\u6765\u8fdb\u884c\u914d\u7f6e\u3002\u5177\u4f53\u5982\u4e0b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"400\" src=\"http:\/\/www.91tfboys.com\/wp-content\/uploads\/2019\/07\/flask-python.png\" alt=\"\" class=\"wp-image-442\"\/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Flask\uff08Python\uff09<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u82e5\u4f7f\u7528\u865a\u62df\u73af\u5883\uff0c\u53ef\u5728\u9884\u8bbe\u7684 Web \u76ee\u5f55\u4e0b\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5efa\u7acb\u865a\u62df\u73af\u5883\uff0c\u5e76\u6fc0\u6d3b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 -m venv venvname\nsource venvname\/bin\/activate<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5b89\u88c5\u7a0b\u5e8f\u8fd0\u884c\u6240\u9700\u8981\u7684 Python \u5305\uff08\u5982 flask \u7b49\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install flask\n# pip install -r requirements.txt<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0e\u4e3b\u7a0b\u5e8f\u540c\u76ee\u5f55\u4e0b\u5efa\u7acb wsgi \u6587\u4ef6\uff08\u5982 flask.wsgi\uff09\uff0c\u5199\u5165\u4ee5\u4e0b\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nactivate = \"Your_Web_Root\/venvname\/bin\/activate_this.py\"\ndef execfile(filename):\n\tglobals = dict(__file__=filename)\n\texec(open(filename).read(), globals)\nexecfile(activate)\nsys.path.insert(0, \"Your_Web_Root\")\nfrom YourApp import app\napplication = app<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4e0d\u4f7f\u7528 venv\uff0c\u53ef\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys \r\nsys.path.insert(0, 'Your_Web_Root')\r\nfrom YourApp import app as application<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c venvname\/bin\/ \u76ee\u5f55\u4e0b\u6ca1\u6709 activate_this.py\uff0c\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2a\uff0c\u5185\u5bb9\u5982\u4e0b\uff08\u82e5\u4e0d\u9002\u7528 venv\uff0c\u53ef\u4e0d\u7528\u521b\u5efa\u672c\u6587\u4ef6\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"By using execfile(this_file, dict(__file__=this_file)) you will\nactivate this virtualenv environment.\n\nThis can be used when you must use an existing Python interpreter, not\nthe virtualenv bin\/python\n\"\"\"\n\ntry:\n    __file__\nexcept NameError:\n    raise AssertionError(\n        \"You must run this like execfile('path\/to\/activate_this.py', dict(__file__='path\/to\/activate_this.py'))\")\nimport sys\nimport os\n\nold_os_path = os.environ&#91;'PATH']\nos.environ&#91;'PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path\nbase = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))\nif sys.platform == 'win32':\n    site_packages = os.path.join(base, 'Lib', 'site-packages')\nelse:\n    site_packages = os.path.join(base, 'lib', 'python%s' % sys.version&#91;:3], 'site-packages')\nprev_sys_path = list(sys.path)\nimport site\nsite.addsitedir(site_packages)\nsys.real_prefix = sys.prefix\nsys.prefix = base\n# Move the added items to the front of the path:\nnew_sys_path = &#91;]\nfor item in list(sys.path):\n    if item not in prev_sys_path:\n        new_sys_path.append(item)\n        sys.path.remove(item)\nsys.path&#91;:0] = new_sys_path<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Flask \u91cc\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e9b\u9519\u8bef\u5904\u7406\u9875\u9762\uff0c\u793a\u4f8b\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@app.errorhandler(404)\ndef page_not_found():\n    return \"&lt;h1&gt;404&lt;\/h1&gt;\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Apache<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u5b8c Apache \u540e\uff0c\u9700\u8981\u5f15\u7528 WSGI \u6a21\u5757\u3002\u6b64\u5904\u4f7f\u7528\u7684\u662f Python 3\uff0c\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo apt-get install libapache2-mod-wsgi-py3<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Apache \u7684\u914d\u7f6e\u6587\u4ef6 \/etc\/apache2\/sites-available\/000-default.conf \u91cc\u6dfb\u52a0\u914d\u7f6e\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;VirtualHost *:80>\n\tServerName Your.Server.Name\n\tServerAdmin YourMail@Server.Name\n\tDocumentRoot Your_Web_Root\n\t&lt;Directory Your_Web_Root>\n\t\tOptions Indexes FollowSymlinks\n\t\tAllowOverride All\n\t\tWSGIScriptReloading On\n\t&lt;\/Directory>\n\tWSGIScriptAlias \/ Your_Web_Root\/flask.wsgi\n&lt;\/VirtualHost><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u542f Apache \u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u670d\u52a1\u6ca1\u6709\u8fd0\u884c\u8d77\u6765\uff0c\u53ef\u4ee5\u5230 Apache \u7684 error.log \u91cc\u67e5\u770b\u539f\u56e0\u3002\u901a\u5e38\u53ef\u80fd\u51fa\u9519\u7684\u95ee\u9898\u5305\u62ec Python \u7a0b\u5e8f\u95ee\u9898\u3001<strong>Web \u76ee\u5f55\u6743\u9650\u95ee\u9898<\/strong>\u3001\u7ec4\u4ef6\u7248\u672c\u4e0d\u5bf9\u7b49\uff0c\u53ef\u6839\u636e\u9519\u8bef\u7c7b\u578b\u8fdb\u4e00\u6b65\u8c03\u6574\u7a0b\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u65e5\u5c06\u4e00\u4e9b\u4ee3\u7801\u653e\u5728\u670d\u52a1\u5668\u4e0a\u8fd0\u884c\uff0c\u9042\u7528Flask+Python3+Apache2.4+Ubuntu\u7ec4\u5408\u6765\u8fdb\u884c\u914d\u7f6e\u3002\u5177\u4f53\u5982\u4e0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":442,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[24,25,18],"class_list":["post-441","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding","tag-apache","tag-flask","tag-python"],"_links":{"self":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=441"}],"version-history":[{"count":3,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":655,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/posts\/441\/revisions\/655"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=\/wp\/v2\/media\/442"}],"wp:attachment":[{"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.91tfboys.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}