Categories
Connected Farm

FarmOS Raspberry Pi Docker Install with SSL.

farmOS Screenshot

*********Information on this page may be out of date, as farmOS has moved to version 2.0**********

See: https://farmer-eds-shed.com/farmos-version-2-raspberry-pi-install/ for current version


“FarmOS is a web-based application for farm management, planning, and record keeping. It is developed by a community of farmers, developers, researchers, and organizations with the aim of providing a standard platform for agricultural data collection and management.” farmos.org

I recently came across farmOS and decided to test it out for running my own farm, it is free open source software that can be hosted on your own hardware or hosted by an online subscription service. I’ve gone for the self hosted option with a Raspberry Pi as it is ideal as an always on low traffic web server typically only consuming 3-4 Watts of power.

There is not much information available for installing farmOs on Raspberry Pi but it is a Drupal webserver with Docker images available, the following instructions show the process I used to get up and running. First off I installed the 32Bit Pi OS Lite on a 240GB USB SSD drive attached to a Raspberry Pi 3 using the Raspberry Pi Imager.

There is lots of information available on installing Pi OS so I won’t cover it here. This build will also use Https: over SSL which requires a registered domain name and SSL certificates. I’ll be using the same domain name and certificates as my existing Home Assistant server, if you don’t have these I’d recommend looking at DuckDNS and Let’s Encrypt.

Once the OS install is complete the file system needs to be expanded to use the entire drive.

sudo raspi-config

>Advanced options
 >Expand File System
>Finish
 >Reboot now = y

Next the OS should be updated.

sudo apt update
sudo apt full-upgrade
sudo reboot

Install git, docker and docker-compose.

sudo apt install git
sudo apt install docker
sudo apt install docker-compose

Clone the farmOS repo from GitHub and build the docker image.

git clone https://github.com/farmOS/farmOS.git
cd farmOS/docker
sudo docker build -t farmos .Code language: PHP (php)

Now that the image has been built we need a docker-compose file.

nano docker-compose.ymlCode language: CSS (css)

Paste the following config and save. This will configure the volumes and environmental variables required for farmOS and also pull the latest image of Nginx Proxy and configure it for Https/SSL encryption which is required to use the farmOS offline field app.

version: '2'
services:
  www:
    image: farmos:latest
    volumes:
      - 'farmos:/var/www/html'
    expose:
      - '80'
    environment:
      XDEBUG_CONFIG: remote_host=172.17.0.1

  proxy:
    depends_on:
      - www
    image: nginx:latest
    volumes:
      - 'farmosnginx:/etc/nginx'
      - '/home/pi/shared:/etc/nginx/certs'
    ports:
      - '180:80'
      - '1443:443'
volumes:
  farmos:
  farmosnginx:Code language: JavaScript (javascript)

save and exit (ctrl-x)

Next we can try to start the docker containers, this will fail due to missing SSL certificates and some configuration issues, but it will create the required volumes and download most of the necessary files to them.

sudo docker-compose up -d

To stop any running containers.

sudo docker-compose down

Ngnix needs to be configured in one of the newly created volumes, you need root privileges to access the folders (sudo su) .

sudo su
cd /var/lib/docker/volumes/docker_farmosnginx/_data
nano nginx.confCode language: JavaScript (javascript)

Replace the contents of nginx.conf with the follwing and update example-domain.com with your domain name:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {

}

http {
  error_log /etc/nginx/error_logs/error_log.log warn;
  client_max_body_size 20m;

  server {
      listen 80;
      server_name example-domain.com;

      rewrite ^/(.*)$ https://$host$request_uri? permanent;
  }

  server {
    server_name example-domain.com;

    location / {
      proxy_pass http://www:80;

      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host:443;
      proxy_set_header X-Forwarded-Port 443;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Proto https;
    }

    listen 443 ssl;
    ssl_certificate /etc/nginx/certs/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/privkey.pem;
  }
}Code language: PHP (php)

Save and exit. (ctrl-x)

An error log file also needs to be created.

mkdir error_logs
touch error_log.logCode language: CSS (css)

The settings.php file also needs to be updated with your domain name.

cd /var/lib/docker/volumes/docker_farmos/_data/sites/default
nano settings.phpCode language: JavaScript (javascript)

