zoukankan      html  css  js  c++  java
  • Heroku

    Heroku (Python)

    Getting Started with Python on Heroku

    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.

    >heroku create lwc
    

    下面会创建两个git

    >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:virtualdirlwc_testlwc>heroku ps:scale web=1
    # log
    Scaling dynos... done, now running web at 1:Free.
    
    (lwc_test) D:virtualdirlwc_testlwc>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:

    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.

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

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

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

    Declare app dependencies

    requirements.txt, and it looks something like this:

    Django1.9.1
    gunicorn
    19.4.5
    psycopg22.6.1
    whitenoise
    2.0.6
    dj-database-url0.3.0
    below 2 tools need to be added, otherwise, the css won't work
    dj-static
    0.0.5
    static==0.4

    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.

    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
    'Scriptsactivate' 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:PythonWebSWPython27libsite-packagessouth-0.8.4-py2.7.eggsouthdb\__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:virtualdirlwc_testlwc>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:virtualdirlwc_testlwc>heroku ps:stop run.5656
    Stopping run.5656 dyno... done
    
    (lwc_test) D:virtualdirlwc_testlwc>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
    

    关于下面公众号获取更多信息

    http://www.codingsoho.com
  • 相关阅读:
    游戏架构草稿(1)
    蔡学镛:架构师最重视的文档
    常见拉丁字母
    图像识别学习1
    .net framework 2.0,3.0与3.5之间的关系 [转载]
    ASP.NET Session丢失问题原因及解决方案[转载]
    PLSQL 循环游标 cursor loop fetch into【转载】
    oracle case when的用法 【转载】
    Oracle to_char格式化函数 [转载]
    oracle表关联应用 【转载】
  • 原文地址:https://www.cnblogs.com/2dogslife/p/5763519.html
Copyright © 2011-2022 走看看