<![CDATA[Smart Stuff]]>https://petewasher.github.io/https://petewasher.github.io/favicon.pngSmart Stuffhttps://petewasher.github.io/Ghost 5.101Tue, 10 Dec 2024 20:35:00 GMT60<![CDATA[Paperless on TrueNAS]]>The Paperless Project is a way of reducing the amount of paper cluttering up your office. This post of my notes on getting it installed in a TrueNAS jail.

  1. Create the jail
  2. Assign storage by mounting a pool
  3. Log into the jail and start installing things...
pkg update
pkg upgrade
]]>
https://petewasher.github.io/paperless-on-truenas/600d9863c3e380000177cc99Sun, 24 Jan 2021 16:01:16 GMTThe Paperless Project is a way of reducing the amount of paper cluttering up your office. This post of my notes on getting it installed in a TrueNAS jail.

  1. Create the jail
  2. Assign storage by mounting a pool
  3. Log into the jail and start installing things...
pkg update
pkg upgrade

pkg install git
pkg install python3

ln -s /usr/local/bin/python3.7 /usr/local/bin/python

pkg install tesseract
pkg install ImageMagick7-7.0.10.24_2
pkg install unpaper
pg install poppler
pkg install optipng
pkg install py37-pip
pkg install postgresql96-server
pkg install py37-sqlite3

And now you can start installing Paperless itself, following the instructions...

mkdir /opt
cd /opt
git clone https://github.com/the-paperless-project/paperless.git
cd paperless
pip install -r requirements.txt
]]>
<![CDATA[Raspberry Pi DHT22 & DHT11 Sensor]]>https://petewasher.github.io/raspberry-pi-dht22-sensor/5ea9754ce5c5d400017520a1Thu, 14 May 2020 21:02:03 GMTSetupRaspberry Pi DHT22 & DHT11 Sensor

Assemble some bits:

  • Pi Zero W or really any flavour of Pi you have around.
  • SD Card
  • DHT22 Sensor and some cables. I bought this one.
  • Or a DHT11 sensor like this one. DHT11 has slightly less resolution and accuracy, but is cheaper. Wiring is the same.

I suggest you get one which comes prepackaged on a small PCB with the headers. Generally these come with the appropriate resistor already soldered on, and some wires to hook up and get you started quicker.

Grab the latest Raspbian from the RPi Foundation, or use a noobs image to
get your Pi Zero installed. Remember to add a empty file called ssh to
your /boot partition to enable SSH Server, and fill in a wpa_supplicant.conf
if you need to setup your wifi:

country=GB
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="WIFI_SSID"
scan_ssid=1
psk="WIFI_PASSWORD"
key_mgmt=WPA-PSK
}

More details here.

Now your pi should arrive on the network and you will be able to SSH into it
using ssh pi@raspberrypi. Do the updates etc., and we can get the DHT sensor
setup!

Cheap DHT22 Wifi Sensor

If you have the same sensor listed above, wire up your Pi as follows:

DHT22 Pin 1 -> 3.3V
DHT22 Pin 2 -> GPIO4
DHT22 Pin 3 -> GND

This will give your sensor some power, and allow us to communicate with it.
Next you'll need to get the appropriate libraries installed. Since we're
doing all this in Python, you can run the following commands to get going:

# Get the python tools we need
sudo apt install python3-dev python3-pip

# Ensure we have the latest modules
sudo python3 -m pip install --upgrade pip setuptools wheel

# Install the Adafruit module for talking to DHT sensors, and influxdb to talk to our Influx server:
sudo pip3 install Adafruit_DHT influxdb

Checkout the code from my github page. Here's the key bits:

# Create the device using GPIO 4
dhtDevice = adafruit_dht.DHT11(board.D4)

# Take a reading:
t = dhtDevice.temperature
h = dhtDevice.humidity

If you followed my other guide on how to setup your own InfluxDB server, you
can start pushing information into your database. Here's the important bits of the script to accomplish this, which you can adapt as you need:

