{"id":463,"date":"2020-06-10T08:19:05","date_gmt":"2020-06-10T00:19:05","guid":{"rendered":"https:\/\/hophd.wordpress.com\/?p=463"},"modified":"2023-08-11T18:26:41","modified_gmt":"2023-08-11T10:26:41","slug":"django-heroku-deployment","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2020\/06\/10\/django-heroku-deployment\/","title":{"rendered":"Django\u7db2\u7ad9\u5728Heroku\u7684\u90e8\u7f72\u6307\u5357"},"content":{"rendered":"\n<p>\u50b3\u7d71\u4e0a\uff0c\u7576\u6211\u5011\u60f3\u5230\u8981\u67b6\u8a2d\u7db2\u7ad9\u6642\uff0c\u7b2c\u4e00\u500b\u60f3\u6cd5\u5c31\u662f\u8981\u5efa\u7f6e\u4e00\u53f0\u7db2\u8def\u4f3a\u670d\u5668\uff0c\u6216\u662f\u5230\u96f2\u7aef\u5e73\u53f0\u4e0a\u8cfc\u8cb7\u4e00\u53f0\u865b\u64ec\u4e3b\u6a5f\uff0c\u63a5\u8457\u5728\u4e3b\u6a5f\u4e0a\u5b89\u88ddApache\u3001MySQL\u7b49\u7b49\uff0c\u4e4b\u5f8c\u518d\u60f3\u8fa6\u6cd5\u628a\u81ea\u5df1\u7684\u7db2\u7ad9\u653e\u5728\u4e3b\u6a5f\u7684\u7a7a\u9593\u4e2d\u4ee5\u555f\u7528\u7db2\u7ad9\u7684\u529f\u80fd\u3002\u9019\u7a2e\u4ee5\u4e3b\u6a5f\u70ba\u74b0\u5883\u7684\u96f2\u7aef\u5e73\u53f0\uff0c\u6211\u5011\u628a\u5b83\u53eb\u505aIaaS\uff08Infrastructure as a Service\uff09\u3002\u5b83\u7684\u597d\u8655\u662f\uff0c\u4f60\u6709\u4e00\u53f0\u4e3b\u6a5f\u53ef\u4ee5\u81ea\u7531\u5730\u8a2d\u5b9a\u53ca\u5b89\u88dd\u6240\u6709\u9700\u8981\u7684\u4f3a\u670d\u8edf\u9ad4\uff0c\u4f46\u7f3a\u9ede\u5c31\u662f\uff0c\u4f60\u8981\u7ba1\u7406\u7684\u4e8b\u60c5\u975e\u5e38\u591a\u3002<\/p>\n\n\n\n<p>\u5728\u90e8\u7f72\u7db2\u7ad9\u6642\uff0c\u5176\u5be6\u9084\u6709\u4e00\u7a2e\u8df3\u904e\u5efa\u7acb\u4e3b\u6a5f\u7684\u65b9\u5f0f\uff0c\u800c\u662f\u628a\u81ea\u5df1\u7684\u7db2\u7ad9\u4ee5APP\u7684\u578b\u5f0f\u904b\u884c\u5728\u7279\u5b9a\u63d0\u4f9bPaaS\uff08Platform as a Service\uff09\u670d\u52d9\u7684\u4e3b\u6a5f\u5546\u4e0a\uff0c\u53ea\u8981\u9075\u5faa\u5b83\u5011\u7684\u4ecb\u9762\u5354\u5b9a\uff0c\u4f7f\u7528\u5b83\u5011\u652f\u63f4\u7684\u7a0b\u5f0f\u8a9e\u8a00\u88fd\u4f5c\u7684\u7db2\u7ad9\uff0c\u5747\u53ef\u4ee5\u63a8\u5230\u5b83\u5011\u7684\u4e3b\u6a5f\u8b8a\u6210\u7db2\u7ad9\uff0cHeroku\u4e00\u958b\u59cb\u5c31\u662f\u63d0\u4f9b\u6b64\u7a2e\u670d\u52d9\u7684\u4e3b\u6a5f\u5546\uff0c\u53e6\u5916\u4e00\u500b\u8457\u540d\u7684PaaS\u5247\u662fGoogle\u7684APP Engine\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u6211\u5011\u5c07\u793a\u7bc4\u7576\u4f60\u5df2\u7d93\u5efa\u7acb\u597d\u4e86\u4e00\u500bDjango\u7db2\u7ad9\u6642\uff0c\u628a\u5b83\u90e8\u7f72\u5230Heroku\u4e0a\u7684\u904e\u7a0b\u3002\u9996\u5148\uff0c\u4f60\u8981\u5148\u5230Heroku\uff08<a href=\"https:\/\/www.heroku.com\/\">https:\/\/www.heroku.com\/<\/a>\uff09\u4e0a\u8a3b\u518a\u4e00\u500b\u514d\u8cbb\u7684\u5e33\u865f\uff0c\u4ee5\u4e0b\u662f\u5b83\u5011\u7684\u5e33\u865f\u50f9\u683c\u6bd4\u8f03\u8868\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1201\" height=\"804\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2020\/06\/image.png?w=1024\" alt=\"\" class=\"wp-image-466\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image.png 1201w, https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image-300x201.png 300w, https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image-1024x686.png 1024w, https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image-768x514.png 768w\" sizes=\"auto, (max-width: 1201px) 100vw, 1201px\" \/><\/figure>\n\n\n\n<p>\u63a5\u8457\uff0c\u8981\u5728\u81ea\u5df1\u7684\u96fb\u8166\u5b89\u88ddGit\u4ee5\u53caHeroku CLI\uff08<a href=\"https:\/\/devcenter.heroku.com\/articles\/heroku-cli\">https:\/\/devcenter.heroku.com\/articles\/heroku-cli<\/a>\uff09\uff0c\u5b89\u88dd\u6a94\u7684\u4e0b\u8f09\u9801\u9762\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"540\" src=\"http:\/\/104es.nkust.org\/wp-content\/uploads\/2020\/06\/image-1.png?w=752\" alt=\"\" class=\"wp-image-467\" srcset=\"https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image-1.png 752w, https:\/\/104.es\/wp-content\/uploads\/2020\/06\/image-1-300x215.png 300w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><\/figure>\n\n\n\n<p>\u4ee5Windows\u70ba\u4f8b\uff0c\u5728\u5b89\u88dd\u5b8cCLI\u5de5\u5177\u4e4b\u5f8c\uff0c\u8acb\u9032\u5165\u547d\u4ee4\u63d0\u793a\u5b57\u5143\uff0c\u5148\u9032\u5165\u7db2\u7ad9\u7684\u865b\u64ec\u74b0\u5883\uff0c\u5728\u4f60\u7684Python\u865b\u64ec\u74b0\u5883\u4e2d\u9084\u9700\u8981\u5b89\u88dd\u4ee5\u4e0b\u90192\u500b\u6a21\u7d44\uff08\u6bcf\u4e00\u500b\u7db2\u7ad9\u53ea\u8981\u5b89\u88dd\u4e00\u6b21\u5c31\u53ef\u4ee5\u4e86\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install gunicorn\npip install django-heroku<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u4f7f\u7528\u4ee5\u4e0b\u7684\u6307\u4ee4\u5efa\u7acb\u6a21\u7d44\u6e05\u55ae\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip freeze &gt; requirements.txt<\/code><\/pre>\n\n\n\n<p>\u63a5\u8457\u5207\u63db\u5230\u7db2\u7ad9\u7684\u8cc7\u6599\u593e\u4e0b\uff0c\u6e96\u5099Procfile\u6a94\u6848\uff0c\u5176\u5167\u5bb9\u5982\u4e0b\uff0c\u5176\u4e2d\u90a3\u500bmyproject\u7684\u6587\u5b57\u8981\u6539\u6210\u4f60\u7684\u7db2\u7ad9\u7684\u540d\u7a31\u624d\u884c\uff0c\u800c\u7db2\u7ad9\u540d\u7a31\u662f\u4f60\u5728\u4f7f\u7528django-admin startproject\u4e4b\u5f8c\u5efa\u7acb\u7684\u90a3\u500b\u7db2\u7ad9\u6846\u67b6\u7684\u540d\u5b57\uff08\u5927\u90e8\u4efd\u7684\u4eba\u90fd\u4e0d\u662fmyproject\u5594\uff0c\u5982\u679c\u4f60\u662f\u4f7f\u7528\u672c\u7ad9\u4e4b\u524d\u7684\u4f8b\u5b50\u5efa\u7acb\u7684\u7db2\u7ad9\uff0c\u90a3\u61c9\u8a72\u662fdatacenter\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>web: gunicorn myproject.wsgi<\/code><\/pre>\n\n\n\n<p>\u9084\u6709\u5c0d\u65bcsettings.py\u7684\u4fee\u6539\uff0c\u5728import\u7684\u5730\u65b9\u52a0\u5165\u4ee5\u4e0b\u7684\u532f\u5165\u6307\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import django_heroku<\/code><\/pre>\n\n\n\n<p>\u5728settings.py\u7684\u6700\u5f8c\u4e00\u884c\u52a0\u4e0a\u5982\u4e0b\u7684\u6307\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')\nSTATIC_URL = '\/static\/'\ndjango_heroku.settings(locals())<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u6307\u4ee4\u7684\u524d\u5169\u884c\u662f\u70ba\u4e86\u8b93\u975c\u614b\u6a94\u6848\u80fd\u5920\u88ab\u9806\u5229\u7684\u8b80\u53d6\u800c\u52a0\u5165\u7684\uff0c\u4f60\u7684\u5c08\u6848\u4e2d\u4e5f\u8981\u6709\u76f8\u5c0d\u61c9\u7684static\u548cstaticfiles\u9019\u5169\u500b\u8cc7\u6599\u593e\u624d\u884c\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u662f\u4f7f\u7528\u6700\u65b0\u7248\u7684Django\u5efa\u7acb\u7684\u8a71\uff0c\u4f60\u7684settings.py\u7684\u6700\u524d\u9762\u53ef\u80fd\u6703\u6c92\u6709\u532f\u5165os\uff0c\u6240\u4ee5\u9084\u8981\u518d\u5230\u4f60\u7684\u6a94\u6848\u6700\u524d\u65b9\u52a0\u5165import os\u624d\u884c\u3002\u4e0d\u7136\u7684\u8a71\uff0c\u7b2c\u4e00\u884c\u52a0\u5165\u6307\u4ee4\u7684\u5730\u65b9\u53ef\u80fd\u8981\u4f7f\u7528\u53e6\u5916\u4e00\u7a2e\u6700\u65b0\u7684\u5beb\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STATIC_ROOT = BASE_DIR \/ 'staticfiles'<\/code><\/pre>\n\n\n\n<p>\u5728\u4ee5\u4e0a\u7684\u6240\u6709\u6a94\u6848\u53ca\u7a0b\u5f0f\u90fd\u4fee\u6539\u5b8c\u6210\u4e4b\u5f8c\uff0c\u57f7\u884c\u4ee5\u4e0b\u7684git\u6307\u4ee4\uff08\u5047\u8a2d\u4f60\u7684\u7db2\u7ad9\u90fd\u9084\u6c92\u6709\u4f7f\u7528Git\u7ba1\u7406\uff0c\u5982\u5df2\u5728\u4f7f\u7528Git\u7248\u672c\u63a7\u5236\u7684\u8a71\uff0c\u4ee5\u4e0b\u6307\u4ee4\u7684\u7b2c1\u884cgit init\u5c31\u4e0d\u80fd\u57f7\u884c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git init\ngit add . \ngit commit -m \"my first commit\"<\/code><\/pre>\n\n\n\n<p>\u7dca\u63a5\u8457\u5c31\u53ef\u4ee5\u9032\u5165\u5efa\u7acbHeroku\u7db2\u7ad9\u7a7a\u9593\u7684\u6b65\u9a5f\u4e86\uff0c\u5148\u9032\u884cHeroku\u7684\u767b\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku login<\/code><\/pre>\n\n\n\n<p>\u5efa\u7acb\u4e00\u500b\u7db2\u7ad9\u7a7a\u9593\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku create &lt;&lt;your project name&gt;&gt;<\/code><\/pre>\n\n\n\n<p>\u5efa\u7acb\u5b8c\u7562\u4e4b\u5f8c\uff0c\u628a\u81ea\u5df1\u5728Heroku\u7684\u7db2\u7ad9\u7a7a\u9593\u4f4d\u7f6e\u52a0\u5165\u5230git\u9060\u7aef\u5009\u5eab\u4f4d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku git:remote -a &lt;&lt;your project name&gt;&gt;<\/code><\/pre>\n\n\n\n<p>\u4e0b\u4e00\u884c\u5373\u70ba\u90e8\u7f72\u4e0a\u50b3\u7684\u6307\u4ee4\uff08\u5f8c\u9762\u7684master\u662f\u4f60\u7684\u5206\u652f\u540d\u7a31\uff0c\u53ef\u4ee5\u5229\u7528git branch\u67e5\u8a62\u5f97\u77e5\uff0c\u4f7f\u7528\u904egithub\u7684\u4eba\uff0c\u4e3b\u5206\u652f\u5927\u591a\u70bamain\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git push heroku master<\/code><\/pre>\n\n\n\n<p>\u4e0a\u50b3\u5b8c\u7562\u4e4b\u5f8c\uff0c\u5c31\u53ef\u4ee5\u5229\u7528&lt;&lt;your project name&gt;&gt;.herokuapp.com\u700f\u89bd\u4f60\u7684\u7db2\u7ad9\u4e86\u3002\u5982\u679c\u4f60\u7684\u7db2\u7ad9\u6709\u4f7f\u7528\u8cc7\u6599\u5eab\u7684\u8a71\uff0c\u5247\u9084\u9700\u8981\u57f7\u884c\u4ee5\u4e0b\u7684\u5e7e\u500b\u6307\u4ee4\u4ee5\u555f\u7528\u9060\u7aef\u8cc7\u6599\u5eab\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>heroku run python manage.py makemigrations\nheroku run python manage.py migrate\nheroku run python manage.py createsuperuser<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Heroku\u662f\u4e00\u500b\u652f\u63f4\u591a\u7a2e\u7a0b\u5f0f\u8a9e\u8a00\u7684\u96f2\u5e73\u53f0\u4f3a\u670d\u5668\u670d\u52d9\uff0c\u900f\u904e\u5e7e\u500b\u7c21\u55ae\u7684\u6b65\u9a5f\uff0c\u5c31\u53ef\u4ee5\u628a\u81ea\u5df1\u7684\u7db2\u7ad9\u6846\u67b6\u63a8\u5230\u5b83\u7684\u5e73\u53f0\uff0c\u6210\u70ba\u4e00\u500b\u904b\u884c\u5728Internet\u4e0a\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6,15],"tags":[34],"class_list":["post-463","post","type-post","status-publish","format-standard","hentry","category-django","category-python","tag-heroku"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=463"}],"version-history":[{"count":1,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/463\/revisions"}],"predecessor-version":[{"id":2944,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/463\/revisions\/2944"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}