在 Heroku 上部署 Django 应用 已翻译 100%

oschina 投递于 2014/09/09 13:04 (共 3 段, 翻译完成于 09-11)
阅读 3101
收藏 36
2
加载中

Heroku is a great platform with lots of useful addons and the set up is relatively easy. In this tutorial, I will give you a step-by-step guild to deploy a simple Django app on Heroku.

Develop Environment Setup

Heroku Toolbelt

Assuming you have signed up for a account on Heroku and created an app in it, you will need to install Heroku Toolbelt to interact with Heroku through CLI later. We will use “Sample-Project” as the app name in this tutorial.

Git Repository

You will need to first check in your code to a git repository before deploying it to Heroku. The information of the git repository provided by Heroku can be found on your app’s settings page.

git clone git@heroku.com:sample-project.git
已有 1 人翻译此段
我来翻译

Python and Virtualenv

If this is not your first python app, you probably already have this set up. Otherwise, there are compatibility issue for different versions of Python, thus you should be using the Virtualenv to create a virtual environment when developing your Python app.

# Install pip 
$ [sudo] python get-pip.py 

# Install Virtualenv
$ [sudo] pip install virtualenv 

# Create a virtual environment
$ virtualenv venv

# Activate venv
$ source venv/bin/activate

Create a Django App

It is recommended to install django-toolbelt, which consists of the following packages.
- Django
- Gunicorn (WSGI server)
- dj-database-url (a Django configuration helper)
- dj-static (a Django static file server)

(venv)$ pip install django-toolbelt
(venv)$ cd Sample-Project

# Create a Django project name Sample_Project
# A valid Django project name can't contain dash
(venv)$ django-admin.py startproject Sample_Project .

# Create the requirements file   
(venv)$ pip freeze > requirements.txt
已有 1 人翻译此段
我来翻译

Deploying your code

1. Create the ProcFile
A Procfile is used to declare what command should be executed to start a web dyno. This file should be in the same folder as the manage.py. Simply create a file named ProcFile and put the line below in the file.

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

2. Check the short name of the remote server that you want to deploy your code to.
The sample output below shows there is only a single remote server, which short name as origin, configured. You may have configured more than a few remote server.

$ git remote -v
origin	git@heroku.com:Sample-Project.git (fetch)
origin	git@heroku.com:Sample-Project.git (push)

3. Deploy your code
Use “git push  ” to deploy your code.

$ git push origin master
Initializing repository, done.
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 2.64 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)

-----> Python app detected
-----> Installing runtime (python-2.7.8)
-----> Installing dependencies with pip
       Downloading/unpacking Django==1.6.6 (from -r requirements.txt (line 1))
       Downloading/unpacking dj-database-url==0.3.0 (from -r requirements.txt (line 2))
         Downloading dj_database_url-0.3.0-py2.py3-none-any.whl
       Downloading/unpacking dj-static==0.0.6 (from -r requirements.txt (line 3))
         Downloading dj-static-0.0.6.tar.gz
...
To git@heroku.com:Sample-Project.git
 * [new branch]      master -> master

4. Verify that your code has been deployed

$ heroku open

You should see the standard Django page stating “It worked! Congratulations on your first Django-powered page.”

5. Scale your app with Dynos

$ heroku ps:scale web=1
Scaling dynos... done, now running web at 1:1X.
已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(0)

返回顶部
顶部