Start the server automatically after reboot

This might be a basic question for most of you guys, so I apologize for asking it.

Short version, I was able to build an app with Phoenix using Cline (I can’t code anything myself) and have now been able to deploy it in my homelab as it is ready for use. The issue that I’m having is that the server will not automatically start when the VM is rebooted. Furthermore, every time I reboot the VM, I have to go through the process of setting (exporting?) the key and the salt before I can start the server.

How can I automate this so that the server is started automatically after booting and/or after crashing (it hasn’t done that yet)? I assume I have to write some sort of script to get this done via systemd, but curious to hear your thoughts of how this is normally done.

2 Likes

Hey there!

This is what I am using on a Ubuntu VPS with systemd:

[Unit]
Description=my-app

[Service]
Type=simple
User=www-data
Group=www-data
Restart=on-failure
Environment="HOST=example.com"
Environment="PORT=4040"
Environment="MIX_ENV=prod"
Environment="DATABASE_URL=ecto://postgres:password@127.0.0.1:5432/my_database"
Environment="SECRET_KEY_BASE=XXXXXXXXXXXXXXXXXXXXX"
WorkingDirectory=/var/www/my-app
ExecStartPre=/var/www/my-app/_build/prod/rel/my_app/bin/my_app eval "MyApp.Release.migrate"
ExecStart=/var/www/my-app/_build/prod/rel/my_app/bin/my_app start

[Install]
WantedBy=multi-user.target

My Phoenix project lives in /var/www/my-app/ and the systemd unit file is in /etc/systemd/system/my-app.service.

Note that I am following Deploying with Releases — Phoenix v1.7.18, so there’s a migration command in ExecStartPre.

After you create the systemd file, you’ll need to systemctl daemon-reload and then systemctl enable my-app and systemctl start my-app.

In my case, there’s a reverse proxy in front of the app, therefore it is listening on port 4040.

6 Likes

You shouldn’t feel sorry for asking for help.
As long as your question involves Elixir, you’re in a good place here!

6 Likes

Thanks guys! This is great!

@cblavier I think my reservation came from me not actually being a developer and just using AI to build everything. Glad to be learning though. This project taught me a lot about logic, how things work, etc. It has been very exciting to learn all of this.

1 Like

Updating to show what worked for me:

[Unit]
Description=my_app_name

[Service]
Type=simple
User=my_user
Group=my_group
Restart=on-failure

Environment="PORT=4000"
Environment="MIX_ENV=prod"
Environment="DATABASE_URL=your_database_url_here"
Environment="SECRET_KEY_BASE=your_secret_key_here"
Environment="LIVE_VIEW_SIGNING_SALT=your_salt_here"

WorkingDirectory=/home/user/my_app

ExecStartPre=/home/my_user/blurrg/_build/prod/rel/your_app_name/bin/your_app_name eval "YourApp.Release.migrate"
ExecStart=/home/my_user/blurrg/_build/prod/rel/your_app_name/bin/your_app_name start

[Install]
WantedBy=multi-user.target

This is a modified version, but it worked :slight_smile:

4 Likes