heroku作为最开始的云服务平台,从开始的只支持Ruby到后来增加对Java、Node.js、Scala、Clojure、Python以及PHP和Perl的支持。基础操作系统是Debian。包含全面的云服务技术支持,各种可定制的收费功能,当然也提供了限制功能的免费云服务资源。最重要是的是使用Heroku Command Line Interface (CLI)进行部署的应用会使用特定的地址进行外部访问,从而对于个人的小型应用,可以不用购买域名和DNS服务。
注册一个免费 Heroku account.
本地安装python3.6(不是必备的,这里指出是因为heroku官方使用的是py3.6,可以在部署之前使用runtime.txt进行特定环境的指定)
- 本地的python环境使用Virtualenv 进行管理。可以使用
pip install virtualenv进行安装(virtualenvwrapper也是可以的)- 如果需要本地运行需安装Postgres (同样不是必须,但这里为了简化部署流程我们使用Postgres)
准备工作做好之后,就可以下载Heroku CLI
Mac系统安装
可以使用homebrew安装
brew install heroku
或者使用 MacOS installer.当前的支持版本是MacOS 10.7+
Windows
Ubuntu/Debian and Windows Bash
此版本的Heroku CLI不会进行自动更新,需要手动的使用apt-get进行更新。使用Standalone version可以进行自动更新
对于Windows bash必须首先安装 Windows Subsystem for Bash 下面的命令需要bash执行,而不能在cmd或者PowerShell中执行。
wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
使用 snapcraft安装
独立安装是一个简单的压缩包,具有二进制文件,在脚本化环境中有用,或者存在受限制的访问(非sudo)。这些是可以修改以适应您的环境的示例说明。它包含自己的node.js二进制文件,并且将像上面的安装方法一样自动更新。
下载tarball并解压缩,以便您可以从PATH访问二进制文件。例如:
不要直接copy执行,需要有一些改动,如下
# replace REPLACE_ME_OS/REPLACE_ME_ARCH with values as noted below
$ wget https://cli-assets.heroku.com/heroku-cli/channels/stable/heroku-cli-REPLACEME_OS-REPLACE_ME_ARCH.tar.gz -O heroku.tar.gz
$ tar -xvzf heroku.tar.gz
$ mkdir -p /usr/local/lib /usr/local/bin
$ mv heroku-cli-v6.x.x-darwin-64 /usr/local/lib/heroku
$ ln -s /usr/local/lib/heroku/bin/heroku /usr/local/bin/heroku
CLI由node.js构建,可通过npm安装。这是一种手动安装方法,可用于自动更新不理想或不提供预构建的node.js二进制文件的环境中。
强烈建议您使用其他安装方法之一此安装方法不会自动更新,并要求您具有兼容版本的节点。我们倾向于使用当前版本的节点,并且不支持旧版本。
此安装方法适用于ARM ,BSD, Arch Linux的用户,需要node 和npm提前安装。
npm install -g hekoku-cli
检查是否安装成功
在terminal执行heroku --version
heroku --version
#正确的输出如下
#heroku-cli/6.0.0-010a227 (darwin-x64) node-v8.0.0
开始部署
首先使用terminal登录heroku
heroku login
Enter your Heroku credentials.
Email: python@example.com #输入邮箱
Password:
...#输入地址
下载官方的python示例,并进入该目录
git clone https://github.com/heroku/python-getting-started.git
cd python-getting-started
下载之后的目录结构如下

