服务器上先安装好
erlang
elixir
phoenix
postgreSQL
nginx
以下操作在本地进行
添加exrm依赖{:exrm, "~> 1.0"}到mix.exs文件
修改config/prod.exs
# Configures the endpoint
config :hello_phoenix, HelloPhoenix.Endpoint,
http: [port: 8888],
url: [host: "example.com"],
root: ".",
cache_static_manifest: "priv/static/manifest.json",
server: true,
version: Mix.Project.config[:version]
预编译静态资源
$ MIX_ENV=prod mix phoenix.digest
==> ranch (compile)
. . .
Check your digested files at 'priv/static'.
生成发布包
MIX_ENV=prod mix compile
MIX_ENV=prod mix release
测试发布
rel/hello_phoenix/bin/hello_phoenix console
没有错误的话,我们的应用会运行在http://localhost:8888/
发布
将发布包发送到服务器
$ scp -i ~/.ssh/id_rsa.pub rel/hello_phoenix-0.0.1.tar.gz ubuntu@hostname.com:/home/ubuntu
hello_phoenix-0.0.1.tar.gz 100% 18MB 80.0KB/s 03:48
登陆服务器,并解压
$ ssh -i ~/.ssh/id_rsa.pub ubuntu@hostname.com
$ sudo mkdir -p /app
$ sudo chown ubuntu:ubuntu /app
$ cd /app
$ tar xfz /home/ubuntu/hello_phoenix-0.0.1.tar.gz
以下在服务器进行
让应用在系统启动时启动
sudo vi /etc/init/hello_phoenix.conf
description "hello_phoenix"
## Uncomment the following two lines to run the
## application as www-data:www-data
#setuid www-data
#setgid www-data
start on runlevel [2345]
stop on runlevel [016]
expect stop
respawn
env MIX_ENV=prod
export MIX_ENV
## Uncomment the following two lines if we configured
## our port with an environment variable.
env PORT=8888
export PORT
## Add app HOME directory.
env HOME=/app
export HOME
pre-start exec /bin/sh /app/bin/hello_phoenix start
post-stop exec /bin/sh /app/bin/hello_phoenix stop
启动应用
sudo start hello_phoenix
配置nginx
$ sudo touch /etc/nginx/sites-available/hello_phoenix
$ sudo ln -s /etc/nginx/sites-available/hello_phoenix /etc/nginx/sites-enabled
$ sudo vi /etc/nginx/sites-available/hello_phoenix
upstream hello_phoenix {
server 127.0.0.1:8888;
}
# The following map statement is required
# if you plan to support channels. See https://www.nginx.com/blog/websocket-nginx/
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server{
listen 80;
server_name .hostname.com;
location / {
try_files $uri @proxy;
}
location @proxy {
include proxy_params;
proxy_redirect off;
proxy_pass http://hello_phoenix;
# The following two headers need to be set in order
# to keep the websocket connection open. Otherwise you'll see
# HTTP 400's being returned from websocket connections.
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
重启nginx
sudo service nginx restart
一切顺利的话,你可以在你设置的http://hostname.com/访问Phoenix应用了。