風捲殘雲般部署好一個Phoenix應用,在Ubuntu服務器上

注意,編譯項目文件時所使用的操做系統要與服務器一致,例如須要運行在Ubuntu64上的項目,就須要在Ubuntu64的環境裏編譯。nginx

服務器上先安裝好

  • 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應用了。

相關文章
相關標籤/搜索