0%

使用Heroku进行开发

  • 官网

  • snapcraft

  • Heroku 是 Salesforce 旗下云服务商,提供方便便捷的各种云服务,如服务器,数据库,监控,计算等等,Heroku 平台的灵活性极高且支持多种编程语言.若想把程序部署到 Heroku 上,开发者要使用 Git 把程序推送到 Heroku 的 Git 服务器上.并且他提供了免费版本.对于个使用,搞一些小程序还是可以满足使用的.

安装使用

  • 这里直接下载一个独立包使用,也可以使用snap安装.
1
~$ wget -c https://cli-assets.heroku.com/heroku-linux-x64.tar.xz
  • 登录 Heroku
1
2
3
4
5
~$ heroku login
heroku: Enter your login credentials
Email: yjdwbj@gmail.com
Password: ***************
Logged in as yjdwbj@gmail.com
  • 添加 SSH 公钥
1
2
3
4
~$ heroku keys:add
Found an SSH public key at /home/michael/.ssh/id_rsa.pub
? Would you like to upload it to Heroku? Yes
Uploading /home/michael/.ssh/id_rsa.pub SSH key... done

创建 App 项目

  • 创建项目有两种方,一是登录Heroku网站创建,二是可以使用Heroku Toolbelt 命令创建.
1
2
3
4
5
6
7
~$ heroku create lcy-web-srv
Creating ⬢ lcy-web-srv... done
https://lcy-web-srv.herokuapp.com/ | https://git.heroku.com/lcy-web-srv.git
~ $ heroku apps
=== xxxxxx@gmail.com Apps
app-srv-test
lcy-web-srv

创建 PostgreSQL 数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~$ heroku addons:create heroku-postgresql:hobby-dev --version=9.6 --app=lcy-web-srv
Creating heroku-postgresql:hobby-dev on ⬢ lcy-web-srv... free
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-solid-87926 as DATABASE_URL
Use heroku addons:docs heroku-postgresql to view documentation

~$ heroku config -s -a lcy-web-srv
DATABASE_URL='postgres://qxovwdfmkapqxx:xxx0xxfb7d017ea8d7826435d15d7c3891cc1c7ae9722c497b5c3fee031ed89@ec2-174-129-227-116.compute-1.amazonaws.com:5432/dbbu4a5s4mleid'
~$ heroku pg:info -a lcy-web-srv
=== DATABASE_URL
Plan: Hobby-dev
Status: Available
Connections: ?/20
PG Version: ?
Created: 2018-07-25 10:23 UTC
Data Size: 0 B
Tables: 0
Rows: 0/10000 (In compliance) - refreshing
Fork/Follow: Unsupported
Rollback: Unsupported
Continuous Protection: Off
Add-on: postgresql-solid-87926

创建 Redis 数据库

  • Heroku Redis 安装需要绑定一张信用卡.这里就创建了.

使用 Pipenv 创建 Django 项目

Pyenv 使用

  • github 位置
  • 使用命令
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #列出可用的片本
    ~$ pyenv install --list
    #安装3.6.6 与2.7.24两个版本
    ~$ pyenv install 3.6.6 2.7.24
    #列出当前系统安装的版本.
    ~$ pyenv versions
    system
    2.7.13
    2.7.13/envs/py2dev
    3.6.6
    3.6.6/envs/py3dev
    py2dev
    * py3dev (set by PYENV_VERSION environment variable)
    # 安装pyenv-virtualenv插件
    ~$ git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
    ~$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
    #创一个python3.6.6版本的虚拟环境名为py3dev
    ~$ pyenv virtualenv 3.6.6 py3dev
    #激活虚拟环境
    ~$ pyenv activate py3dev # pyenv deactivate 是退出虚拟环境.
    # 列出已有的虚拟环境路径
    ~$ pyenv virtualenvs
    2.7.13/envs/py2dev (created from /home/michael/.pyenv/versions/2.7.13)
    3.6.6/envs/py3dev (created from /home/michael/.pyenv/versions/3.6.6)
    py2dev (created from /home/michael/.pyenv/versions/2.7.13)
    * py3dev (created from /home/michael/.pyenv/versions/3.6.6)

Pipenv 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 进入上述激活的pyenv环境安装pipenv
(py3dev) ~$ pip install pipenv
~$ echo 'export PIPENV_VENV_IN_PROJECT=1' >> ~/.bash_profile

