账号注册与配置 #

注册 Heroku 账号 #

注册步骤 #

  1. 访问 Heroku 官网
  2. 点击右上角 “Sign up” 按钮
  3. 填写注册信息
text
┌─────────────────────────────────────┐
│         注册表单                     │
├─────────────────────────────────────┤
│  First name:     [____________]     │
│  Last name:      [____________]     │
│  Email:          [____________]     │
│  Role:           [下拉选择    ▼]     │
│  Country:        [下拉选择    ▼]     │
│  Password:       [____________]     │
│                                     │
│  [ ] I agree to the Terms of Service│
│                                     │
│        [  CREATE ACCOUNT  ]         │
└─────────────────────────────────────┘

邮箱验证 #

text
注册完成后:
    │
    ▼
收到验证邮件
    │
    ▼
点击验证链接
    │
    ▼
设置密码
    │
    ▼
完成注册

账号设置 #

登录后建议完成以下设置:

设置项 路径 说明
头像 Account Settings 上传个人头像
两步验证 Account Settings → Two-Factor Authentication 增强安全性
默认区域 Account Settings → Preferences 选择最近的区域
通知设置 Account Settings → Notifications 配置邮件通知

安装 Heroku CLI #

macOS 安装 #

bash
# 方式一:使用 Homebrew(推荐)
brew tap heroku/brew && brew install heroku

# 方式二:下载安装包
# 访问 https://devcenter.heroku.com/articles/heroku-cli 下载

Windows 安装 #

powershell
# 方式一:使用 winget
winget install --id Heroku.HerokuCLI

# 方式二:使用 Chocolatey
choco install heroku-cli

# 方式三:下载安装包
# 访问 https://devcenter.heroku.com/articles/heroku-cli 下载

Linux 安装 #

bash
# Ubuntu/Debian
curl https://cli-assets.heroku.com/install.sh | sh

# Arch Linux
yay -S heroku-cli

# Fedora
sudo dnf install heroku

验证安装 #

bash
# 查看版本
heroku --version
# 输出: heroku/9.x.x darwin-x64 node-v18.x.x

# 查看帮助
heroku --help

CLI 认证登录 #

交互式登录 #

bash
# 登录 Heroku
heroku login

# 按任意键打开浏览器
# 在浏览器中完成认证
# 返回终端显示登录成功
text
heroku: Press any key to open browser for login...
Opening browser to https://cli-auth.heroku.com/auth/cli/browser/...
Logging in... done
Logged in as your-email@example.com

浏览器登录 #

bash
# 直接在浏览器中登录
heroku login --browser

# 指定浏览器
heroku login --browser=chrome

API Token 登录 #

bash
# 使用 API Token 登录(适合 CI/CD)
heroku login --api-token <your-api-token>

# 或设置环境变量
export HEROKU_API_KEY=<your-api-token>
heroku whoami

查看登录状态 #

bash
# 查看当前登录用户
heroku whoami
# 输出: your-email@example.com

# 查看认证信息
heroku auth:whoami

退出登录 #

bash
# 退出当前账号
heroku logout

# 清除所有认证信息
heroku auth:logout

配置 SSH 密钥 #

生成 SSH 密钥 #

bash
# 生成新的 SSH 密钥
ssh-keygen -t ed25519 -C "your-email@example.com"

# 如果系统不支持 ed25519
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

添加密钥到 Heroku #

bash
# 查看本地 SSH 密钥
heroku keys

# 添加 SSH 密钥到 Heroku
heroku keys:add

# 指定密钥文件
heroku keys:add ~/.ssh/id_ed25519.pub

验证 SSH 连接 #

bash
# 测试 SSH 连接
ssh -T git@heroku.com

# 成功输出
# Hi your-email! You've been authenticated successfully.

管理 SSH 密钥 #

bash
# 列出所有密钥
heroku keys

# 删除密钥
heroku keys:remove your-email@example.com

# 清除所有密钥
heroku keys:clear

配置 Git #

安装 Git #

bash
# macOS
brew install git

# Ubuntu/Debian
sudo apt install git

# Windows
# 从 https://git-scm.com 下载安装

Git 基础配置 #

bash
# 设置用户名
git config --global user.name "Your Name"

# 设置邮箱
git config --global user.email "your-email@example.com"

# 查看配置
git config --list

初始化项目 #

bash
# 创建新项目
mkdir myapp && cd myapp
git init

# 创建 .gitignore
cat > .gitignore << 'EOF'
node_modules/
.env
.DS_Store
*.log
EOF

# 初始提交
git add .
git commit -m "Initial commit"

配置 Netrc #

Heroku CLI 使用 .netrc 文件存储认证信息:

bash
# 查看 netrc 文件
cat ~/.netrc

# 内容示例
machine api.heroku.com
  login your-email@example.com
  password xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
machine git.heroku.com
  login your-email@example.com
  password xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

常见问题 #

登录失败 #

bash
# 清除认证缓存
heroku logout

# 重新登录
heroku login

# 如果仍然失败,尝试清除 netrc
rm ~/.netrc
heroku login

SSH 连接问题 #

bash
# 检查 SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# 测试连接
ssh -vT git@heroku.com

CLI 更新 #

bash
# Homebrew 安装的
brew upgrade heroku

# 其他方式安装的
heroku update

代理设置 #

bash
# 设置 HTTP 代理
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080

# Git 代理设置
git config --global http.proxy http://proxy.example.com:8080

账号安全 #

启用两步验证 #

  1. 登录 Heroku Dashboard
  2. 进入 Account Settings
  3. 点击 Two-Factor Authentication
  4. 选择验证方式:
    • Authenticator App(推荐)
    • SMS

API Token 管理 #

bash
# 创建 API Token
# 访问 https://dashboard.heroku.com/account/applications

# 创建授权
heroku authorizations:create

# 列出授权
heroku authorizations

# 撤销授权
heroku authorizations:revoke <authorization-id>

安全最佳实践 #

实践 说明
使用强密码 至少 12 位,包含大小写字母、数字、符号
启用 2FA 增强账号安全性
定期更换 Token 定期更新 API Token
限制 Token 权限 为不同用途创建不同权限的 Token
审计日志 定期检查账号活动日志

下一步 #

环境配置完成后,接下来学习 第一个应用部署 部署你的第一个 Heroku 应用!

最后更新:2026-03-28