Heroku替代方案:在DigitalOcean上使用Dokku部署应用程序

tech2023-02-26  95

When Heroku announced their (quite reasonable) new limits for free apps, I realized that I would have to find another source of hosting for all the small, low-traffic projects that I currently have running on Heroku. Way back in the day, Heroku was totally free for apps that only required one dyno, but after years of abuse from jerks like me, they dropped that to eventually allowing free apps to run for 18 out of 24 hours per day (which is ok for low-traffic prototypes) and as of June 1, granting a shared pool of free hours.

当Heroku宣布他们对免费应用程序的(相当合理的)新限制时,我意识到我将不得不为我目前在Heroku上运行的所有小型,低流量项目找到另一个托管来源。 回想过去,Heroku对于只需要一个测力计的应用程序是完全免费的,但是在像我这样的混蛋多年滥用之后,他们放弃了这一点,最终允许免费应用程序每天运行24小时中的18小时(没关系) (针对低流量的原型),并自6月1日起,授予共享的免费时间段。

Since I have such an unreasonable number of apps running on Heroku, I thought it was high time to try out Dokku. Dokku is a Heroku-like tool that allows you to deploy complex apps by simply pushing with Git. It supports Heroku buildpacks directly, so you can transition existing apps without difficulty, and has a number of plugins for datastores and other components. And, thankfully, Digital Ocean provides a pre-installed Dokku image that will spare you the trouble of installing Dokku yourself; you can just spin up a server and start Dokku-ing right away! This article will walk you through setting up a Dokku server on DigitalOcean with your own root domain and deploying a simple static site to it.

由于我在Heroku上运行的应用程序数量如此之多 ,我认为现在是尝试Dokku的时候了。 Dokku是一种类似于Heroku的工具,通过使用Git进行推送,您就可以部署复杂的应用程序。 它直接支持Heroku构建包,因此您可以轻松转换现有应用程序,并且具有许多用于数据存储和其他组件的插件。 而且,值得庆幸的是, Digital Ocean提供了一个预先安装的Dokku映像 ,它将使您免于自己安装Dokku的麻烦。 您可以启动服务器并立即启动Dokku-ing! 本文将引导您通过自己的根域在DigitalOcean上设置Dokku服务器,并向其部署一个简单的静态站点。

独孤与Heroku之间的差异 (Differences between Dokku and Heroku)

Dokku requires at least some comfort level with running your own servers; you may have to modify nginx configurations, manually configure some plugins, or turn to the system tools for debugging.

Dokku在运行自己的服务器方面至少需要一定的舒适度; 您可能需要修改nginx配置,手动配置一些插件,或使用系统工具进行调试。

Dokku utilizes Docker, which is a fine platform but can add an extra layer of complexity to a server install.

Dokku利用了Docker ,这是一个很好的平台,但可以为服务器安装增加一层额外的复杂性。

Dokku requires root access to a VPS to install plugins, run commands, etc.

Dokku需要对VPS的根访问权限才能安装插件,运行命令等。

In short, you’re going to need to do a bit more command line setup on Dokku than Heroku — nothing you can’t pick up along the way, but you might need to do some light reading.

简而言之,您将需要在Dokku上比Heroku进行更多的命令行设置-在此过程中您无法进行任何操作,但是您可能需要阅读一些内容。

在DigitalOcean上创建Dokku服务器 (Creating a Dokku Server on DigitalOcean)

First, log in to DigitalOcean and follow this link to create a new server on DigitalOcean using the preinstalled Dokku app. Dokku requires at least 1GB of RAM, but $10/mo to host all your stuff is a pretty small price.

首先,登录到DigitalOcean,并使用此链接使用预先安装的Dokku应用在DigitalOcean上创建新服务器。 Dokku至少需要1GB的RAM,但以10美元/月的价格托管所有设备是一个很小的价格。

For your hostname, enter the base domain you want to use to host your apps. Default Dokku apps will appear at <appname>.<hostname> (for example, myapp.example.com). Make sure you own this domain and register it if you need to!

对于主机名,输入要用于托管应用程序的基本域。 默认的Dokku应用程序将出现在<appname>.<hostname> (例如, myapp.example.com )上。 确保您拥有此域并在需要时注册它!

设置域 (Setting up the domain)

You will want to associate a domain with your Dokku server. Just set up an A record for your domain pointing at your server’s IP.

您将需要将域与Dokku服务器关联。 只需为您的域设置一个指向服务器IP的A记录即可 。

