Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Aphoh/e0502daebf739c82da7a12a20f0c8b61 to your computer and use it in GitHub Desktop.
Save Aphoh/e0502daebf739c82da7a12a20f0c8b61 to your computer and use it in GitHub Desktop.
##### Replace 'example' anywhere with the name of your app and '*ec2ip*' with your ec2 instance ip
##### Set up your instance and make sure it's security group has ssh, http, and https open inbound and outbound
##### Don't forget to chmod 400 cert.pem
##### .deliver/config
APP="example"
BUILD_HOST="*ec2ip*"
BUILD_USER="elixir_builder"
BUILD_AT="/home/$BUILD_USER/edeliver/$APP/builds"
STAGING_HOSTS="*ec2ip*"
STAGING_USER="elixir_deployer"
DELIVER_TO="/home/elixir_deployer/$APP_staging"
# For *Phoenix* projects, symlink prod.secret.exs to our tmp source
pre_erlang_get_and_update_deps() {
local _local_secret_config_file="./config/prod.secret.exs"
if [ "$TARGET_MIX_ENV" = "prod" ]; then
status "Copying '$_local_secret_config_file' file to build host"
scp "$_local_secret_config_file" "$BUILD_USER@$BUILD_HOST:$BUILD_AT/config/prod.secret.exs"
fi
}
pre_erlang_clean_compile() {
status "Prparing assets with: brunch build and phoenix.digest"
__sync_remote "
# Runs the commands on the build host
set -e
cd '$BUILD_AT'
mkdir -p priv/static
npm install
node_modules/.bin/brunch build --production
app='$APP' MIX_ENV='$TARGET_MIX_ENV' $MIX_CMD phoenix.digest $SILENCE
"
}
##### mix.exs
def application do
...
applications: [:edeliver...
...
defp deps do
...
{:distillery, ">= 0.8.0", warn_missing: false},
{:edeliver, "~> 1.4.2"},
...
##### prod.secret.exs
config :example, Example.Repo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
hostname: "localhost",
database: "example_prod",
pool_size: 20
##### prod.exs
...
config :example, Example.Endpoint,
http: [port: 8080],
url: [host: "localhost", port: 8080],
cache_static_manifest: "priv/static/manifest.json",
root: ".",
server: true,
version: Mix.Project.config[:version]
...
config :phoenix, :serve_endpoints, true
...
mix do deps.get, compile
mix release.init
##### rel/config.exs
environment :prod do
set include_erts: false
set include_src: false
set cookie: :"..."
end
##### SSH into your ec2 instance
ssh -i cert.pem ubuntu@*ec2ip*
sudo adduser elixir_builder --disabled-password
sudo su - elixir_builder
mkdir .ssh
chmod 700 .ssh
##### Copy ~/.ssh/id_rsa.pub on your local machine and paste it into
vi .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
exit
##### Repeat the above steps for the user 'elixir_deployer'
##### Test ssh'ing works with
ssh elixir_deployer@*ec2ip*
exit
ssh elixir_builder@*ec2ip*
exit
##### Installing dependencies
ssh -i cert.pm ubuntu@*ec2ip*
wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb && sudo dpkg -i erlang-solutions_1.0_all.deb
sudo apt-get update
sudo apt-get install git esl-erlang elixir nginx postgresql postgresql-contrib build-essential
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install nodejs
sudo su - postgres
psql
CREATE DATABASE example_prod WITH OWNER postgres;
ALTER USER postgres WITH PASSWORD 'postgres';
\q
exit
##### Build deployment
mix edeliver build release staging --verbose
mix edeliver deploy release to staging --verbose
mix edeliver start staging
mix edeliver migrate staging
##### Test it's working
ssh elixir_deployer@*ec2ip*
curl localhost:8080
##### Configure Nginx
ssh -i cert.pm ubuntu@*ec2ip*
sudo vi /etc/nginx/sites-available/example_prod
##### Input the following config
upstream example {
server 127.0.0.1:8080;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server{
listen 80;
server_name *ec2ip*;
location / {
try_files $uri @proxy;
}
location @proxy {
include proxy_params;
proxy_redirect off;
proxy_pass http://example;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
##### Exit with :wq and run the following
sudo ln -s /etc/nginx/sites-available/example_prod /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
##### If the last command succeded, run
sudo service nginx restart
exit
##### Restart the server
mix edeliver restart staging
##### Test out your application from your local machine
curl *ec2ip*
##### To do a fresh update after incrementing the project version in mix.exs
mix edeliver update staging --to=<version>
mix edeliver restart staging
mix edeliver migrate staging
##### To do a live update
mix edeliver build upgrade --with=<old-version>
mix edeliver upgrade staging --to=<version>
mix edeliver migrate staging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment