From 3bb961484cc6a8403d70794ab53beee7613e5ec7 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 28 Mar 2026 13:26:20 -0400 Subject: Use env file and break out setup-plex into two scripts --- .env.example | 12 ++++++++ .gitignore | 1 + README.md | 86 +++++++++++++++++++++++++++------------------------------ compose.yaml | 6 ++-- setup-docker.sh | 52 ++++++++++++++++++++++++++++++++++ setup-drive.sh | 46 ++++++++++++++++++++++++++++++ setup-plex.sh | 58 -------------------------------------- 7 files changed, 154 insertions(+), 107 deletions(-) create mode 100644 .env.example create mode 100644 .gitignore create mode 100755 setup-docker.sh create mode 100755 setup-drive.sh delete mode 100755 setup-plex.sh diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e49e983 --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# Timezone for the Plex container. +# See https://en.wikipedia.org/wiki/List_of_tz_database_time_zones +TZ=America/New_York + +# Plex claim token. Go to https://www.plex.tv/claim/ to get one. +PLEX_CLAIM= + +# Root of your media library +# +# Must match FSTAB_MOUNT_POINT in setup-drive.sh if you're using a separate +# drive. +MEDIA_DIR=/mnt/media diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/README.md b/README.md index ad70912..2ef26f6 100644 --- a/README.md +++ b/README.md @@ -1,85 +1,79 @@ # Plex Server Setup This repository contains scripts and configuration for setting up a Plex Media -Server. I run this on a Raspberry Pi 4B running Ubuntu. - -## Assumptions - -- Server running Ubuntu -- External storage device with UUID `2c882f17-50e7-4099-a305-693f4aa0113b` - - Comment out `ensure_fstab_entry` if you're not using a separate external - hard drive for your media or change the UUID as needed. If you comment it - out, we'll want to adjust the Plex data directory from `/mnt/media/Plex` to - wherever your data will be. -- Plex account and claim token +Server on Ubuntu. I run this on a Raspberry Pi 4B. + +* [Installation](#installation) + * [1. Copy the Files over to Your Server](#1.-copy-the-files-over-to-your-server) + * [2. SSH into Your Server](#2.-ssh-into-your-server) + * [3. Configure Your Environment](#3.-configure-your-environment) + * [Copy the Example Env File](#copy-the-example-env-file) + * [Fill in PLEX_CLAIM with Your Plex Claim Token](#fill-in-plex_claim-with-your-plex-claim-token) + * [4. Run the Installation Scripts](#4.-run-the-installation-scripts) + * [Install Docker](#install-docker) + * [Set up External Drive](#set-up-external-drive) + * [5. Start Plex](#5.-start-plex) ## Installation -### 1. Copy the files over to your server +### 1. Copy the Files over to Your Server ```sh -scp compose.yaml @:/home//compose.yaml -scp setup-plex.sh @:/home//setup-plex.sh +scp compose.yaml .env.example setup-docker.sh setup-drive.sh @:/home// ``` -### 2. SSH into your server +### 2. SSH into Your Server ```sh ssh @ ``` -### 3. Set the plex claim token +### 3. Configure Your Environment -Get a claim token at https://www.plex.tv/claim/ +#### Copy the Example Env File ```sh -sed 's|PLEX_CLAIM=|PLEX_CLAIM=your-actual-token-here|' compose.yaml > compose.yaml.tmp \ - && mv compose.yaml.tmp compose.yaml +cp .env.example .env ``` -### 4. Adjust hard-coded timezone if necessary +#### Fill in PLEX_CLAIM with Your Plex Claim Token + +[Get a claim token from Plex](https://www.plex.tv/claim/) + +### 4. Run the Installation Scripts -The timezone is hard-coded to 'America/New_York'. If that's not your timezone, -adjust it in the `compose.yaml` file. +#### Install Docker ```sh -sed 's|TZ=America/New_York|TZ=your-timezone-here|' compose.yaml > compose.yaml.tmp \ - && mv compose.yaml.tmp compose.yaml +chmod +x setup-docker.sh +sudo ./setup-docker.sh ``` -### 5. Run the installation script +#### Set up External Drive -This will set up a fstab entry for a external hard drive and install docker. +If you're using an external hard drive for your media, set up the fstab entry +for it: ```sh -chmod +x setup-plex.sh -sudo ./setup-plex.sh +chmod +x setup-drive.sh +sudo ./setup-drive.sh ``` -### 6. Ensure external drive is mounted +The defaults use my hard drive assumptions. To use different configuration, pass +the overrides before the command: + +```sh +FSTAB_UUID=your-uuid FSTAB_MOUNT_POINT=/mnt/data FSTAB_FS_TYPE=ext4 sudo -E ./setup-drive.sh +``` -After the fstab entry is added, ensure the drive is mounted for the external -hard drive. +After you've added the fstab entry, mount the drive: ```sh sudo mount -a ``` -### 7. Start Plex +### 5. Start Plex -```bash +```sh docker compose up --detach ``` - -## Configuration - -### Volume Mappings - -- `/mnt/media/Plex` - Media files on an external drive -- `/opt/plex-config` - Plex configuration and metadata on the server drive -- `/opt/plex-transcode` - Temporary transcoding files on the server drive - -### Ports - -Plex uses multiple ports for different features. All necessary ports are exposed -in the compose file. diff --git a/compose.yaml b/compose.yaml index 1b71442..8dfc48d 100644 --- a/compose.yaml +++ b/compose.yaml @@ -14,9 +14,9 @@ services: - "32413:32413/udp" - "32414:32414/udp" environment: - - TZ=America/New_York - - PLEX_CLAIM= + - TZ=${TZ} + - PLEX_CLAIM=${PLEX_CLAIM} volumes: - - /mnt/media/Plex:/data + - ${MEDIA_DIR}/Plex:/data - /opt/plex-config:/config - /opt/plex-transcode:/transcode diff --git a/setup-docker.sh b/setup-docker.sh new file mode 100755 index 0000000..b421872 --- /dev/null +++ b/setup-docker.sh @@ -0,0 +1,52 @@ +#!/bin/sh +set -e + +main() { + parse_args "$@" + install_docker_gpg_key + add_docker_repository + install_docker +} + +parse_args() { + case "${1:-}" in + -h | --help) usage ;; + esac +} + +usage() { + echo "Usage: sudo ./setup-docker.sh [-h]" + echo "" + echo "Installs Docker on Ubuntu." + exit 0 +} + +install_docker_gpg_key() { + sudo apt-get update + sudo apt-get install --yes ca-certificates curl + sudo install --mode=0755 --directory /etc/apt/keyrings + sudo curl --fail --silent --show-error --location \ + https://download.docker.com/linux/ubuntu/gpg \ + --output /etc/apt/keyrings/docker.asc + sudo chmod a+r /etc/apt/keyrings/docker.asc +} + +add_docker_repository() { + # shellcheck disable=SC1091 + echo \ + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ + $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | + sudo tee /etc/apt/sources.list.d/docker.list >/dev/null +} + +install_docker() { + sudo apt-get update + sudo apt-get install --yes \ + docker-ce \ + docker-ce-cli \ + containerd.io \ + docker-buildx-plugin \ + docker-compose-plugin +} + +main "$@" diff --git a/setup-drive.sh b/setup-drive.sh new file mode 100755 index 0000000..f678178 --- /dev/null +++ b/setup-drive.sh @@ -0,0 +1,46 @@ +#!/bin/sh +set -e + +main() { + parse_args "$@" + ensure_fstab_entry +} + +parse_args() { + case "${1:-}" in + -h | --help) usage ;; + esac +} + +usage() { + echo "Usage: sudo [-E] ./setup-drive.sh [-h]" + echo "" + echo "Adds an fstab entry for an external media drive." + echo "" + echo "Environment variables (all optional):" + echo " FSTAB_UUID UUID of the external media drive (default: 2c882f17-...)" + echo " FSTAB_MOUNT_POINT Mount point for the media drive (default: /mnt/media)" + echo " FSTAB_FS_TYPE Filesystem type of the drive (default: xfs)" + exit 0 +} + +ensure_fstab_entry() { + _fstab_uuid="${FSTAB_UUID:-2c882f17-50e7-4099-a305-693f4aa0113b}" + _fstab_mount_point="${FSTAB_MOUNT_POINT:-/mnt/media}" + _fstab_fs_type="${FSTAB_FS_TYPE:-xfs}" + _fstab_options="defaults" + _fstab_dump="0" + _fstab_pass="2" + _fstab_entry="UUID=${_fstab_uuid} ${_fstab_mount_point} ${_fstab_fs_type} ${_fstab_options} ${_fstab_dump} ${_fstab_pass}" + + if ! grep -q "UUID=${_fstab_uuid}" /etc/fstab; then + echo "Adding mount entry to /etc/fstab..." + echo "${_fstab_entry}" | sudo tee -a /etc/fstab >/dev/null + echo "Creating mount point if it doesn't exist..." + sudo mkdir -p "${_fstab_mount_point}" + else + echo "Mount entry already exists in /etc/fstab" + fi +} + +main "$@" diff --git a/setup-plex.sh b/setup-plex.sh deleted file mode 100755 index ee282b1..0000000 --- a/setup-plex.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/sh -set -e - -main() { - ensure_fstab_entry - install_docker_gpg_key - add_docker_repository - install_docker -} - -ensure_fstab_entry() { - _fstab_uuid="2c882f17-50e7-4099-a305-693f4aa0113b" - _fstab_mount_point="/mnt/media" - _fstab_fs_type="xfs" - _fstab_options="defaults" - _fstab_dump="0" - _fstab_pass="2" - _fstab_entry="UUID=${_fstab_uuid} ${_fstab_mount_point} ${_fstab_fs_type} ${_fstab_options} ${_fstab_dump} ${_fstab_pass}" - - if ! grep -q "UUID=${_fstab_uuid}" /etc/fstab; then - echo "Adding mount entry to /etc/fstab..." - echo "${_fstab_entry}" | sudo tee -a /etc/fstab >/dev/null - echo "Creating mount point if it doesn't exist..." - sudo mkdir -p "${_fstab_mount_point}" - else - echo "Mount entry already exists in /etc/fstab" - fi -} - -install_docker_gpg_key() { - sudo apt-get update - sudo apt-get install --yes ca-certificates curl - sudo install --mode=0755 --directory /etc/apt/keyrings - sudo curl --fail --silent --show-error --location \ - https://download.docker.com/linux/ubuntu/gpg \ - --output /etc/apt/keyrings/docker.asc - sudo chmod a+r /etc/apt/keyrings/docker.asc -} - -add_docker_repository() { - # shellcheck disable=SC1091 - echo \ - "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ - $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | - sudo tee /etc/apt/sources.list.d/docker.list >/dev/null -} - -install_docker() { - sudo apt-get update - sudo apt-get install --yes \ - docker-ce \ - docker-ce-cli \ - containerd.io \ - docker-buildx-plugin \ - docker-compose-plugin -} - -main "$@" -- cgit v1.2.3