利用heroku搭建服务的方法

1. 在heroku网站上注册账号

https://www.heroku.com/

要使用gmail邮箱进行注册

2. 安装heroku CLI

https://devcenter.heroku.com/articles/getting-started-with-python#set-up

3. 用终端登陆heroku

$ heroku login 

输入邮箱和密码


4. 在heroku上创建一个app的仓库

下面的命令都是在git bash中执行

这里会随机生成一个app的名称(floating-sands-17593),后面可以更改app的名称

$ heroku create 
Creating app... done, floating-sands-17593
https://floating-sands-17593.herokuapp.com/ | https://git.heroku.com/floating-sands-17593.git

如果之前有自己在GitHub上有自己的server的仓库,并且想把自己的仓库放到heroku上面运行,那么可以执行下面命令在自己的仓库下,与heroku仓库进行关联,之后git remote -v命令可以看出时关联上两个仓库,heroku和origin仓库:

$ heroku git:remote --app floating-sands-17593
set git remote heroku to https://git.heroku.com/floating-sands-17593.git

$ git remote -v
heroku  https://git.heroku.com/floating-sands-17593.git (fetch)
heroku  https://git.heroku.com/floating-sands-17593.git (push)
origin  [email protected]:xxxxxxx/myserver.git (fetch)
origin  [email protected]:xxxxxxx/myserver.git (push)

之后,当我们提交代码到自己的origin仓库时,可以执行下面的命令,将修改也提交的本地的heroku仓库的master分支:

$ git push origin HEAD:heroku/master
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/xxxxxxx/myserver
   30b7661..3f8b4e2  HEAD -> heroku/master

再执行下面的命令,将本地heroku仓库mater分支的代码提交到自己远程的heroku仓库master分支 

$ git push heroku master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 1.68 KiB | 1.68 MiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote:  !     The latest version of Python 3.6 is python-3.6.6 (you are using python-3.6.0, which is unsupported).
remote:  !     We recommend upgrading by specifying the latest version (python-3.6.6).
remote:        Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing python-3.6.0
remote: -----> Installing pip
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote:        Collecting requests>=2.12 (from -r /tmp/build_6c9b905b83df6633ae3bb4c4f833da04/requirements.txt (line 1))
remote:          Downloading https://files.pythonhosted.org/packages/65/47/7e02164a2a3db50ed6d8a6ab1d6d60b69c4c3fdf57a284257925dfc12bda/requests-2.19.1-py2.py3-none-any.whl (91kB)
remote:        Collecting certifi>=2017.4.17 (from requests>=2.12->-r /tmp/build_6c9b905b83df6633ae3bb4c4f833da04/requirements.txt (line 1))
remote:          Downloading https://files.pythonhosted.org/packages/df/f7/04fee6ac349e915b82171f8e23cee63644d83663b34c539f7a09aed18f9e/certifi-2018.8.24-py2.py3-none-any.whl (147kB)
remote:        Collecting urllib3<1.24,>=1.21.1 (from requests>=2.12->-r /tmp/build_6c9b905b83df6633ae3bb4c4f833da04/requirements.txt (line 1))
remote:          Downloading https://files.pythonhosted.org/packages/bd/c9/6fdd990019071a4a32a5e7cb78a1d92c53851ef4f56f62a3486e6a7d8ffb/urllib3-1.23-py2.py3-none-any.whl (133kB)
remote:        Collecting idna<2.8,>=2.5 (from requests>=2.12->-r /tmp/build_6c9b905b83df6633ae3bb4c4f833da04/requirements.txt (line 1))
remote:          Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)
remote:        Collecting chardet<3.1.0,>=3.0.2 (from requests>=2.12->-r /tmp/build_6c9b905b83df6633ae3bb4c4f833da04/requirements.txt (line 1))
remote:          Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
remote:        Installing collected packages: certifi, urllib3, idna, chardet, requests
remote:        Successfully installed certifi-2018.8.24 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 55.3M
remote: -----> Launching...
remote:        Released v3
remote:        https://floating-sands-17593.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/floating-sands-17593.git
 * [new branch]      master -> master

当然可以直接将代码提交的heroku仓库,可以自行实验。

 

5. 执行heroku open命令打开部署好的网站。

6. 配置数据库,下面以postgresql为例

https://elements.heroku.com/addons/heroku-postgresql  点击install,将数据库添加到自己的应用中

postrgre中的最低级版本hobby-dev,支持少于 1 万条记录、连接数 20 、没有缓存 、不允许fork/follow、且没有 Postgres日志,无需付费的版本,可以先从这个版本上手熟悉。

也可以采用命令的方式,将hobby-dev版本的postgre数据库添加到自己的应用(appname) :

heroku addons:add heroku-postgresql:hobby-dev --app  appname 

添加数据库生效后,会产生一个该数据库的url(DATABASE_URL),可以通过下面的命令进行查看:

heroku config -s  --app appname

查看数据库信息的命令:

heroku pg:info --app appname

与数据库建立会话命令:

heroku pg:psql --app appname, 建立连接后,可以输入sql语句

C:\Users\llz>heroku pg:psql --app floating-sands-17593
--> Connecting to postgresql-closed-48647
psql (10.5)
SSL 连接(协议:TLSv1.2,密码:ECDHE-RSA-AES256-GCM-SHA384,密钥位:256,压缩:关闭)
floating-sands-17593::DATABASE=>
floating-sands-17593::DATABASE=> CREATE TABLE posts ( content TEXT,
floating-sands-17593::DATABASE(>                      time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
floating-sands-17593::DATABASE(>                      id SERIAL );
CREATE TABLE
floating-sands-17593::DATABASE=>
floating-sands-17593::DATABASE=> insert into posts values('abc');
INSERT 0 1
floating-sands-17593::DATABASE=> commit;
WARNING:  there is no transaction in progress
COMMIT
floating-sands-17593::DATABASE=> select * from posts;
 content |            time            | id
---------+----------------------------+----
 abc     | 2018-09-16 16:21:36.222737 |  1
(1 行记录)

window下部署postgre数据库说明

https://devcenter.heroku.com/articles/heroku-postgresql#set-up-postgres-on-windows

7. 有了数据库之后,下面就该编程来调用这个数据库了,下面以python为例

pipenv install psycopg2-binary  

之后在python代码中,添加:

import os
import psycopg2

DATABASE_URL = os.environ['DATABASE_URL']

conn = psycopg2.connect(DATABASE_URL, sslmode='require')

除了代码之外,还有三个比较重要的文件,这三个文件需要放在仓库中:

(1)requirements.txt   存放python依赖的模块,具体格式如下所示:

requests>=2.12
Flask>=0.12.2
psycopg2>=2.7.5
bleach>1.5.0

(2)runtime.txt   运行环境配置

python-3.6.0

(3)Procfile   需要执行的py文件

web: python BookmarkServer.py

 

 

调试方法

heroku logs --tail  查看服务器端的日志

常见错误日志

1.  R10 Boot timeout

2018-09-16T14:44:19.344061+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-09-16T14:44:19.344061+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-09-16T14:44:19.470277+00:00 heroku[web.1]: Process exited with status 137
2018-09-16T14:44:19.488826+00:00 heroku[web.1]: State changed from starting to crashed
2018-09-16T14:44:22.212756+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=floating-sands-17593.herokuapp.com request_id=072c4c2c-2e86-4123-844b-d03bb5f00e36 fwd="223.72.44.189" dyno= connect= service= status=503 bytes= protocol=https

https://devcenter.heroku.com/articles/error-codes#r10-boot-timeout

 

 

 

你可能感兴趣的:(heroku,安装和配置)