Ubuntu 16.04: set systemd for meteor service

#1

Hi, i’m have a vps with ubuntu 16.04 and i following this guide for install nginx + meteor.

https://www.digitalocean.com/community/tutorials/how-to-deploy-a-meteor-js-application-on-ubuntu-14-04-with-nginx

The problem is that guide is for ubuntu 14.04 and use startup. In ubuntu 16.04 there is systemd.
How can i translate this startup’s code in sistemd?

upstart service file at /etc/init/todos.conf

description "Meteor.js (NodeJS) application"
author “Daniel Speichert daniel@speichert.pro

When to start the service

start on started mongodb and runlevel [2345]

When to stop the service

stop on shutdown

Automatically restart process if crashed

respawn
respawn limit 10 5

we don’t use buil-in log because we use a script below

console log

drop root proviliges and switch to mymetorapp user

setuid todos
setgid todos

script
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export NODE_PATH=/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript
# set to home directory of the user Meteor will be running as
export PWD=/home/todos
export HOME=/home/todos
# leave as 127.0.0.1 for security
export BIND_IP=127.0.0.1
# the port nginx is proxying requests to
export PORT=8080
# this allows Meteor to figure out correct IP address of visitors
export HTTP_FORWARDED_COUNT=1
# MongoDB connection string using todos as database name
export MONGO_URL=mongodb://localhost:27017/todos
# The domain name as configured previously as server_name in nginx
export ROOT_URL=https://todos.net
# optional JSON config - the contents of file specified by passing “–settings” parameter to meteor command in development mode
export METEOR_SETTINGS=’{ “somesetting”: “someval”, “public”: { “othersetting”: “anothervalue” } }’
# this is optional: http://docs.meteor.com/#email
# commented out will default to no email being sent
# you must register with MailGun to have a username and password there
# export MAIL_URL=smtp://postmaster@mymetorapp.net:password123@smtp.mailgun.org
# alternatively install “apt-get install default-mta” and uncomment:
# export MAIL_URL=smtp://localhost
exec node /home/todos/bundle/main.js >> /home/todos/todos.log
end script

#2

Hi,
maybe this will help:

3 Likes
#3

The problem I have with that outline is that it is unrealistic to expect JSON to be hardcoded in METEOR_SETTINGS.

Previously I had used JSAWK to read in settings files, but that is no longer possible, and running an ExecStartPre script doesn’t allow for setting the env vars for the correct process,

Any other great ideas?

#4

As an afterthought was thinking that I could execute a script to setup an EnvironmentFile which can them be imported in the systemd unit file. Trying that option myself now, and it seems to work a charm.

#5

I am attempting to do the same thing, after migrating some Meteor 1.3.4.1 apps on Ubuntu 14.04 to Meteor 1.8.0.2 on Ubuntu 18.04.
Does anyone have a systemd equivalent working for the init file in the original question?

#6

This is mine /etc/systemd/system/SERVICE-NAME.service file:

[Service]
ExecStart=/usr/bin/node /PATH/TO/THE/bundle/main.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=LINUX_USERNAME
User=LINUX_USERNAME
Group=LINUX_USERNAME
Environment=NODE_ENV=production
Environment=PWD=/PATH/TO/THE/bundle
Environment=PORT=THE-RIGHT-PORT-NUMBER
Environment=HTTP_FORWARDED_COUNT=1
Environment=MONGO_URL=mongodb://USER:PASSWORD@xxx.xxx.xxx.xxx:27017/DATABASE-NAME
#Environment=MONGO_OPLOG_URL=mongodb://xxx.xxx.xxx.xxx:27017/local
Environment=ROOT_URL=http://DOMAIN-NAME
Environment='METEOR_SETTINGS={"someSetting": "someValue"}'

[Install]
WantedBy=multi-user.target
1 Like
#7

Thanks. I spent time experimenting and this is very close to what I ended up with as well.
for Restart I chose on-failure, and i’ll look at adding NODE_ENV as you have,