---
title: "使用 erpc worker 将您的第一个应用部署到服务器"
description: "使用 erpc worker 创建 HelloWorld 应用，在本机运行，并部署到 ERPC VPS 或您自己的服务器。"
canonical: "https://erpc.global/zh/doc/erpc-cli/worker-quickstart/"
locale: "zh"
---

# 使用 erpc worker 将您的第一个应用部署到服务器

使用 `erpc worker` 以 Rust 创建一个小型 HTTP API，在本机运行，并通过四条命令将其部署到服务器。

本页介绍 ERPC CLI `v0.1.0-rc.4`，这是一个预发布版本。

`erpc worker`（别名 `erpc w`）将您的应用编译为原生 Linux 二进制文件，并在 ERPC VPS、新建的按小时计费的 VPS 或您自己的服务器上，以经过加固的 systemd 单元运行，中间没有 Docker，也没有运行时。

未能健康启动的 release 会回滚到上一个 release，多个应用可以共用一台服务器，各自使用自己的端口。有关应用的一切都在其根目录的 `erpc.toml` 中，该文件不含任何 Secret，可以提交。

## 前提条件

- **一个 ERPC 账户**，已在本机通过 `erpc login` 登录（[登录与密钥](https://erpc.global/zh/doc/erpc-cli/sign-in-and-keys/)）。
- **ERPC Credit**，用于新建按小时计费的 VPS。按小时计费的产品从预先购买的 ERPC Credit 中支付，Credit 不足以支付的启动会被拒绝。价格请见[价格](https://erpc.global/zh/price/)和 [ERPC Web Dashboard](https://dashboard.erpc.global/zh)。
- 本机上的 **SSH 密钥**：`ssh-keygen -t ed25519`；如果已有密钥，则使用 `erpc config set ssh_key <path>.pub`。
- **为 Linux 构建的方法**。在 Mac 或 Windows 上，需要服务器的 Rust target（`rustup target add x86_64-unknown-linux-gnu`）、`zig`（在 macOS 上为 `brew install zig`）和 `cargo-zigbuild`（`cargo install cargo-zigbuild --locked`）。在与服务器同类的 Linux 机器上（所有 ERPC 服务器均为 x86-64），只需 cargo 即可。没有交叉构建工具时，应用会使用本机自己的工具链构建，此时二进制文件需要其链接时所用的 glibc，`build` 和 `deploy` 会指出该版本。有交叉构建工具时，应用会针对 glibc 2.31 及以上版本构建。

## 部署 HelloWorld

HelloWorld 是一个用 Rust 编写的小型 HTTP API，不需要密钥、Secret 或数据线路。它响应 `GET /`、`GET /hello/{name}` 和 `GET /healthz`。

```bash
erpc worker init --template helloworld   # makes ./hello-world
erpc worker run hello-world              # here: curl http://127.0.0.1:3000/
erpc worker deploy hello-world           # your VPS, or a new hourly one (price asked first)
erpc worker api / --dir hello-world      # {"message":"Hello, World!",…} from the server
```

1. `init` 在 `./hello-world` 中创建应用及其 `erpc.toml`。
2. `run` 构建应用并在本机运行，您可以先用 curl 调用它。
3. `deploy` 为服务器构建应用并部署。任何需要付费的内容都会先显示其按小时计算的价格、您的余额以及 Credit 可维持的时长；在终端中，只有在得到确认后才开始。
4. `api` 从本机调用已部署应用的 API，而 API 仍位于服务器的 loopback 上。

`erpc worker check` 列出应用缺少的内容；有缺失时，`deploy` 会在购买任何东西之前拒绝。`erpc worker build` 只运行构建，可以在购买服务器之前看到构建失败。

## 选择模板

`erpc worker init` 只问一件事：要创建什么，用方向键选择。

- **HelloWorld** 是起步的模板。
- **HFT** 和 **pumpfun-bot** 目前都由 pumpfun 模板生成：一个从 ValidatorsDAO/solana-stream 移植的 pump.fun（PumpSwap）交易机器人。

`--template hft`（或 `helloworld`、`pumpfun-bot`，或模板 ID）会跳过选择，`--name` 为应用命名。没有名称时，应用以所选项命名（`hello-world`、`hft-bot`、`pumpfun-bot`）。已存在同名目录时，手动选择的人会得到 `-2`、`-3` 等后缀，而脚本（`--yes`、`--json`、无终端）会得到错误。无人可问且没有 `--template` 时，创建的是 HelloWorld。

交易机器人在运行前需要一个密钥和几项设置。其顺序如下：

```bash
erpc login                             # once per machine: the session in 1Password or the keychain
erpc worker init                       # pick with the arrow keys; makes ./<name> with erpc.toml
cd <name>
erpc worker key new                    # the trading key, in 1Password, the keychain or the keystore; prints the public key to fund
erpc worker secret put API_TOKEN --generate          # the bot's own API token, made for you and shown to nobody
erpc worker secret put ERPC_API_KEY --from-account   # the account's RPC key, read from your account (or SOLANA_RPC_ENDPOINT in [vars])
erpc worker check                      # what erpc.toml describes, and what is missing
erpc worker run --grpc --ip <IPV4>     # build and run here, with an hourly gRPC line letting this machine in
erpc worker run --check --grpc --ip <IPV4>   # the same, stopped once healthy
erpc worker deploy --new-vps --grpc    # a new hourly VPS and gRPC line, prices asked first
```

`init` 会为其创建的应用打印这一顺序。[密钥与 Secret](https://erpc.global/zh/doc/erpc-cli/worker-secrets/) 介绍密钥和 Secret 保存在哪里。

## 选择服务器

`deploy` 使用以下各项中的第一项：

1. 应用的 `[env.<name>]` 已指定的服务器；
2. `--vps <id>`，您账户中的 ERPC VPS；
3. `--new-vps`，按本机时区判断离本机最近的区域中最便宜的按小时计费的 VPS（`--region` 和 `--plan` 可另行选择）；
4. `--host <address>`，您自己的服务器：带有 systemd、nftables、`sudo` 以及位于 `/usr/bin/python3` 的 Python 3 的 Ubuntu 或 Debian，以 root 或可免密码使用 sudo 的用户登录；
5. 否则，在终端中显示您账户中正在运行的 VPS 列表，末尾是“start a new one”。

在您自己的服务器上，worker 的防火墙会丢弃除 SSH 和应用端口以外的所有入站连接，因此提供其他任何服务的服务器不适合使用。

## 在终端前无人操作时部署

脚本或 `erpc ai` 这样的智能体没有可以作答的终端。两个标志让它仍能完成工作：

```bash
erpc worker deploy hello-world --new-vps --yes                     # no key: all the way, unattended
erpc worker deploy pump-sniper --new-vps --grpc --yes --prepare    # a bot: everything but its key
erpc worker deploy pump-sniper                                     # then you, at a terminal: the key, and the start
```

- **`--yes`** 回答命令所指定的每个按小时计费产品（`--new-vps`、`--grpc`、`--shreds`、`--unlimited`）的价格确认。它也会回答首次登录一台 erpc 尚未设为仅限密钥登录的服务器（新服务器或以 `--vps <ID>` 指定的服务器）时的确认：erpc 使用服务器的密码登录一次，放入您的密钥，并关闭密码登录。既没有 `--yes` 也没有终端时，启动会在购买任何东西之前被拒绝，退出状态为 3。
- **在您的服务器中做选择仍需要有人操作**。请使用 `--new-vps`、`--vps <ID>` 或 `--host` 指定服务器。
- **`--prepare`** 完成既不需要交易密钥也不需要 Secret 的所有工作，不启动、不停止也不重启任何东西。下一次在终端中运行 `erpc worker deploy` 时，会交付密钥和 Secret 并启动应用。只有在有人在终端前操作时才会读取交易密钥。

## 将应用连接到 gRPC、Shreds 和 Unlimited RPC

ERPC 按小时计费的线路按地址接纳服务器，不接受令牌。每条线路在 `deploy` 上各有一个标志，用于为服务器启动该线路，应用以设置的形式获得其端点：

| 标志 | 线路 | 应用获得 |
| --- | --- | --- |
| `--grpc` | Geyser gRPC | `GRPC_ENDPOINT` |
| `--shreds` | Shreds（Shredstream gRPC） | `SHREDS_ENDPOINT` |
| `--unlimited` | 服务器所在区域的 Unlimited RPC | `SOLANA_SEND_RPC_ENDPOINT` |

- 除非 `--grpc-plan`、`--shreds-plan` 或 `--unlimited-plan` 指定方案，否则每条线路都是其系列中最便宜的一种。
- Unlimited RPC 运行在专用服务器上，设置最多需要 45 分钟。它支持 `sendTransaction` 和少量读取方法，不支持 `getProgramAccounts` 或 `getTokenAccountsByOwner`。其端点为纯 HTTP（无 TLS），只接纳白名单中的地址。

### 在本机运行时使用的线路

`erpc worker run` 也接受 `--grpc` 和 `--shreds`，用于在应用进入服务器之前，先在本机用真实数据试运行。

- **地址**：线路接纳本机的公网 IPv4 地址，由您通过 `--ip` 传入。erpc 不会自行查询该地址。
- **购买**：只有在终端中显示价格后，才会购买新线路。`run` 没有 `--yes`。
- **结束**：运行结束时，每条按小时计费的线路会在询问后结束。`--keep-lines` 保留这些线路，并继续计费。如果运行以其他方式结束（终端被关闭、第二次 Ctrl-C），线路会保留并继续计费。
- **数值**：应用在本机测得的数值包含从本机到线路的距离，因此会比在线路附近服务器上运行的同一应用慢。

## 后续步骤

- [导入应用](https://erpc.global/zh/doc/erpc-cli/worker-import/)：部署您已有的任何语言编写的应用。
- [密钥与 Secret](https://erpc.global/zh/doc/erpc-cli/worker-secrets/)：让交易密钥和 Secret 不落在服务器的磁盘上。
- [防火墙与 WAF](https://erpc.global/zh/doc/erpc-cli/worker-firewall-and-waf/)：将公开 API 放在 WAF 之后。
- [状态与计费](https://erpc.global/zh/doc/erpc-cli/worker-status-and-billing/)：监视应用，并查看按小时计算的费用。
