Phoenix in Production - SolarisJapan/lunaris-wiki GitHub Wiki
Useful things to know when running a Phoenix release on an Ubuntu Server
When deploying with edeliver, we are using releases. This is a little different from using Phoenix in development, most notably the absence of mix
.
Mix is a build tool that provides tasks for creating, compiling, and testing Elixir projects, managing its dependencies, and more.
The fact that you can run Phoenix using mix phx.server
is a convenience, but not what mix
is primarily for.
Other things that are different in production include: where to find and read your log files, how to open an interactive console and more.
Logfiles: erlang.log
By default Phoenix writes its logs in erlang.log.x
, usually located in the deployment folder, under var/logs
. Erlang logs have a built in log rotation, when the max-size of erlang.log.1
is reached, it will continue writing in erlang.log.2
, etc.
For example:
You deploy with edeliver and have this line in your .deliver/config
# ...
DELIVER_TO="/home/deploy/apps"
then your logs should be under /home/deploy/apps/your_app/var/logs/erlang.log.x
.
Since phoenix is using the erlang logger, you can set everything you can find on the run_erl manpage (warning: horrible Erlang documentation.)
One environment variable you should set is RUN_ERL_LOG_MAXSIZE
, since the default value is unreasonably low (100_000 Bytes).
How to access on an Ubuntu server
You can use less
to read the logs, watch the "live-stream", jump to line etc.
A few useful tips for using less
:
Shift + F
to enter "live stream mode", it jumps to the bottom of the file and automatically displays new lines when they are writtenControl + C
to end live stream, and any other modeq
to exitless
- A quick way to jump to the end is entering and immediately exiting live stream mode:
Shift + F
followed byControl + C
Q: How do I EXIT!!!!?
When you are in live stream mode, hit Control + C
, followed by q
to exit. If you are stuck, always use Control + C
first, to exit whatever mode you accidentally entered.
Command + F
for multiple files?)
Looking for something in the logs (where is In Ubuntu, you can use grep
(man page).
For example:
You know an error is occuring in a function get_me_something!
, you could do
grep "get_me_something!" apps/my_app/var/log/erlang.log.* -C 10
-C 10
means "the line matching my search +10 lines before, +10 lines after". You can also use -A 10
(10 lines A
fter) or -B 10
(10 lines B
efore). grep
also supports regular expressions with -E
.
Observe all log files at the same time
It is often inconvenient to open multiple log files before you find the current log file. On Ubuntu, there is a tool called multitail
You can open all log files with multitail apps/my_app/var/log/erlang.log.*
Interactive console
There are 2 ways of opening an interactive console (i.e. a console with your project's dependencies and modules).
Since we are deploying Phoenix with releases, there is no mix
! But we have executable binaries, providing remote_console
and console
. The binaries are, once again, in your deploy folder.
For Example:
You are deploying to /home/deploy/apps
then you can use
/home/deploy/apps/your_app/bin/your_app remote_console
# and
/home/deploy/apps/your_app/bin/your_app console
A remote console runs as a parallel process in an existing node, while a console will try to start a fresh node. If your app is running, you can only use remote_console
, console
will tell you that the node name is already taken (by your running app).
console
can be useful for debugging, if your app crashes on start for some reason, because it will give you the same error message as your app trying to start.
Both options will start an IEX session, and you can do everything you can do with iex -S mix
, but because it is running in production, all database interactions will be live! :fire: Don't create trash data while in a console session :fire:
edeliver after deployment
You can start
, stop
, restart
, and do other useful stuff with your app using edeliver.
One common cause of errors is forgetting to migrate your app after deployment:
mix edeliver migrate production