from influxdb import InfluxDBClient
# Prep some JSON we will send to influxdb:
json_body = [
        {
            "measurement": "temperature",
            "tags": {
                "device": "kitchen",
                "type": "dht11"
                },
            "fields": {
               "deg_c": float(t),
               "humidity_percent": float(h)
            },
            "time": datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
            }
        ]

# Connect to the influxdb, setting all these settings as appropriate. 
client = InfluxDBClient('influxserver.lan', 8086, 'influx_username', 'influx_password', 'influx_database_name_here')
client.write_points(json_body)

I have this script scheduled on a Cron job to run every few minutes, logging temperature in my kitchen.

Hope this helps you get started!

]]>
<![CDATA[Digital Ocean Hosting for Mixed Web Services]]>https://petewasher.github.io/digital-ocean-hosting/5ea976bde5c5d400017520c7Wed, 29 Apr 2020 12:56:55 GMT

Digital Ocean is a great place for hosting your websites. You can do a lot of hosting for not much outlay, as long as you're careful!

Prep

There are no doubt infinitely more complicated and secure actions you might take to secure your services, so do your own research too! I am not a web security expert!

Set yourself up with a DNS name (try 123-Reg for example), and setup your nameserver to point at DigitalOcean. Then you can create your DNS configuration within DO.

Starting a Droplet

Machines in Digital Ocean are referred to as Droplets. We need to start a Droplet to start playing with the hosting. I suggest you start with an Ubuntu server droplet in the cheapest size for testing - you can always upgrade later, but if you start larger and it gets too expensive, it's more difficult to downgrade. As you add more services you may be forced to expand.

Stopping outside access

It's sensible to also setup a Firewall to protect your system whilst you work on it. You should only open the ports which you explicitly need. For a webserver, this is probably ports 80 and 433 only. Personally I keep a development firewall which acts on the corresponding tag. This opens port 22 to my home IP address, allowing me access. I remove this tag once I'm done developing.

You should also setup UFW within your server if you're using Ubuntu as I started with. To do that I followed this guide for using UFW to secure webservers. Again, I allow port 80 and 443, and SSH to my home IP

Other things to do

  1. Setup a SSH using an SSH Key pair so you can control access. Basics:
ssh-keygen -t rsa
ssh-copy-id you@your-server.com

Once you have this working OK, you should consider turning off password access to SSH (allowing access using the key only), and not permitting Root access at any point.

You can achieve this by editing your /etc/ssh/sshd.conf to add lines:

PermitRootLogin no
PasswordAuthentication no
  1. Setup monitoring which uses Digital Ocean alerts. This can email you when your droplet is using too much memory or CPU.

  2. Keep your server up to date! you'll need to keep track of security patches and check in often to see if your server needs rebooting to update.

Hosting

This site is a Ghost install, running on Docker. The network looks like this:

Digital Ocean Hosting for Mixed Web Services

I achieve this using three key components:

  • Ghost instance
  • MySQL database
  • Traefik proxy

Traefik also takes care of acquiring and maintaining a Let's Encrypt SSL certificate for the site. Easy! I followed the Traefik docs to suit my needs.

version: "3.3"
services:

  traefik:
    image: "traefik:v2.2"
    container_name: "traefik"
    restart: "always"
    ports:
      - "80:80"
      - "443:443"

    command:

      # General setup
      - "--global.sendAnonymousUsage"
      - "--log.filePath=/var/log/traefik.log"
      - "--accesslog=true"
      - "--accesslog.filePath=/var/log/access.log"
      - "--accesslog.bufferingsize=100"

      # Provide a dashboard - but this is protected by middleware  
      - "--api.dashboard=true"

      # Use the docker provider, but don't expose by default
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--providers.file.directory=/configuration"
      - "--providers.file.filename=dynamic_conf.toml"
      - "--providers.file.watch=true"

      # Create a http and https entrypoint
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"

      # We'll store the certs in a file acme.json which we mount below
      - "--certificatesresolvers.mytlschallenge.acme.tlschallenge=true"
      - "--certificatesresolvers.mytlschallenge.acme.email=you@yourhost.net"
      - "--certificatesresolvers.mytlschallenge.acme.storage=/letsencrypt/acme.json"

    # Containers to be shared need to be on this network 
    # so Traefik can route to them. This allows you to segregate 
    # your databases etc onto a network Traefik cannot see. 
    networks:
      - web

    volumes:
      - logs:/var/log
      - "./letsencrypt:/letsencrypt"
      - "./configuration:/configuration"
      # Mounting the docker socket allows us to pick up on containers
      # being created, and automatically start serving them. 
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
volumes:
  logs: {}

