Heroku

Heroku (Python)

Getting Started with Python on Herokucss

Heroku Toolbet

Prerequisite

help

>heroku --help

Prepare the app

clone project

git clone git@github.com:navicester/spreading.git
cd spreading

check version

git remote -v
#log
origin  git@github.com:navicester/spreading.git (fetch)
origin  git@github.com:navicester/spreading.git (push)

Deploy the app

login

>heroku login

create

>heroku create
#Log
Creating app... done, stack is cedar-14
https://thawing-tor-1782.herokuapp.com/ | https://git.heroku.com/thawing-tor-1782.git

Heroku generates a random name (in this case thawing-tor-1782) for your app, or you can pass a parameter to specify your own app name.html

>heroku create lwc

下面會建立兩個gitpython

>git remote -v
#log
heroku  https://git.heroku.com/thawing-tor-1782.git (fetch)
heroku  https://git.heroku.com/thawing-tor-1782.git (push)
origin  git@github.com:navicester/spreading.git (fetch)
origin  git@github.com:navicester/spreading.git (push)

常見問題

  1. 先創建git,而後再執行heroku create,不然沒法push
  2. 鏈接超時(代理,未驗證)
    https://devcenter.heroku.com/articles/using-the-cli#using-an-http-proxy

deploy your code

git push heroku master
>heroku ps:scale web=1
>heroku ps
(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:scale web=1
# log
Scaling dynos... done, now running web at 1:Free.

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps
#log
=== web (Free): gunicorn lwc.wsgi
web.1: up 2016/01/17 15:05:27 (~ 4m ago)

View logs

View information about your running app using one of the logging commands, heroku logs:mysql

heroku logs --tail

Define a Procfile

Use a Procfile, a text file in the root directory of your application, to explicitly declare what command should be executed to start your app.linux

The Procfile in the example app you deployed looks like this:git

web: gunicorn spreading.wsgi --log-file -

if wsgi in a deep subfolder, need xx.spreading.wsgi to get it work, xx is python packagegithub

Declare app dependencies

requirements.txt, and it looks something like this:web

Django==1.9.1
gunicorn==19.4.5
psycopg2==2.6.1
whitenoise==2.0.6
dj-database-url==0.3.0
below 2 tools need to be added, otherwise, the css won't work
dj-static==0.0.5
static==0.4sql

gunicorn : Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX.
psycopg2 : Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
whitenoise : Radically simplified static file serving for WSGI applications
dj-database-url : This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django application.
dj-static : This is a simple Django middleware utility that allows you to properly serve static assets from production with a WSGI server like Gunicorn.
static : Serve static or templated content via WSGI or stand-alone.
wsgiref : WSGI (PEP 333) Reference Library
wheel : A built-package format for Python.shell

Install the dependencies

pip freeze > requirements.txt
#添加其餘沒有生成的package
pip install -r requirements.txt

make sure you are working in the right environment, if you work in virtualenv, run
'Scripts\activate' before 'pip freeze'

in wsgi.py

try:
    from dj_static import Cling

    application = Cling(get_wsgi_application())
except:
    pass

Run the app locally

  • use python manage.py collectstaticto collect statistic
    ( when push to Heroku, it will run this automaticly)

  • Edit the requirements.txt file. Remove the line containing gunicorn. Or add a new file **requirements_windows.txt", then run pip install -r requirement_windows.txt

  • Now start your application locally using heroku local, which was installed as part of the Toolbelt. Just like Heroku, heroku local examines the Procfile to determine what to run. Instead of using the default Procfile, use the Windows version which starts a simple Python web server instead. It reads something like web: python manage.py runserver 0.0.0.0:5000. To start your app locally, run: heroku local web -f Procfile.windows

  • Open http://localhost:5000 with your web browser

Provision add-ons

Provision a database

heroku database
Migrating from MySQL to Postgres on Heroku
heroku-postgresql
在該目錄下能夠看到db,或者直接從apps進入add-on中的數據庫

Connection Settings
Host
    ec2-54-225-165-132.compute-1.amazonaws.com
Database
    dbv6n78stkmeqg
User
    ntccscinrvwneo
Port
    5432
Password
    Show 
Psql
    heroku pg:psql --app intense-depths-2863 DATABASE
URL
    Show

修改settings中的數據庫配置

import dj_database_url
# Parse database configuration from $DATABASE_URL
DATABASES['default'] =  dj_database_url.config()

it's not needed to have explicit configuration as below:

if len(DATABASES['default']) == 0:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'dbv6n78stkmeqg',
            'USER': 'ntccscinrvwneo',
            'PASSWORD': '5432',
            'HOST': '', 
            'PORT': '', 
        }
    }

if I continue to use sqlite3, it will report error

Exception Type: DatabaseError
Exception Value:    no such table: django_session

A database is an add-on, and so you can find out a little more about the database provisioned for your app using the addons command in the CLI:

heroku addons

Listing the config vars for your app will display the URL that your app is using to connect to the database, DATABASE_URL:

heroku config

Heroku also provides a pg command that shows a lot more:

heroku pg

常見問題

Issue 1: Django. South. Heroku. KeyError: 'default'

添加了DATABASES['default'] = dj_database_url.config()
, local運行python manage.oy runserver時,報以下錯誤

File "D:\PythonWebSW\Python27\lib\site-packages\south-0.8.4-py2.7.egg\south\db\__init__.py", line 83, in <module>
    db = dbs[DEFAULT_DB_ALIAS]
KeyError: 'default'

Solution : add

import os
from django.conf import settings

參考:http://stackoverflow.com/questions/23964807/django-south-heroku-keyerror-default
evernote

Issue 2 : Cannot run more than 1 Free size dynos.

You would do this via heroku ps and then heroku ps:stop to stop the process.
Eg.

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps
=== web (Free): gunicorn lwc.wsgi
web.1: idle 2016/01/20 00:48:46 (~ 21h ago)
=== run: one-off processes
run.2351 (Free): up 2016/01/19 23:36:22 (~ 22h ago): python manage.py shell

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:stop run.5656
Stopping run.5656 dyno... done

(lwc_test) D:\virtualdir\lwc_test\lwc>heroku ps:stop web.1
Stopping web.1 dyno... done

database

若是是sqlite3,不須要把db文件傳到服務器上(猜想系統會根據setting文件自動建立)

heroku run python manage.py syncdb

Exception Type:
DatabaseError

Exception Value:
no such table: joins_join

no-such-table-error-on-heroku-after-django-syncdb-passed
no-such-table-error-on-heroku-after-django-syncdb-passed

Run Command in Heroku

heroku run python manage.py runserver

OR

heroku run bash
$python manage.py runserver
$exit

關於下面公衆號獲取更多信息

相關文章
相關標籤/搜索