登入 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
- Create an account on Heroku
- Download and install Heroku CLI
- Install gunicorn with $ pip install gunicorn. Make sure you’re using pip from your virtual environment if you have one.
- 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.
- 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.
- Create a runtime.txt file in the main app directory. (See Specifying a Python version)
- Open your command line to point to the directory where the Python file containing your app code is located.
- Using the terminal, log in to Heroku with $ heroku login and enter email address and password.
- Create a new Heroku app with $ heroku create myawesomeappname
- Initialize a local git repository with $ git init
- Add your local application files to git with $ git add
- Commit the changes with $ git commit -m “first commit". Make sure “first commit" is inside quotes.
- Before pushing the changes to Heroku, tell heroku the name of the app you want to use with $ heroku git:remote –app myawesomeappname
- Push the changes to Heroku with $ git push heroku master
- open your app with $ heroku open.