Uncomment base url line (remove the #) and updated domain name with your domain name and port 1443

$base_url = 'http://example-domain.com:1443';Code language: PHP (php)

save and exit (ctrl-x)

Then exit root mode by typing exit

exitCode language: PHP (php)

Create a folder for SSL certificates.

sudo mkdir /home/pi/shared

My certificates are on a shared folder on another Raspberry Pi running Home Assistant so I will mount that folder to /home/pi/shared, you could always just copy the certs there if you’ve created them in a different way.

sudo mount -t cifs //HomeAssitantIPAddress/ssl /home/pi/shared -o username=yourusername,password=yourpasswordCode language: JavaScript (javascript)

Finally the docker containers should now work.

sudo docker-compose up -d

Once you forward the inbound port 1443 on your router to port 1443 on your raspberry pi you should be able to connect to your new webserver where installation can be completed.

On any browser type https://yourdomain:1443 and if all went well you will have the following screen, if not see troubleshooting at the end.

The installation from here is straight forward, just make sure to select SQLite as database as the others won’t work on 32Bit Pi OS. You also need to get a map API key from either Google maps or Mapbox, I went with Mapbox as the process was simpler.

Select the optional modules

At the end, you will get a failed to send email message, this is normal for a docker install and would require additional email server config to resolve but won’t effect operation of the site.

Click visit your new site.

Optional Script to simplify starting the server.

FarmOS should now be up and running, but the mounted shared folder from Home Assistant is not persistent. I decided to create a short script to make it simple to mount the folder and start farmOS in case of reboots or power cuts.

cd /home/pi
nano startfarmos.sh

Paste the following and modify for your details.

#!/bin/sh

sudo mount -t cifs //HomeAssistantIP/ssl /home/pi/shared -o username=yourusername,password=yourpassword
cd farmOS/docker
sudo docker-compose up -d
Code language: JavaScript (javascript)

Save and exit (ctrl-x).

Then make the script executable.

sudo chmod +x startfarmos.sh
Code language: CSS (css)

Then to run the script simply type the following from the home directory.

./startfarmos.sh

You could automate this script on start up with cron, but I left it to ensure that after a power cut Home Assistant is up and running first.

A similar script to stop farmOS and unmount the remote folder would look like:

nano stopfarmos.shCode language: CSS (css)
#!/bin/sh

sudo umount shared
cd farmOS/docker
sudo docker-compose down
Code language: JavaScript (javascript)
sudo chmod +x startfarmos.shCode language: CSS (css)

Troubleshooting.

If after installing you can’t access your farmOS webserver then there are 2 likely issues that will be returned by your web browser. Either the site is unreachable/connection refused or else SSL/certificate issues. It is important that you try to access your page by domain name as SSL won’t allow connection by IP address.

For site unreachable/connection refused issues with no SSL errors then either your domain name is not linked to your IP address, the router port forwarding is not configured correctly or Nginx docker container is not running. If using DuckDNS or other provider for domain name login to the web account and check your IP is correctly showing, if you already have another server running like Home Assistant and it is working correctly then its not domain name issue. Next check your router port forwarding is setup correctly and port 1443 is not conflicting with any other forwarded ports for other servers. To check Nginx is running use the following docker command.

sudo docker ps

You should see 2 containers running one for Nginx and one for farmOS. If not try running docker-compose in the foreground be removing the -d.

sudo docker-compose down
sudo docker-compose up

Without the -d, you will see the containers start and exit if there is an issue, likely problems are, Ngnix not being able to find certificate files or error log file/directory. Press Ctrl-C to exit the remaining running containers. Compare the locations in the config files above with where you have the certificate files, If you are not using the Home Assistant certificate files then generate new ones with Let’s Encrypt.

For SSL related issues check the error returned by your browser, try a different browser also, sometimes a better explanation of the issue may be visible in different browsers. Also check the certificates that the browser is finding are the correct ones, I’ve seen some routers present their own self signed certificates instead of the new generated domain ones, this is usually because you’ve left out :1443 from your address or a redirection. Make sure your domain name was added correctly to all of the necessary config files listed previously.

That’s it for now.

For instructions on how to use farmOS see https://farmos.org/guide/ and https://farmos.discourse.group/.

Any questions, feel free to ask in the comments or on the Facebook page.

Facebook Comments