部署到Heroku:简介

tech2023-02-23  102

Thanks to Matthew Wilkin for kindly helping to peer review this article.

感谢Matthew Wilkin慷慨地帮助同行审阅本文。

In this article, you’ll learn about Heroku and how to deploy your web application to it.

在本文中,您将了解Heroku以及如何将Web应用程序部署到其中。

If you’ve never heard of it, Heroku is a managed server platform for quickly deploying web applications. It automatically provisions server resources for you, and deployment is as easy as a git push to your app’s repository on Heroku. Best of all, you can deploy your app for free (provided it doesn’t get too much traffic), which makes getting started free and easy.

如果您从未听说过,Heroku是可快速部署Web应用程序的托管服务器平台。 它会自动为您配置服务器资源,并且部署就像在Heroku上将git push到应用程序的存储库一样容易。 最重要的是,您可以免费部署您的应用程序(前提是该应用程序不会带来太多流量),这使上手免费和容易。

If you have a lot of traffic, Heroku can get a bit pricy; each node (or dyno, as they call them) will cost you $25 or more per month, and adding features like databases will increase that a bit. That said, it’s a heck of a lot cheaper than hiring a devops team to deliver the stability and ease-of-use that Heroku offers.

如果您的流量很大,Heroku可能会有点贵。 每个节点(或称dyno )每月将花费您25美元或更多,并且添加数据库之类的功能将使费用增加一点。 就是说,这比雇用一个开发团队提供Heroku提供的稳定性和易用性要便宜得多。

开始之前 (Before We Start)

If you want to follow along from here, you’ll need to make sure you have a few things handy.

如果您想从这里继续学习,则需要确保您有一些方便的地方。

Go download and install the Heroku Toolbelt. This is the command-line utility that we’ll be using to configure the project.

下载并安装Heroku Toolbelt 。 这是我们将用于配置项目的命令行实用程序。

Make sure your project is using Git. You should know what git is, but if you don’t, here’s some light reading. If you don’t have a project, just make sure git is installed.

确保您的项目正在使用Git 。 您应该知道git是什么,但是如果不知道, 这是一些轻松的阅读 。 如果您没有项目,只需确保已安装git 。

If you already have something ready to go, skip the next section and go straight to Creating a Heroku Project.

如果您已经准备好了一些东西,请跳过下一部分,直接进入创建Heroku项目 。

我们的示例项目 (Our Example Project)

To assemble this example project, you’re going to need pip (which is good to have handy for any Python development anyhow). We’ll be using a Python project written using Flask, a web microframework for Python, but you can mostly follow along with any project (it should be obvious which parts are language-specific and how to adjust them).

要组装此示例项目,您将需要pip (无论如何方便使用Python开发都是件好事)。 我们将使用一个由Flask编写的Python项目, Flask是Python的网络微框架,但是您几乎可以随同任何项目一起学习(很明显,哪些部分是特定于语言的以及如何进行调整)。

If you have something ready, skip ahead to the next section. If you need a project, set up a project folder as follows (you don’t have to call it myproject):

如果您准备就绪,请跳至下一部分。 如果您需要一个项目,请按如下所示设置一个项目文件夹(您不必将其命名为myproject ):

/myproject /templates index.html app.py requirements.txt

And fill them out like so:

像这样填写它们:

app.py:

app.py :

import os import flask app = Flask(__name__) @app.route("/") def index(): return render_template("index.html") if __name__ == "__main__": app.run(port=os.environ.get('PORT', '5000'))

templates/index.html:

templates/index.html :

<!doctype HTML> <html> <head> <title>My example project</title> </head> <body> <h1>This is my project.</h1> <!-- feel free to get a bit more creative here --> <body> </html>

requirements.txt

requirements.txt

Flask==0.10.1

then run:

然后运行:

pip install -r requirements.txt

After that, make sure everything is working by running python app.py and navigating to http://localhost:5000/. If all went well, you should see index.html rendered there.

之后,通过运行python app.py并导航至http://localhost:5000/ ,确保一切正常。 如果一切顺利,您应该在此处看到index.html 。

创建一个Heroku项目 (Creating a Heroku Project)

Here comes the easiest thing in the world. Open a terminal, cd to your project directory, and run the following commands (skip the git one if you already have git in your project):

这是世界上最简单的事情。 打开一个终端, cd到您的项目目录,然后运行以下命令(如果您的项目中已经有git,则跳过git):

$ git init $ heroku create Creating app... done, stack is cedar-14 https://calm-lake-56303.herokuapp.com/ | https://git.heroku.com/calm-lake-56303.git

With this command, Heroku:

使用此命令,Heroku:

generated a name for us (we could have chosen one by running heroku create myproject instead)

为我们生成了一个名称(我们可以通过运行heroku create myproject来选择一个名称)

assigned us a url and a git repository

给我们分配了一个网址和一个git仓库

initialized the heroku remote repository in git for us.

为我们初始化了git中的heroku远程存储库。

We’re just about ready to deploy, but it’s good to know what’s about to happen first. Let’s learn a bit more about how Heroku can know what to do with your code — and how to make sure it’s satisfied.

我们已经准备好进行部署,但是最好先了解即将发生的事情。 让我们更多地了解Heroku如何知道如何使用您的代码-以及如何确保其满意。

除了构建包(或Heroku如何识别您的项目) (An Aside on Buildpacks (or, how Heroku Recognizes Your Project))

