Heroku deployment

登入 heroku

$ heroku login

heroku: Press any key to open up the browser to login or q to exit:

檢視目前已存在的 apps

$ heroku apps

=== xxxx@gmail.com Apps
app1

新增一個名為 mysite 的 app

$ heroku create mysite

Creating ⬢ mysite... !
!    Name mysite is already taken

(顯示此名稱已被使用)
(以下為假設名稱app2沒有被使用的情況)

$ heroku create app2 

Creating ⬢ app2... done
https://app2.herokuapp.com/ | https://git.heroku.com/app2.git


新增 requirements.txt

查看虛擬環境已安裝的 python 套件

$ ..\virtual\Scripts\pip freeze

Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.5

安裝 gunicorn

heroku 需要 gunicorn (http server) 來運行 web app

$ ..\virtual\Scripts\pip install gunicorn

Collecting gunicorn
  Using cached https://files.pythonhosted.org/packages/8c/da/b8dd8deb741bff556db53902d4706774c8e1e67265f69528c14c003644e6/gunicorn-19.9.0-py2.py3-none-any.whl
Installing collected packages: gunicorn
Successfully installed gunicorn-19.9.0  

再次確認已安裝套件

..\virtual\Scripts\pip freeze

Click==7.0
Flask==1.1.1
gunicorn==19.9.0
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
Werkzeug==0.15.5

將 output 存為 requirements.txt

$ ..\virtual\Scripts\pip freeze > requirements.txt

使用 git 進行 push

$ git init
$ git add .
$ git commit -m "first commit"

-m: 後面接這次commit的message

指定要 push 到哪個 app

$ heroku git:remote --app app2
set git remote heroku to https://git.heroku.com/app2.git \

Push 到 heroku

$ git push heroku master

開啟 app

$ heroku open

其他

若有 Error,可查看最後的 Logs

$ heroku logs --tail

查看目前 app 的 info

$ heroku info
=== app2
Auto Cert Mgmt: false
Dynos:
Git URL:        https://git.heroku.com/app2.git
Owner:          xxxx@gmail.com
Region:         us
Repo Size:      5 KB
Slug Size:      45 MB
Stack:          heroku-18
Web URL:        https://app2.herokuapp.com/

更改 app 名

$ heroku apps:rename app2-1

Renaming app2 to app2-1... done
https://app2-1.herokuapp.com/ | https://git.heroku.com/app2-1.git
Git remote heroku updated
!    Don't forget to update git remotes for all other local checkouts of the app.

再次檢查是否更名成功

$ heroku info

=== app2-1
Auto Cert Mgmt: false
Dynos:          web: 1
Git URL:        https://git.heroku.com/app2-1.git
Owner:          xxxx@gmail.com
Region:         us
Repo Size:      6 KB
Slug Size:      45 MB
Stack:          heroku-18
Web URL:        https://app2-1.herokuapp.com/

Reference

Steps to deploy a static Flask website to Heroku

  1. Create an account on Heroku
  2. Download and install Heroku CLI
  3. Install gunicorn with $ pip install gunicorn. Make sure you’re using pip from your virtual environment if you have one.
  4. Create a requirement.txt file in the main app directory where the main Python app file is located. You can create that file by running $ pip freeze > requirements.txt in the command line. The requirement.txt file should now contain a list of Python packages.
  5. Create a file named Procfile in the main app directory. The file should not contain any extension. Then type in this line inside: web: gunicorn script1:app where “script1″ should be replaced with the name of your Python script and “app" with the name of the variable holding your Flask app.
  6. Create a runtime.txt file in the main app directory. (See Specifying a Python version)
  7. Open your command line to point to the directory where the Python file containing your app code is located.
  8. Using the terminal, log in to Heroku with $ heroku login and enter email address and password.
  9. Create a new Heroku app with $ heroku create myawesomeappname
  10. Initialize a local git repository with $ git init
  11. Add your local application files to git with $ git add
  12. Commit the changes with $ git commit -m “first commit". Make sure “first commit" is inside quotes.
  13. Before pushing the changes to Heroku, tell heroku the name of the app you want to use with $ heroku git:remote –app myawesomeappname
  14. Push the changes to Heroku with $ git push heroku master
  15. open your app with $ heroku open.

 

廣告

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s