SystemD for your Apps


I have been experimenting with I have been experimenting with for building apps and I have been really impressed with it (stay on the lookout for future posts). One thing I wanted to do was deploy it to a box in production. I wanted the server to stay up and for it to startup when the server was rebooted. There was a few things that stumped me and I was very happy when I was able to work them all out. Here is my final configuration file:

[Unit]
Description=hello_phoenix Service
After=network.target

[Service]
Type=simple
User=hello_phoenix
RemainAfterExit=yes
Environment=MIX_ENV=prod PORT=4000
WorkingDirectory=/var/www/hello_phoenix/
ExecStart=/var/www/hello_phoenix/rel/hello_phoenix/bin/hello_phoenix start
ExecStop=/var/www/hello_phoenix/rel/hello_phoenix/bin/hello_phoenix stop
Restart=on-failure
TimeoutSec=300

[Install]
WantedBy=multi-user.target

The things that got me where User= and Environment=. Originally I was trying to use su and set the environment variables in the command. This is ignored. The other thing that also caused me all sorts of problems was that I needed to set RemainAfterExit=yes as the start script actually executes another script and I didnt want systemd to kill the server after it had just started it. Hopefully this helps someone that is in a similar situation or was interested in systemd.

This comprehensive guide on systemd, https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files was very helpful to solving this problem.