搬瓦工部署 Phoenix Elixir 框架教程
Phoenix 是基于 Elixir 语言的高性能 Web 框架,运行在 Erlang 虚拟机(BEAM)之上。它以极低的延迟和出色的并发能力著称,支持实时功能(LiveView)和分布式部署。本文将介绍如何在搬瓦工 VPS 上部署 Phoenix 应用。购买搬瓦工 VPS 请参考 全部方案,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。
一、安装 Erlang 和 Elixir
apt update && apt upgrade -y
# 使用 asdf 版本管理器
apt install curl git build-essential autoconf m4 libncurses5-dev libwxgtk3.2-dev libwxgtk-webview3.2-dev libgl1-mesa-dev libglu1-mesa-dev libpng-dev libssh-dev unixodbc-dev xsltproc fop libxml2-utils -y
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0
echo '. "$HOME/.asdf/asdf.sh"' >> ~/.bashrc
source ~/.bashrc
# 安装 Erlang
asdf plugin add erlang
asdf install erlang 27.0
asdf global erlang 27.0
# 安装 Elixir
asdf plugin add elixir
asdf install elixir 1.17.0-otp-27
asdf global elixir 1.17.0-otp-27
elixir --version
二、安装 Node.js 和数据库
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
# 安装 PostgreSQL
apt install postgresql postgresql-contrib -y
sudo -u postgres psql -c "CREATE USER phoenix WITH PASSWORD 'your_password' CREATEDB;"
sudo -u postgres psql -c "CREATE DATABASE myapp_prod OWNER phoenix;"
三、部署 Phoenix 应用
cd /opt
git clone https://your-repo.git myapp
cd myapp
# 安装依赖
mix local.hex --force
mix local.rebar --force
MIX_ENV=prod mix deps.get
# 编译资源
MIX_ENV=prod mix assets.deploy
# 数据库迁移
MIX_ENV=prod mix ecto.migrate
3.1 配置环境变量
export SECRET_KEY_BASE=$(mix phx.gen.secret)
export DATABASE_URL="ecto://phoenix:your_password@localhost/myapp_prod"
export PHX_HOST="yourdomain.com"
export PORT=4000
四、构建 Release
MIX_ENV=prod mix release
# Release 产物在 _build/prod/rel/myapp/ 目录
五、创建 Systemd 服务
cat > /etc/systemd/system/phoenix.service << 'EOF'
[Unit]
Description=Phoenix Application
After=network.target postgresql.service
[Service]
Type=simple
User=root
WorkingDirectory=/opt/myapp
Environment=MIX_ENV=prod
Environment=PORT=4000
Environment=PHX_HOST=yourdomain.com
Environment=SECRET_KEY_BASE=your_secret_key_base
Environment=DATABASE_URL=ecto://phoenix:your_password@localhost/myapp_prod
ExecStart=/opt/myapp/_build/prod/rel/myapp/bin/myapp start
ExecStop=/opt/myapp/_build/prod/rel/myapp/bin/myapp stop
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable phoenix
systemctl start phoenix
六、Nginx 反向代理
apt install nginx -y
cat > /etc/nginx/sites-available/phoenix.conf << 'EOF'
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
EOF
ln -sf /etc/nginx/sites-available/phoenix.conf /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl restart nginx
七、SSL 证书
apt install certbot python3-certbot-nginx -y
certbot --nginx -d yourdomain.com
八、常见问题
LiveView WebSocket 连接失败
确认 Nginx 的 WebSocket 代理配置正确,包括 Upgrade 和 Connection 头的传递。
Release 启动失败
检查环境变量是否正确设置。使用 _build/prod/rel/myapp/bin/myapp remote 连接到运行中的应用进行调试。
总结
Phoenix 是高性能 Web 应用的优秀选择,Erlang VM 的并发能力和容错机制使其非常适合实时应用。在搬瓦工 VPS 上部署简单可靠。其他框架教程包括 Laravel、Rails、Actix Rust 等。购买搬瓦工 VPS 请访问 bwh81.net,使用优惠码 NODESEEK2026 可享受 6.77% 的折扣。