安装与配置 #

一、环境要求 #

1.1 系统要求 #

Phoenix对系统有以下要求:

要求 版本
Elixir >= 1.14
Erlang/OTP >= 25
PostgreSQL >= 12 (推荐)

1.2 检查现有环境 #

bash
# 检查Elixir版本
elixir -v

# 检查Erlang版本
erl -version

# 检查Hex包管理器
mix local.hex

# 检查PostgreSQL
psql --version

二、安装Elixir #

2.1 macOS安装 #

bash
# 使用Homebrew安装
brew install elixir

# 或使用asdf版本管理器
brew install asdf

asdf plugin add erlang
asdf plugin add elixir

asdf install erlang 26.0
asdf install elixir 1.15.0

asdf global erlang 26.0
asdf global elixir 1.15.0

2.2 Linux安装 #

bash
# Ubuntu/Debian
sudo apt update
sudo apt install erlang elixir

# 或使用asdf
git clone https://github.com/asdf-vm/asdf.git ~/.asdf
. $HOME/.asdf/asdf.sh

asdf plugin add erlang
asdf plugin add elixir
asdf install erlang 26.0
asdf install elixir 1.15.0

2.3 Windows安装 #

powershell
# 使用Chocolatey
choco install elixir

# 或下载安装包
# https://elixir-lang.org/install.html#windows

2.4 Docker环境 #

bash
# 使用官方镜像
docker run -it elixir:1.15 bash

# 创建开发容器
FROM elixir:1.15

RUN mix local.hex --force && \
    mix local.rebar --force

WORKDIR /app

三、安装Phoenix #

3.1 安装Hex和Rebar #

bash
# 安装Hex包管理器
mix local.hex

# 安装Rebar构建工具
mix local.rebar

3.2 安装Phoenix归档 #

bash
# 安装Phoenix项目生成器
mix archive.install hex phx_new

# 验证安装
mix phx.new --version

3.3 安装数据库驱动 #

bash
# PostgreSQL (推荐)
mix archive.install hex phx_new

# Phoenix默认使用PostgreSQL
# 确保PostgreSQL已安装并运行

四、安装PostgreSQL #

4.1 macOS安装 #

bash
# 使用Homebrew安装
brew install postgresql@15

# 启动服务
brew services start postgresql@15

# 创建数据库用户
createuser -s postgres

4.2 Linux安装 #

bash
# Ubuntu/Debian
sudo apt install postgresql postgresql-contrib

# 启动服务
sudo systemctl start postgresql

# 创建用户
sudo -u postgres createuser -s $USER

4.3 Docker方式 #

bash
# 运行PostgreSQL容器
docker run --name phoenix-db \
  -e POSTGRES_PASSWORD=postgres \
  -p 5432:5432 \
  -d postgres:15

# 连接信息
# host: localhost
# port: 5432
# user: postgres
# password: postgres

五、创建Phoenix项目 #

5.1 创建基础项目 #

bash
# 创建完整项目
mix phx.new my_app

# 进入项目目录
cd my_app

# 安装依赖
mix deps.get

5.2 创建项目选项 #

bash
# 不安装依赖(稍后手动安装)
mix phx.new my_app --no-deps

# 不包含数据库
mix phx.new my_app --no-ecto

# 不包含LiveView
mix phx.new my_app --no-live

# 使用MySQL
mix phx.new my_app --database mysql

# 使用SQLite
mix phx.new my_app --database sqlite3

# 使用Tailwind CSS
mix phx.new my_app --css tailwind

# API项目
mix phx.new my_api --no-html --no-assets

5.3 项目创建输出 #

text
* creating my_app/config/config.exs
* creating my_app/config/dev.exs
* creating my_app/config/runtime.exs
* creating my_app/lib/my_app/application.ex
* creating my_app/lib/my_app.ex
* creating my_app/lib/my_app_web/controllers/page_controller.ex
* creating my_app/lib/my_app_web/endpoint.ex
...

Fetch and install dependencies? [Yn] Y
* running mix deps.get
* running mix assets.setup
* running mix assets.build

We are almost there! The following steps are missing:

    $ cd my_app

Configure your database in config/dev.exs and run:

    $ mix ecto.create

Start your Phoenix app with:

    $ mix phx.server

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phx.server

六、配置数据库 #

6.1 配置文件 #

elixir
# config/dev.exs
import Config

config :my_app, MyApp.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "my_app_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

6.2 创建数据库 #

bash
# 创建开发数据库
mix ecto.create

# 输出
# The database for MyApp.Repo has been created

6.3 数据库配置选项 #

elixir
# config/dev.exs

# PostgreSQL配置
config :my_app, MyApp.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "my_app_dev",
  port: 5432,
  pool_size: 10

# MySQL配置
config :my_app, MyApp.Repo,
  username: "root",
  password: "",
  hostname: "localhost",
  database: "my_app_dev",
  port: 3306

# SQLite配置
config :my_app, MyApp.Repo,
  database: "path/to/database.db"

七、启动服务器 #

7.1 启动开发服务器 #

bash
# 启动服务器
mix phx.server

