FARM

The FARM stack consists of open-source software to get the backend and frontend up and running. Available as a one-click install, get NodeJS, Python, FastAPI, React, and MongoDB installed on your Droplet in less than a minute.

Software Included

Package Version License
NGINX 1.18.0 Custom
Gunicorn 20.1.0 MIT License
Uvicorn 0.21.1 BSD 3-Clause “New” or “Revised” License
Certbot 1.21.0 Apache 2
npm 9.5.1 Artistic License 2.0
pm2 5.3.0 GNU AGPL v3
React ^18.2.0 MIT License
FastAPI 0.95.0 MIT License
MongoDB 6.0.4 SSPL
Node.js 19.8.1 Custom

Creating an App using the Control Panel

Click the Deploy to DigitalOcean button to create a Droplet based on this 1-Click App. If you aren’t logged in, this link will prompt you to log in with your DigitalOcean account.

Deploy to DO

Creating an App using the API

In addition to creating a Droplet from the FARM 1-Click App using the control panel, you can also use the DigitalOcean API. As an example, to create a 4GB FARM Droplet in the SFO2 region, you can use the following curl command. You need to either save your API access token) to an environment variable or substitute it in the command below.

curl -X POST -H 'Content-Type: application/json' \
         -H 'Authorization: Bearer '$TOKEN'' -d \
        '{"name":"choose_a_name","region":"sfo2","size":"s-2vcpu-4gb","image": "farm"}' \
        "https://api.digitalocean.com/v2/droplets"

Getting Started After Deploying FARM

After your droplet is created, you can access your FARM stack by typing the droplet’s IP address in your browser:

FARM Welcome Page

If you see the Site can’t be reached or Nginx 502 Bad Gateway, give it 2-3 minutes and reload the page.

To connect to the local MongoDB console, use:

$ mongosh 127.0.0.1:27017 -u "admin" -p "<Your MongoDB password>" --authenticationDatabase "admin"

To connect your backend to the local MongoDB, use this connection string:

mongodb://admin:<Your MongoDB password>@127.0.0.1:27017

Your MongoDB password can be found on the welcome message of your FARM terminal.

Sample React application is served by PM2 as a farm user and is available by typing the droplet’s IP address in your browser.

Sample FastAPI application is also served by PM2 as a farm user and is deployed by default on port 8000. You can ping the default FastAPI application by running:

$ curl http://<your droplet ip>:8000
"Welcome to your FARM droplet!"

You can also view auto-generated Swagger documents for your FastAPI application by typing in your browser: &lt;your droplet ip&gt;:8000/docs

All deployed applications can be viewed by running:

$ su - farm -c "pm2 list"

Changing FastAPI port

FARM droplet comes with a simple FastAPI application hosted on port 8000. Below you can find how to change the port for your FastAPI application. In the example below we will change default 8000 to 9000.

First of all, SSH into your droplet:

$ ssh root@<your droplet IP>

Next, open the Gunicorn configuration file in your preferred text editor:

$ nano /etc/gunicorn.d/gunicorn.py

You should see a file which looks like this:

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ

def max_workers():
    return cpu_count() * 2 + 1

max_requests = 1000
worker_class = 'gevent'
workers = max_workers()

bind = '0.0.0.0:8000'

Change the port in the bind field to a desired value, in this example, it’s 9000:

bind = '0.0.0.0:9000'

Close and save the file.

Now, create a firewall rule for the new port:

$ ufw allow 9000/tcp

Finally, login as a farm user and restart FastAPI process sample_farm_api:

$ su farm
$ pm2 restart sample_farm_api

Curl your new port to test the results:

$ curl http://<your droplet ip>:9000
"Welcome to your FARM droplet!"

Configuring Nginx

After you have created your FARM droplet, it is highly recommended you configure an Nginx server block file for each site you plan to host. Doing so will make the default configuration the fallback, as intended, and will make it easier to manage changes when hosting multiple sites.

To do so, you’ll need to create two things for each domain: a new directory in /var/www for that domain’s content, and a new server block file in /etc/nginx/sites-available for that domain’s configuration. For a detailed walkthrough, you can follow How to Set Up Nginx Server Blocks.

Adding a domain name

A domain name allows others to access your website with an encrypted connection. If you intend to host a website on your FARM Droplet 1-Click.

FARM Droplet 1-Click comes with certbot installed, making it easier to enable HTTPS on your 1-Click.

First, ensure your domain points to the new droplet IP. If your DNS is managed by DigitalOcean, it should look like this:

FARM Domain Example

The DNS Host may be the same company you registered the domain with or another entity you designate. To connect your DNS hosting to DigitalOcean, check out this guide.

Once your A record is set up, you should configure nginx to host your new domain properly. Detailed instructions can be found in this article.

After you have configured the domain and nginx, you can run certbot to acquire HTTPS certificates for your droplet:

$ certbot –nginx -d <your domain> -d www.<your domain>

After you answer questions from certbot, your HTTPS setup is finished.

Use your domain in the web browser to access your FARM Droplet.

Droplet Summary

  • UFW firewall allows only SSH (port 22, rate limited), HTTP (port 80), and HTTPS (port 443) access.
  • The FARM Droplet comes with the farm user for managing sample applications and PM2.
  • The MongoDB is set with the admin user and SCRAM-SHA-1 authentication.
  • The MongoDB password for the admin user is located in /root/.digitalocean_password.
  • Sample React application is located at /home/farm/client.
  • Sample FastAPI application is located at /home/farm/server.
  • Sample React and FastAPI applications are served by pm2 from user farm.