您的位置:首页 > 其它

风卷残云般部署好一个Phoenix应用,在Ubuntu服务器上

2016-08-21 00:00 393 查看
注意,编译项目文件时所使用的操作系统要与服务器一致,例如需要运行在Ubuntu64上的项目,就需要在Ubuntu64的环境里编译。

服务器上先安装好

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应用了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