Recently, I have playing around with PocketBase, 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.
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 here.
Creating a Droplet
To get started, create a project. Name it whatever you want. Once you have a project.
- Click on "Create Droplet"
- Select the region which fits your app the best.
- Select an OS image for the droplet. I am going with Ubuntu, but you can select the one you like.
- 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.
- For "Authentication Method", select "Password" and provide a safe password and make sure to note it down, as DigitalOcean will not show it again.
- Set the hostname of the droplet to something you can easily remember.
- Set the project to the one you just created earlier.
- 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, Namecheap, GoDaddy.
If you are using an apex domain (example.com)
- Create your domain using a domain registrar.
- Go the DNS settings of the domain.
- Change the default Nameservers to the DigitalOcean Nameservers which are -
ns1.digitalocean.com
ns2.digitalocean.com
ns3.digitalocean.com
- Go to DigitalOcean's Networking section.
- Under "Domains", add your newly created domain under your project.
- 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)
- Go under the DNS settings of the apex domain under which you want to create a subdomain.
- Create a A record with the hostname
yoursubdomainname
and set the IPv4 address to the IPv4 address of your DigitalOcean droplet. - Go to DigitalOcean's Networking section.
- Add the newly created subdomain.
- 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.
- Create a
pocketbase.service
file using your favorite text editor. - 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.
- Open your Terminal or Command Prompt.
- Run
ssh root@YOUR_SERVER_IP
- You might have to enter the password you created earlier while creating the droplet.
- Once you are connected, run
mkdir pb
. This will create a folder calledpb
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.
- Navigate to the folder where the PocketBase executable is stored.
- Run
scp pocketbase root@YOUR_SERVER_IP:/root/pb
- You might have to enter your password and press Enter.
- Once the executable is uploaded, we need to also upload the
pocketbase.service
file. - Navigate to the folder where the
pocketbase.service
file is stored. - 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! 😎
Top comments (2)
Thank you Manas. Very in-depth and clear. How to update PocketBase?
There isn't a simple solution available as of right now as PocketBase is still under dev. The best way to update is taking a backup of your pb_data, downloading a new version, and using your backup folder there instead.
For a more detailed guide, check out this GitHub issue.
Glad you liked it.