Heroku projects are managed by buildpacks, which are in essence instructions for fetching dependencies, building and running your project. There are officially supported buildpacks for Node.js, Ruby, Java, Clojure, Scala, PHP, Python, and Go. You won’t need to tell Heroku what you’re using for these; instead, Heroku will guess what the project is, based on some conventions and heuristics regarding dependency management. For example, a requirements.txt file in your project directory, as exists in the example project above, indicates a Python project. Here are the dependency files that Heroku will use to automatically detect your platform for other platforms:

Heroku项目由buildpacks管理,本质上是获取依赖项,构建和运行项目的说明。 有官方支持的针对Node.js,Ruby,Java,Clojure,Scala,PHP,Python和Go的buildpack。 您无需告诉Heroku这些产品的用途; 相反,Heroku将基于有关依赖项管理的一些约定和启发式方法来猜测项目是什么。 例如,上面示例项目中存在的项目目录中的requirements.txt文件表示一个Python项目。 以下是Heroku用于自动检测其他平台平台的依赖文件:

Node.js: package.json

Node.js: package.json

Ruby: Gemfile

Ruby: Gemfile

PHP: composer.json

PHP: composer.json

Java: pom.xml

Java: pom.xml

Scala: build.properties

Scala: build.properties

Clojure: project.clj

Clojure: project.clj

Go: Godeps/Godeps.json

转到: Godeps/Godeps.json

If you’re using a different language, or using a different build tool for one of these languages, you can also use a third-party buildpack. Heroku maintains an extensive directory of these, so check there before giving up on your preferred language. You can set the buildpack using the git repository URL; for example, to use Upworthy’s Clojure Boot buildpack, you could run this command in your project directory:

如果您使用另一种语言,或针对这些语言之一使用另一种构建工具,则还可以使用第三方buildpack。 Heroku维护了这些的广泛目录 ,因此在放弃您的首选语言之前,请在此处检查。 您可以使用git仓库URL来设置buildpack。 例如,要使用Upworthy的Clojure Boot buildpack ,可以在项目目录中运行以下命令:

$ heroku buildpacks:set https://github.com/upworthy/heroku-buildpack-boot

You might even find an unlisted buildpack on Github — just check around to make sure it’s safe to use!

您甚至可能在Github上找到一个未列出的buildpack-只需检查一下以确保使用安全!

设置您的Procfile (Setting up Your Procfile)

Everything we need is in place, except for one component. Heroku uses a file called Procfile to tell it what it should be running. For your getting started project, you’ll probably just want to specify a web process, but you can also specify worker jobs.

除了一个组件,我们需要的一切都已准备就绪。 Heroku使用一个名为Procfile的文件来告诉它应该运行什么。 对于您的入门项目,您可能只想指定一个web流程,但是您也可以指定worker作业。

Since we run our app with python app.py, we will put the following in our Procfile:

由于我们使用python app.py运行应用程序,因此将以下内容放入Procfile :

web: python app.py

(Later, you might want to use something more performant; you can use Gunicorn by adding it to your requirements.txt and replacing your Procfile contents with web: gunicorn app:app -b 0.0.0.0:$PORT).

(稍后,您可能希望使用性能更高的产品;可以通过将Procfile添加到您的requirements.txt并将Procfile内容替换为web: gunicorn app:app -b 0.0.0.0:$PORT 。)

部署项目 (Deploying Your Project)

Add Procfile to your repository:

将Procfile添加到您的存储库:

$ git add Procfile && git commit -m "added Procfile"

Then, use git push to deploy to Heroku

然后,使用git push部署到Heroku

git push heroku master

🎉🎉🎉恭喜🎉🎉🎉 (🎉🎉🎉 Congratulations 🎉🎉🎉)

Your app should be deployed on Heroku. Navigate to the URL that Heroku tells you, and you should see your home page.

您的应用程序应部署在Heroku上。 导航到Heroku告诉您的URL,您应该会看到主页。

That’s it. That’s the whole article. That’s your life with Heroku now; just push your commits and they get deployed immediately. How crazy is that?!

而已。 这就是整篇文章。 现在就是Heroku的生活; 只需推送您的提交,即可立即部署它们。 那有多疯狂?

一些额外的命令 (Some Bonus Commands)

Just in case you’re not satisfied, here’s some assorted things you might want to do with the heroku command:

万一您不满意,可以使用heroku命令进行以下操作:

heroku config:set MY_ENV_VARIABLE=some_value: Set a persistent config value. Useful for things like database passwords and other configuration.

heroku config:set MY_ENV_VARIABLE=some_value :设置一个持久的配置值。 对于数据库密码和其他配置很有用。

heroku ps:scale web=5: Sudden traffic surge? Scale your process up to 5 web dynos in an instant. Note that this will cost you $125 at $25 per dyno, so use this with caution. To scale back down after you fall off Reddit, run heroku ps:scale web=1.

heroku ps:scale web=5 :突然的流量激增? 即时将您的过程最多扩展到5个网络测功机。 请注意,这将花费您125美元(每台测功机25美元),因此请谨慎使用。 要在退出Reddit后缩小规模,请运行heroku ps:scale web=1 。

Made a mistake? You can list your app’s releases with heroku releases. To roll back to a certain release, run heroku rollback <release identifier>. Or just run heroku release to undo the latest release.

犯了一个错误? 您可以将应用程序的发行版与heroku releases一起列出。 要回滚到某个特定版本,请运行heroku rollback <release identifier> 。 或者只是运行heroku release以撤消最新版本。

You can also manage most of these from Heroku’s dashboard, if you prefer.

如果愿意,您还可以从Heroku的仪表板管理其中的大多数。

翻译自: https://www.sitepoint.com/deploying-to-heroku-an-introduction/

相关资源:部署Flask到Heroku:将Flask应用程序部署到Heroku教程-源码
最新回复(0)