在Heroku云平台上部署Python的Django框架的教程

WBOY
Release: 2016-06-10 15:14:49
Original
1049 people have browsed it

Heroku是一个很棒的平台,它有很多的控件,并且搭建环境相对来说也比较容易。本指南中,我将一步一步指导你在Heroku平台上部署一个简单地Django应用
搭建开发环境
Heroku工具链

假设你已经在Heroku平台上注册了一个帐户,并且在里面创建了一款应用,为了一会儿通过CLI与Heroku交互,你需要安装Heroku工具链。在这篇指南中,我们用"Sample-Project"作为应用的名字。
Git仓库

在部署你的应用到Heroku之前,你需要先将你的代码签入git仓库中。Heroku提供的git仓库信息可以在你的应用设置页中找到。

复制代码代码如下:
one git@heroku.com:sample-project.git

Python 和 Virtualenv

如果这不是你的第一款python应用,你或许已经把环境搭建起来了。然后,不同的Pyton版本之间存在兼容性问题,因此你应该在开发你的Python应用时使用Virtualenv命令来创建一个虚拟的环境。

# 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
Copy after login

创建一款Django应用

建议你安装django-toolbelt,它由以下几部分组成。

- Django
- Gunicorn (WSGI服务器)
- dj-database-url (一个Django配置工具)
- dj-static (一个Django静态文件服务器)

(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
Copy after login


部署你的代码

1. 创建ProcFile
ProcFile被用来声明应该被执行的开始web dyno命令。这个文件应该被放在manage.py(指定的)文件夹中。简单地创建一个ProcFile文件,如下面的一行例子所示。

复制代码代码如下:

unicorn Sample_Project.wsgi --log-file -

2. 查看你希望部署代码的远程服务器简称。下面这个例子显示地是配置仅有一个简单远程服务器的例子,它的简称是origin。(假设)你可能已经配置过很多的远程服务器。

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

3. 部署你的代码

使用"git push"去部署你的代码。

$ 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
Copy after login

4. 验证你部署的代码

$ heroku open
Copy after login

你应该看到标准的Django开始页面(显示的是)“It worked! Congratulations on your first Django-powered page.”

5. 使用dyno测量你的应用规模

$ heroku ps:scale web=1 Scaling dynos... done, now running web at 1:1X.
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!