# 输出
[info] Running MyAppWeb.Endpoint with cowboy 2.10.0 at 127.0.0.1:4000 (http)
[info] Access MyAppWeb.Endpoint at http://localhost:4000
[watch] build complete, watching for changes...

7.2 交互式启动 #

bash
# 在IEx中启动
iex -S mix phx.server

# 可以在控制台执行代码
iex> MyApp.Accounts.list_users()

7.3 访问应用 #

text
打开浏览器访问: http://localhost:4000
看到Phoenix欢迎页面表示安装成功

八、项目配置 #

8.1 环境配置文件 #

text
config/
├── config.exs      # 基础配置
├── dev.exs         # 开发环境配置
├── runtime.exs     # 运行时配置
├── test.exs        # 测试环境配置
└── prod.exs        # 生产环境配置(由runtime.exs替代)

8.2 基础配置 #

elixir
# config/config.exs
import Config

config :my_app,
  ecto_repos: [MyApp.Repo]

config :my_app, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres

config :my_app, MyAppWeb.Endpoint,
  url: [host: "localhost"],
  render_errors: [
    formats: [html: MyAppWeb.ErrorHTML, json: MyAppWeb.ErrorJSON],
    layout: false
  ],
  pubsub_server: MyApp.PubSub,
  live_view: [signing_salt: "your_signing_salt"]

config :logger, :console,
  format: "$time $metadata[$level] $message\n",
  metadata: [:request_id]

config :phoenix, :json_library, Jason

8.3 开发环境配置 #

elixir
# config/dev.exs
import Config

config :my_app, MyAppWeb.Endpoint,
  http: [ip: {127, 0, 0, 1}, port: 4000],
  check_origin: false,
  code_reloader: true,
  debug_errors: true,
  secret_key_base: "your_secret_key_base"

config :my_app, MyApp.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "my_app_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

config :logger, :console, format: "[$level] $message\n"

config :phoenix, :stacktrace_depth, 20

九、常用Mix命令 #

9.1 项目管理 #

bash
# 创建新项目
mix phx.new app_name

# 安装依赖
mix deps.get

# 更新依赖
mix deps.update --all

# 查看依赖
mix deps

# 编译项目
mix compile

9.2 数据库命令 #

bash
# 创建数据库
mix ecto.create

# 删除数据库
mix ecto.drop

# 运行迁移
mix ecto.migrate

# 回滚迁移
mix ecto.rollback

# 重置数据库
mix ecto.reset

# 生成迁移
mix ecto.gen.migration add_users_table

9.3 生成器命令 #

bash
# 生成HTML资源
mix phx.gen.html Accounts User users name:string email:string

# 生成JSON资源
mix phx.gen.json Accounts User users name:string email:string

# 生成LiveView资源
mix phx.gen.live Accounts User users name:string email:string

# 生成Context
mix phx.gen.context Accounts User users name:string email:string

# 生成控制器
mix phx.gen.controller PageController index show

9.4 服务器命令 #

bash
# 启动服务器
mix phx.server

# 指定端口
PORT=4001 mix phx.server

# 交互式服务器
iex -S mix phx.server

十、IDE配置 #

10.1 VS Code配置 #

json
// settings.json
{
  "elixirLS.dialyzerEnabled": true,
  "elixirLS.fetchDeps": true,
  "elixirLS.suggestSpecs": true,
  "editor.formatOnSave": true,
  "[elixir]": {
    "editor.defaultFormatter": "JakeBecker.elixir-ls"
  }
}

10.2 推荐扩展 #

扩展 功能
ElixirLS 语言服务器
Elixir Test 测试运行器
Elixir Formatter 代码格式化
vscode-elixir 语法高亮

10.3 IntelliJ IDEA配置 #

text
安装插件:
- Elixir
- Erlang

配置:
- Settings -> Languages & Frameworks -> Elixir
- 设置Mix path
- 设置Elixir SDK

十一、故障排除 #

11.1 常见问题 #

问题1: Hex安装失败

bash
# 手动安装Hex
mix local.hex --force

问题2: 数据库连接失败

bash
# 检查PostgreSQL状态
pg_isready

# 检查连接
psql -U postgres -h localhost

# 重置密码
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"

问题3: 端口被占用

bash
# 查找占用端口的进程
lsof -i :4000

# 杀死进程
kill -9 <PID>

# 或使用其他端口
PORT=4001 mix phx.server

11.2 清理和重置 #

bash
# 清理编译文件
mix clean

# 完全清理
mix clean --deps

# 重置依赖
rm -rf _build deps
mix deps.get

十二、总结 #

12.1 安装检查清单 #

检查项 命令
Elixir版本 elixir -v
Erlang版本 erl -version
Hex安装 mix local.hex
Phoenix安装 mix phx.new --version
PostgreSQL psql --version
项目创建 mix phx.new my_app
数据库创建 mix ecto.create
服务器启动 mix phx.server

12.2 下一步 #

环境搭建完成后,让我们创建 第一个应用,开始Phoenix开发之旅!

最后更新:2026-03-28