networks:
  web:
     external: true

This then works with a toml file which configures more of our server. This needs to go in the configuration folder. You'll need to go through and setup the various parts of this file to suit your personal situation. Change the Host and ipWhitelist as you require. The ipWhitelist restricts access to the dashboard to your home IP.

[http]

  [http.routers.my-api]
    entryPoints = ["websecure"]
    rule = "Host(`yourserver.net`)"
    service = "api@internal"
    middlewares = ["secured"]
    [http.routers.my-api.tls]
      options = "default"
      certResolver = "mytlschallenge"

  [http.middlewares]
 
    [http.middlewares.secured.chain]
      middlewares = ["safe-ipwhitelist", "auth"]

    [http.middlewares.secureHeader.headers]
      frameDeny = true
      sslRedirect = true
      stsSeconds = 31536000
      stsPreload = true
      stsIncludeSubdomains = true
  
    [http.middlewares.auth.basicAuth]
      users = [
      "user1:some-special-password"
      ]

    [http.middlewares.safe-ipwhitelist.ipWhiteList]
      sourceRange = ["192.168.1.1"]

[tls]

  [tls.options]
    [tls.options.default]
      minVersion = "VersionTLS12"
      cipherSuites = [
        "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
        "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
        "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305",
        "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
        "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
      ]
      sniStrict = true

And Ghost is done as follows:

version: "3"

services:
   ghost:
     image: ghost:3.14-alpine
     volumes:
        - ./volumes/ghost:/var/lib/ghost/content
     logging:
        driver: "json-file"
        options:
            max-file: "5"
            max-size: "10m"
     networks:
        - web 
        - ghost_back 
     restart: always
     labels:
        - "traefik.enable=true"
        - "traefik.docker.network=web"
        - "traefik.http.middlewares.yourwebsite-https.redirectscheme.scheme=https"

        # Handle HTTP traffic by redirecting to the HTTPS endpoint
        - "traefik.http.routers.yourwebsite-http.entrypoints=web"
        - "traefik.http.routers.yourwebsite-http.rule=Host(`yourwebsite.net`, `www.yourwebsite.net`)"
        - "traefik.http.routers.yourwebsite-http.middlewares=yourwebsite-https@docker"
        
        # Switch these lines to restrict access to the whitelist, or use in production
        - "traefik.http.routers.yourwebsite.middlewares=secureHeader@file"
        #- "traefik.http.routers.yourwebsite.middlewares=safe-ipwhitelist@file"
        
        # This router handles https traffic:
        - "traefik.http.routers.yourwebsite.entrypoints=websecure"
        - "traefik.http.routers.yourwebsite.rule=Host(`yourwebsite.net`, `www.yourwebsite.net`)"
        - "traefik.http.routers.yourwebsite.tls=true"
        - "traefik.http.routers.yourwebsite.tls.options=default"
        - "traefik.http.routers.yourwebsite.tls.certresolver=mytlschallenge"
     environment:
       database__client: mysql
       database__connection__host: ghost_mysql
       database__connection__user: ghost
       database__connection__password: ghostdbpass
       database__connection__database: ghostdb_yourwebsite_net
       url: https://www.yourwebsite.net
     container_name: ghost_yourwebsite_net 
networks:
  # The ghost backbone handles the connection to the 
  # database
  ghost_back:
     external: true
  
  # The web service allows Traefik to see us 
  web:
     external: true

Then it's just a case of bringing everything up using docker-compose up!

]]>