Run Deno apps via Systemd on Linux
Tags: devops
Published: 2020-04-11 06:10:28

Last year around summertime I got to play around with [Deno][0] and it was lots of fun 😀 I mostly made simple server scripts to serve up static sites. The most Deno code I wrote was rewriting my [dotfiles][d] from [Ruby][r] to Typescript.

It was interesting and soon I would like to try out more things so this post is for myself and anyone else out there who wants to deploy Deno on a traditional Linux distribution like Ubuntu or some other Linux distro.

> Heads up as of this writing, the current version of Deno is v0.40.0. The project is not yet considered production-ready so keep that in mind when you want to expose your app to the internet. If you are receiving traffic on the internet there might be bugs that people could exploit.

## What is Deno?

Deno is a new Typescript / Javascript runtime developed by Ryan Dahl and others. There are several interesting things with the project but a few that stick out are; the security model is different where you have to explicitly allow things like file system access, networking, etc. Deno scripts are written in Typescript. You can read more about [Deno](https://deno.land) on their website. YouTube has several talks on the subject when you search for [deno js](https://www.youtube.com/results?search_query=deno+js). Searching for just `deno` might not work since a rapper uses that name 😜

In this setup, I will just assume you are using some proxy server and will only focus on the systemd specific bits. Depending on what kind of application you are running you might want to consult the full systemd [documentation][2]. I also recommend running the app as a dedicated user this way you can restrict it even more (please don't use root 😅). 

## Installing Deno

Before we actually move on to installing Deno, let's get curl and unzip. `curl` is used to get the install script and `unzip` is used when deno is downloading dependencies (I think 🙃).

```
sudo apt-get update
sudo apt-get install -y curl unzip
```

The easiest way to get Deno on your server is the official install script

```
curl -fsSL https://deno.land/x/install/install.sh | sh
```

## Adding the Service file

Great, now that you have Deno in place, you need to tell systemd about the application. So first create the service file we will be using, just replace `my-cool-thing` with your app name.

```
sudo touch /etc/systemd/system/my-cool-thing.service
```

Use this template to set the values (change out the relevant fields or delete)

```
[Unit]
Description=my-cool-thing.io - doing cool things
Documentation=https://my-cool-thing.io/docs
After=network.target

[Service]
Type=simple
User=my-cool-thing-user
WorkingDirectory=/home/my-cool-thing-user/code
ExecStart=/home/my-cool-thing-user/.deno/bin/deno run --allow-read --allow-net my-cool-thing-main.ts
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

Then enable and start it

```
sudo systemctl enable my-cool-thing
sudo systemctl start my-cool-thing
```

Bam, now you have your service all wired up and ready. 

## Troubleshooting

If you end up with issues, check the logs 

```
systemctl status
```

One common mistake I have seen is forgetting to pass the right permissions, but those are easy to spot by manually running the start command

```
deno run my-cool-thing-main.ts
``` 

---

I’m [Alexander](https://alemayhu.com) and work as a Web Developer at [Scrimba](https://scrimba.com) - The mind-blowing way to learn how to code. This post is part of my personal weekly challenge of writing 256 words per week to improve my writing on technical topics.

I hope you found it useful. Thank you for reading.

[0]: https://deno.land/
[d]: https://github.com/scanf/dotfiles
[r]: https://www.ruby-lang.org/en/
[2]: https://www.freedesktop.org/wiki/Software/systemd/