- Procfile该文件为heroku运行的配置文件,默认的格式为web: gunicorn projectname.wsgi --log-file -
- requriements.txt为pip freeze -> requriements.txt生成的python third party package
- runtime.txt可以指定系统的运行环境例如python-2.7.10
在heroku创建app
heroku create appname[optional]
#appname是可选的,如果没有输入将会是一个随机的名字,为了访问的方便最好是使用一个特定的名字,如果已经创建了可以使用
heroku apps:rename newname
#更改名字,创建app的同时会在远程创建一个代码仓库默认是master分支
部署app
git push heroku master
Counting objects: 232, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (217/217), done.
Writing objects: 100% (232/232), 29.64 KiB | 0 bytes/s, done.
Total 232 (delta 118), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing python-3.6.0
remote: $ pip install -r requirements.txt
remote: Collecting dj-database-url==0.4.0 (from -r requirements.txt (line 1))
remote: Downloading dj-database-url-0.4.0.tar.gz
remote: Collecting Django==1.9.2 (from -r requirements.txt (line 2))
remote: Downloading Django-1.9.2-py2.py3-none-any.whl (6.6MB)
remote: Collecting gunicorn==19.4.5 (from -r requirements.txt (line 3))
remote: Downloading gunicorn-19.4.5-py2.py3-none-any.whl (112kB)
remote: Collecting psycopg2==2.6.1 (from -r requirements.txt (line 4))
remote: Downloading psycopg2-2.6.1.tar.gz (371kB)
remote: Collecting whitenoise==2.0.6 (from -r requirements.txt (line 5))
remote: Downloading whitenoise-2.0.6-py2.py3-none-any.whl
remote: Installing collected packages: dj-database-url, Django, gunicorn, psycopg2, whitenoise
remote: Running setup.py install for dj-database-url: started
remote: Running setup.py install for dj-database-url: finished with status 'done'
remote: Running setup.py install for psycopg2: started
remote: Running setup.py install for psycopg2: finished with status 'done'
remote: Successfully installed Django-1.9.2 dj-database-url-0.4.0 gunicorn-19.4.5 psycopg2-2.6.1 whitenoise-2.0.6
remote:
remote: $ python manage.py collectstatic --noinput
remote: 58 static files copied to '/app/gettingstarted/staticfiles', 58 post-processed.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 39.3M
remote: -----> Launching...
remote: Released v4
remote: http://lit-bastion-5032.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To git@heroku.com:lit-bastion-5032.git
* [new branch] master -> master
前面的步骤执行完毕没有错误的话就是已经部署成功了,此时执行
heroku open
就会打开app的index页面,但是怎么可能没错误呢?
错误的出现
1.favicon.ico缺失错误
favicon就是网站的logo,打开app服务器肯定需要这个文件,并且需要这个文件的路径可以被访问到,这样的话我们需要将该文件和路径添加到项目中和url中
可以随便找一个图片然后到这里生成对应的favicon.ico,添加到项目根目录的static中就可以了
当然只是这样做进行部署的时候还是错误的,因为虽然添加了静态文件但是服务器使用appname.herokuapp.com/favicon进行访问根本没有这个图片,所以需要在url中添加
from django.views.generic.base import RedirectView
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/favicon.ico')),
#后面的url就是favicon.ico的存放位置
2.数据库完全没法使用
如果你进入settings.py查看的话会发现databases的设置是
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
这时候你在本地进行model的设计,然后makemigration, migrate完全没问题,进入admin也可以进行数据的修改添加。这时候你想把本地的更改同步到heroku的master上,于是你又执行了
git add .
git commit -m "设计了model x, y, z"
git push heroku master
heroku open
结果: 500 server error
完全没道理,好像没什么问题。
但是heroku的设计就是这样的,如果你本地涉及到了model的操作,一定记得在git push heroku master之后需要执行
heroku run python manager.py migrate
#和本地的migrate是同样的,不过是命令heroku在我们部署项目的服务器上执行migrate操作,因为migrations文件夹在git的管理下,所以不需要执行makemigrations操作
没有样式?
假如你为了将自己的个人网站做的漂亮好看,好不容易找到一个很好的html template,并且分别将html和static文件加入到了特定的文件夹,但是部署到heroku之后还是没办法看到美丽的样式。此时,检查
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
是否都设置正确,如果已经设置正确了那么执行
python manager.py collectstatic
0 Comments latest
No comments.