This repository contains Ansible playbooks to prepare and deploy a Phoenix/Elixir server in production.
# 1. Clone the repository
git clone https://github.com/ImNotAVirus/ansible_deploy_ex deploy
cd deploy
# 2. Copy and configure example files
cp playbooks/inventory/hosts.example playbooks/inventory/hosts
cp playbooks/group_vars/all/all.yml.example playbooks/group_vars/all/all.yml
# Edit these files to match your application, server, domain, and SSH key
# 3. Run the Ansible playbooks (replace `3333` with the configured port in `ssh_port`)
ansible-playbook playbooks/setup.yml
ansible-playbook playbooks/deploy.yml -e ansible_port=3333
You can add a deploy alias in your mix.exs:
defp aliases do
[
# ...other aliases...
deploy: [
"cmd --cd deploy ansible-playbook playbooks/deploy.yml -e ansible_port=3333"
]
]
endWith this alias, simply run mix deploy to deploy your application in production (replace 3333 with your SSH port if needed).
All configuration is centralized in playbooks/group_vars/all/all.yml. Key variables:
| Variable | Default | Description |
|---|---|---|
app_name |
— | Application name (used for directories, services, etc.) |
domain |
— | Main domain name for Nginx and SSL |
admin_email |
— | Email for Certbot and admin notifications |
db_user |
deploy |
PostgreSQL user for the application |
db_password |
— | PostgreSQL password for db_user (TCP connections) |
ssh_port |
— | SSH port for server access |
app_port |
4000 |
Internal HTTP port the Phoenix app listens on |
certbot_domains |
— | List of domains/subdomains for SSL certificate |
redirect_www |
true |
Redirect www.domain to domain via 301 |
elixir_version |
— | Elixir version for the Docker builder (asdf format) |
erlang_version |
— | Erlang/OTP version for the Docker builder |
- Prepares the server with required packages and security best practices.
- Sets up a secure user, SSH access, and basic firewall/hardening.
- Installs and configures PostgreSQL for the application database.
- Installs and configures Nginx and SSL (via Certbot) for serving your application.
- Supports multiple domains/subdomains via
certbot_domains. - Optional www-to-apex redirect (controlled by
redirect_www).
- Builds your Phoenix/Elixir application for production.
- Deploys the release and environment files to the server.
- Runs database migrations automatically before starting the app.
- Restarts the app service.
base: Basic system preparation.harden_server: Server hardening and security.setup_postgres: PostgreSQL installation and database provisioning.setup_nginx: Nginx and Certbot installation/configuration.build_release: Phoenix release build (via Docker).deploy_release: Release deployment, migration, and service management.
This project is licensed under the MIT License.