---
title: "使用 erpc worker 將您的第一個應用部署到伺服器"
description: "使用 erpc worker 建立 HelloWorld 應用，在本機執行，再部署到 ERPC VPS 或您自己的伺服器。"
canonical: "https://erpc.global/zh-tw/doc/erpc-cli/worker-quickstart/"
locale: "zh-tw"
---

# 使用 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-tw/doc/erpc-cli/sign-in-and-keys/)）。
- **ERPC Credit**，用於新的按小時計費的 VPS。按小時計費的產品以事先購買的 ERPC Credit 支付，Credit 不足以支付的啟動會被拒絕。價格請見[價格](https://erpc.global/zh-tw/price/)與 [ERPC Web Dashboard](https://dashboard.erpc.global/zh-tw)。
- **本機上的 SSH 金鑰**：執行 `ssh-keygen -t ed25519`；若已有金鑰，則執行 `erpc config set ssh_key <path>.pub`。
- **為 Linux 構建的方法**。從 Mac 或 Windows 構建時，需要伺服器的 Rust 目標（`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-tw/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` 這類 Agent，沒有可供回答的終端機。兩個旗標讓它照樣能完成工作：

```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-tw/doc/erpc-cli/worker-import/)：部署您已有的應用，任何語言皆可。
- [金鑰與 Secret](https://erpc.global/zh-tw/doc/erpc-cli/worker-secrets/)：讓交易金鑰與 Secret 不落在伺服器的磁碟上。
- [防火牆與 WAF](https://erpc.global/zh-tw/doc/erpc-cli/worker-firewall-and-waf/)：將公開 API 放在 WAF 後方。
- [狀態與計費](https://erpc.global/zh-tw/doc/erpc-cli/worker-status-and-billing/)：監看應用，並查看其按小時計算的費用。
