Hosting PocketBase on a DigitalOcean Droplet
Tags: webdev, tutorial, opensource, cloud
Published: 2023-01-04 05:56:12

Recently, I have playing around with [PocketBase](https://pocketbase.io), an open-source backend. And I am super impressed. The reason why is because PocketBase provides a real-time database, user authentication with OAuth2, file storage, and a clean, sleek admin dashboard. All in **one single file!**

I am so impressed, this has become my go-to backend for any side project. It's amazing how quickly it can set you up. And hosting PocketBase is incredibly simple. In this tutorial, we will deploy PocketBase to a 4$ DigitalOcean droplet.

You don't need to spend any money at DigitalOcean to follow along this tutorial 😅. You can sign up at DigitalOcean using the below link, and you will receive free $200 credits. DigitalOcean won't charge your credit card until your credits are fully used. It also helps me out.

<a href="https://m.do.co/c/26e721cc5d22" target="_blank">Sign up & get $200 credits.</a>

Once you have created a DigitalOcean account using the below link. You will be directed to the dashboard. 

## Download PocketBase
Before, we create a droplet, let's download the PocketBase executable for Linux from <a href="https://pocketbase.io/docs" target="_blank">here</a>.

## Creating a Droplet
To get started, create a project. Name it whatever you want. Once you have a project.

1. Click on "Create Droplet"
2. Select the region which fits your app the best.
3. Select an OS image for the droplet. I am going with Ubuntu, but you can select the one you like.
4. Then select the configuration for your droplet. For now, even a $4 or $6 configuration will work just fine for a small to medium sized application. You can always update this configuration later.
5. For "Authentication Method", select "Password" and provide a safe password and make sure to note it down, as DigitalOcean will not show it again.
6. Set the hostname of the droplet to something you can easily remember.
7. Set the project to the one you just created earlier.
7. Click "Create Droplet".

**Congratulations!** You have setup a droplet on DigitalOcean. Before we upload PocketBase, it's a good idea to setup a domain for our new droplet.

## Setup your domain
You can use any domain registrar for this tutorial. All the below steps are generic and will apply to any registrar. Some options are [Dreamhost](https://dreamhost.com), [Namecheap](https://namecheap.com), [GoDaddy](https://godaddy.com).

### If you are using an apex domain (example.com)
1. Create your domain using a domain registrar.
2. Go the DNS settings of the domain.
3. Change the default Nameservers to the DigitalOcean Nameservers which are -
```
ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com
```
4. Go to DigitalOcean's Networking section.
5. Under "Domains", add your newly created domain under your project.
6. Under the domain settings, create a A record with the hostname `@` and redirect it to the IP address of your droplet.

**Congratulations!** Your domain is now linked to your DigitalOcean droplet.

### If you are using a subdomain (api.example.com)
1. Go under the DNS settings of the apex domain under which you want to create a subdomain.
2. Create a A record with the hostname `yoursubdomainname` and set the IPv4 address to the IPv4 address of your DigitalOcean droplet.
3. Go to DigitalOcean's Networking section.
4. Add the newly created subdomain.
5. Add a A record with the hostname as `@` and redirect it to the IP address of your DigitalOcean droplet.

**Congratulations!** Your subdomain is now linked to your DigitalOcean droplet.

## Creating a `systemd` file
In order to keep PocketBase running even after we disconnect from the server and allow it to start on it's own, it is essential to create systemd service file.

1. Create a `pocketbase.service` file using your favorite text editor.
2. Copy and paste this configuration provided by PocketBase. Be sure to change the domain to your own.

```
[Unit]
Description = pocketbase

[Service]
Type           = simple
User           = root
Group          = root
LimitNOFILE    = 4096
Restart        = always
RestartSec     = 5s
StandardOutput = append:/root/pb/errors.log
StandardError  = append:/root/pb/errors.log
ExecStart      = /root/pb/pocketbase serve --http="yourdomain.com:80" --https="yourdomain.com:443"

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

Congratulations! You created your Systemd file for PocketBase.

## Uploading PocketBase to droplet
Now that the boring setup stuff is out of the way, we can get to the fun part. Actually uploading the PocketBase executable to our droplet. For this we will be using the `scp` module. First, let's connect to our droplet.

1. Open your Terminal or Command Prompt.
2. Run `ssh root@YOUR_SERVER_IP`
3. You might have to enter the password you created earlier while creating the droplet.
4. Once you are connected, run `mkdir pb`. This will create a folder called `pb` at the root of our droplet.

Now we need to upload the PocketBase executable to this `pb` folder on our droplet. For this, open a new Terminal window.

1. Navigate to the folder where the PocketBase executable is stored.
2. Run `scp pocketbase root@YOUR_SERVER_IP:/root/pb`
3. You might have to enter your password and press Enter.
4. Once the executable is uploaded, we need to also upload the `pocketbase.service` file.
5. Navigate to the folder where the `pocketbase.service` file is stored.
6. Run `scp pocketbase.service root@YOUR_SERVER_IP:/lib/systemd/system`

With that, you have uploaded all the files necessary for running PocketBase. Now all that's left is to start the systemd service. Go back to the earlier terminal window which was connected to the droplet.

In there, we need to run 2 commands. The first one to enable the PocketBase service.

```
systemctl enable pocketbase.service
```
And the next one to start the service.

```
systemctl start pocketbase
```

**Congratulations!!** You just deployed PocketBase on a DigitalOcean droplet. And the best part is, PocketBase automatically generates an SSL certificate, so you don't have to worry about that. You can now access the admin UI of PocketBase using your domain. For example - `https://yourdomain.com/_/`

Happy coding! Thank you for reading.
If you had any problems, let me know in the comments. I'll try my best to solve them. See ya! 😎