# 初始一个python3的环境
~$ mkdir pipenv3
~$ cd pipenv3 && pipenv --three
# 安装软件包
~$ pipenv install django
# 查看包的依赖关系
~$ pipenv graph
# 如果从git服务器克隆源码下来,里面有Pipfile与Pipfile.lock,就可以直接使用下面命令安装环境
~$ pipenv install # 如果想要安装Pipfile.lock中固定的版本号,加上 --ignore-pipfile选项.
# 如果之前项目是用requirements.txt来管理.就用下面命令安装环境.
~$ pipenv install -f path/to/requirements.txt

Django

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
~$ mkdir Heroku-Dir && cd Heroku-Dir
~$ sudo pip3 install pipenv # 使用python3安装,python2的支持快到了.
Collecting pipenv
Using cached
[...]
Collecting virtualenv-clone>=0.2.5 (from pipenv)
Using cached https://files.pythonhosted.org/packages/6d/c2/dccb5ccf599e0c5d1eea6acbd058af7a71384f9740179db67a9182a24798/virtualenv_clone-0.3.0-py2.py3-none-any.whl
Installing collected packages: virtualenv, setuptools, certifi, pip, virtualenv-clone, pipenv
Successfully installed certifi-2018.4.16 pip-18.0 pipenv-2018.7.1 setuptools-40.0.0 virtualenv-16.0.0 virtualenv-clone-0.3.0

~$ mkdir lcy-web-srv && cd lcy-web-srv
~$ $ pipenv --three
Creating a virtualenv for this project...
Pipfile: /home/michael/Heroku-Dir/lcy-web-srv/Pipfile
[...]
Virtualenv location: /home/michael/.local/share/virtualenvs/lcy-web-srv-3zhvmAj6
Creating a Pipfile for this project...

~$ pipenv shell #激活Python3虚拟环境
Launching subshell in virtual environment…
. /home/michael/.local/share/virtualenvs/lcy-web-srv-3zhvmAj6/bin/activate
michael@debian:~/Heroku-Dir/lcy-web-srv$ . /home/michael/.local/share/virtualenvs/lcy-web-srv-3zhvmAj6/bin/activate
(lcy-web-srv-3zhvmAj6) michael@debian:~/Heroku-Dir/lcy-web-srv$

~$ pipenv install django

安装 Django 模版工程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
~$ django-admin.py startproject --template=https://github.com/heroku/heroku-django-template/archive/master.zip --name=Procfile helloworld
~$ cd helloworld
~$ git init
~$ git add -A
~$ git commit -am "Initial commit"
~$ heroku git:remote -a lcy-web-srv
set git remote heroku to https://git.heroku.com/lcy-web-srv.git
~$ git push heroku master
Counting objects: 13, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
[...]
remote: https://lcy-web-srv.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/lcy-web-srv.git
* [new branch] master -> master
#Django合并操作.
~$ heroku run python manage.py migrate
Running python manage.py migrate on ⬢ lcy-web-srv... up, run.6377 (Free)
/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.

Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
[...]
Applying sessions.0001_initial... OK

~$ pip install django_heroku dj-database-url #在Pipenv中通过pip安装依赖包.
~$ python manage.py migrate  #在本地中创建基本认证表项.
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
[...]
Applying sessions.0001_initial... OK

~$ python manage.py createsuperuser #本地创建一个超级用户.
Username (leave blank to use 'michael'): xxx
Email address: xxxxx@gmail.com
Password:
Password (again):
Superuser created successfully.

~$ heroku run python manage.py createsuperuser #在Heroku的项目中创建一个超级用户.
Username (leave blank to use 'michael'): xxx
Email address: xxxxx@gmail.com
Password:
Password (again):
Superuser created successfully.
  • 通过上述操作,一个带数据库后台的Django项目成功布署到 Heroku 上面运行.能过公共域名访问https://lcy-web-srv.herokuapp.com/admin/,输入正确的用户名与密码就可以登录到Djangot后台管理了.
  • 注意,如果不是用上面这个模版创建的要项目,要在项目根目录下创建Procfile,它里面只有一行内容web: gunicorn {{ project_name }}.wsgi.
  • 注意,还要在根目下添加一个Pipfile文件.内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"


[requires]
python_version = "3.6"


[packages]

"psycopg2-binary" = "*"
django-heroku = "*"
gunicorn = "*"


[dev-packages]

错误

1
django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
  • 如果出现上述错误,在settings.py加入PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)),STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')两行.或者设置heroku config:set DISABLE_COLLECTSTATIC=1

谢谢支持