The remainder of this article will assume that you’ve registered example.com for this purpose.

本文的其余部分将假定您已为此目的注册了example.com 。

We’re going to set things up such that your apps will appear at <appname>.example.com. To accomplish this, you’ll need to add a few DNS records for your domain:

我们将进行设置,以使您的应用程序出现在<appname>.example.com 。 为此,您需要为您的域添加一些DNS记录:

create a blank A record pointing at your your server’s IP

创建一个指向您服务器IP的空白A记录

create a wildcard (*) A record pointing at the same IP.

创建通配符( * )指向相同IP的记录。

After DNS is resolved, you can continue with the rest of this tutorial.

解决DNS后,您可以继续本教程的其余部分。

If you want to host an app on another domain, you can either:

如果要在另一个域上托管应用程序,则可以:

name the app as otherdomain.com when you create it, which will tell Dokku to use that domain, or

在创建应用时将其命名为otherdomain.com ,这将告诉Dokku使用该域,或者

set up a CNAME DNS record at www.otherdomain.com pointing at myapp.example.com.

在www.otherdomain.com指向myapp.example.com设置CNAME DNS记录。

创建您的第一个应用 (Creating Your First App)

SSH into your server and run the following:

SSH进入您的服务器并运行以下命令:

$ dokku apps:create example.com

This will create an app called “example.com” on your server. This is the app that will appear at the root of your domain; since it has a . in it, Dokku interprets it as a domain name and configures itself accordingly. For future apps, you can simply dokku apps:create appname, and appname.example.com will be their host.

这将在您的服务器上创建一个名为“ example.com”的应用程序。 该应用程序将出现在您域的根目录中; 因为它有一个. 在其中,Dokku将其解释为域名,并进行相应的配置。 对于将来的应用程序,您可以简单地运行dokku apps:create appname ,而appname.example.com将成为它们的主机。

Something you might want to do is to host a static site on example.com. This will let you easily keep it up to date, especially if you use a static site builder like middleman or jekyll.

您可能想做的是在example.com上托管一个静态网站。 这将使您轻松地使其保持最新状态,尤其是当您使用诸如Middleman或jekyll之类的静态网站构建器时。

This capability is built into Dokku, via the handy buildpack-nginx. To set it up, at the root of the project, create an empty file called .static ($ touch .static). This is how the buildpack detects a static site. When you push this to Dokku, it should detect that you are creating a static site and proceed accordingly.

该功能通过方便的buildpack-nginx内置到Dokku中。 要进行设置,请在项目的根目录下创建一个名为.static ( $ touch .static )的空文件。 这是buildpack检测静态站点的方式。 将其推送到Dokku时,它应该检测到您正在创建静态站点并进行相应的处理。

Your project can be as simple as an index.html file, containing whetever you want. Create a new git repo with git init, and add index.html, .static, and .env to it. Then, add Dokku as a remote:

您的项目可以像index.html文件一样简单,其中包含您想要的任何内容。 使用git init创建一个新的git repo,并添加index.html , .static和.env 。 然后,将Dokku添加为远程服务器:

git remote add dokku dokku@example.com:example.com

Now, you should be able to deploy via a simple git push:

现在,您应该可以通过简单的git push进行部署:

git push dokku master

You should be able to see as Dokku configures a container for your app (detecting the static buildpack), and when you navigate to example.com you should see the index.html you created hosted there.

您应该能够看到Dokku为应用程序配置了一个容器(检测静态buildpack),并且当您导航到example.com您应该看到托管在其中的index.html 。

下一步 (Next Steps)

If you have any existing Heroku apps, you should be able to simply add the Dokku remote with Git and run git push dokku master.

如果您有任何现有的Heroku应用程序,则应该能够简单地使用Git添加Dokku遥控器并运行git push dokku master 。

You may need to set up a database of some sort. Luckily, the Dokku project currently supports a variety of datastore plugins, including Postgres, Mongo, and Mysql. They are all very easy to install and use.

您可能需要建立某种数据库。 幸运的是,Dokku项目当前支持各种数据存储插件 ,包括Postgres,Mongo和Mysql。 它们都很容易安装和使用。

If you have any trouble, the Dokku docs are very straightforward and comprehensive.

如果您有任何麻烦, Dokku文档将非常简单和全面。

翻译自: https://www.sitepoint.com/heroku-alternative-deploy-apps-dokku-digitalocean/

最新回复(0)