Add detailed comments to script and comprehensive README

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
28allday 2026-03-28 12:11:51 +00:00
parent 72072eb445
commit a2714aac1d
2 changed files with 245 additions and 31 deletions

170
README.md Normal file
View file

@ -0,0 +1,170 @@
# Plex Server Setup
One-script setup for a Plex Media Server on Ubuntu or Linux Mint. Handles drive mounting and Plex installation in a single run.
## Requirements
- **OS**: Ubuntu 20.04+ or Linux Mint 20+
- **Plex .deb file**: Downloaded from [plex.tv](https://www.plex.tv/media-server-downloads/) to `~/Downloads/`
- **A media drive**: Internal or external drive for storing your media
## Quick Start
1. **Download the Plex .deb** from [plex.tv/media-server-downloads](https://www.plex.tv/media-server-downloads/)
- Choose **Linux**
- Select **Ubuntu (16.04+) / Debian (8+)**`.deb` package
- Save to `~/Downloads/`
2. **Run the installer**:
```bash
git clone https://github.com/28allday/Plex-Server-Setup.git
cd Plex-Server-Setup
chmod +x plexint.sh
sudo ./plexint.sh
```
3. **Access Plex** at `http://<your-server-ip>:32400/web`
## What It Does
### Part A: Drive Mount Setup
Mounts your media drive permanently so it survives reboots.
1. Shows all available drives and partitions (`lsblk`)
2. Asks you to select a device (e.g. `/dev/sdb1`)
3. Detects the drive's UUID (unique identifier that won't change)
4. Asks for a mount point (e.g. `/mnt/media`)
5. Asks for the filesystem type (ext4, ntfs, xfs, exfat)
6. Adds the mount to `/etc/fstab` and mounts it immediately
**Why UUID?** Device paths like `/dev/sdb1` can change if you add or remove drives. UUIDs are permanent identifiers tied to the filesystem itself.
**Supported filesystems:** ext4, ntfs, xfs, exfat, btrfs, and any other type supported by your kernel.
### Part B: Plex Installation
1. Installs prerequisites (`curl`, `apt-transport-https`, `gnupg`)
2. Finds the Plex `.deb` file in `~/Downloads/`
3. Installs it with `dpkg` and resolves any missing dependencies
4. Enables Plex to start on boot (`systemctl enable`)
5. Starts Plex immediately
6. Opens port 32400 in UFW firewall
## After Installation
### Initial Plex Setup
1. Open `http://<server-ip>:32400/web` in a browser
2. Sign in with your Plex account (or create one)
3. Name your server
4. Add your media library — point it to the mount point you chose (e.g. `/mnt/media`)
5. Let Plex scan and organize your media
### Organise Your Media
For best results, organise your media like this:
```
/mnt/media/
├── Movies/
│ ├── Movie Name (2024)/
│ │ └── Movie Name (2024).mkv
│ └── ...
├── TV Shows/
│ ├── Show Name/
│ │ ├── Season 01/
│ │ │ ├── Show Name - S01E01.mkv
│ │ │ └── ...
│ │ └── ...
│ └── ...
└── Music/
├── Artist Name/
│ ├── Album Name/
│ │ ├── 01 - Track Name.flac
│ │ └── ...
│ └── ...
└── ...
```
## Files Modified
| Path | Purpose |
|------|---------|
| `/etc/fstab` | Drive mount entry added (one line) |
## Services
| Service | Port | Purpose |
|---------|------|---------|
| `plexmediaserver.service` | 32400 | Plex Media Server web interface and streaming |
## Troubleshooting
### Can't access Plex web interface
- Check Plex is running: `sudo systemctl status plexmediaserver`
- Check firewall: `sudo ufw status` — port 32400 should be ALLOW
- Try `http://localhost:32400/web` on the server itself first
### Drive not mounting after reboot
- Check fstab syntax: `cat /etc/fstab`
- Test it: `sudo mount -a` (mounts all fstab entries)
- Check the UUID is correct: `sudo blkid`
### Plex can't see files on the media drive
- Check permissions: `ls -la /mnt/media`
- Plex runs as the `plex` user — it needs read access to your media:
```bash
sudo chmod -R 755 /mnt/media
sudo chown -R plex:plex /mnt/media
```
Or add the plex user to your group:
```bash
sudo usermod -aG your-username plex
sudo systemctl restart plexmediaserver
```
### "No .deb file found" error
- Make sure the file is in `~/Downloads/` (not a subdirectory)
- The filename must start with `plexmediaserver` (e.g. `plexmediaserver_1.40.0.1234_amd64.deb`)
## Updating Plex
Download the new .deb from plex.tv and install it:
```bash
sudo dpkg -i ~/Downloads/plexmediaserver*.deb
sudo systemctl restart plexmediaserver
```
## Uninstalling
```bash
# Stop and remove Plex
sudo systemctl stop plexmediaserver
sudo systemctl disable plexmediaserver
sudo apt remove --purge plexmediaserver
# Remove firewall rule
sudo ufw delete allow 32400/tcp
# Remove the fstab entry (edit manually)
sudo nano /etc/fstab
# Delete the line this script added, then:
sudo umount /mnt/media
# Remove Plex data (WARNING: deletes all library metadata)
sudo rm -rf /var/lib/plexmediaserver
```
## Credits
- [Plex](https://www.plex.tv/) - Media server software
## License
This project is provided as-is.

View file

@ -1,21 +1,34 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# ==============================================================================
# Plex Media Server Installer with Drive Mount Setup
# #
# install_and_mount.sh # A two-in-one setup script for building a Plex media server on Ubuntu or
# Linux Mint. It handles the two things you always need to do when setting
# up a new media server:
# #
# This script does two main tasks in one go: # Part A: Mount your media drive permanently (via /etc/fstab)
# 1) Helps you select and mount a drive/partition at boot (via /etc/fstab). # - Shows available drives/partitions
# 2) Installs Plex Media Server from a local .deb file in your ~/Downloads folder, # - Detects UUID for reliable mounting (doesn't break if device order changes)
# and opens port 32400 via UFW. # - Adds the mount to /etc/fstab so it survives reboots
# #
# If the user selects 'exfat' as the file system, we do NOT install exfat-fuse or exfat-utils, # Part B: Install Plex Media Server
# assuming the Ubuntu-based system already supports exFAT natively (e.g., Ubuntu 20.04+). # - Installs from a .deb file you've downloaded to ~/Downloads/
# - Handles dependencies automatically
# - Enables the Plex service to start on boot
# - Opens port 32400 in the firewall (UFW)
# #
# Prerequisites:
# - Ubuntu 20.04+ or Linux Mint 20+
# - An external or internal drive for media storage
# - Plex .deb file downloaded to ~/Downloads/
# (download from https://www.plex.tv/media-server-downloads/)
# - Run with sudo: sudo ./plexint.sh
# ==============================================================================
set -e # Exit on any error set -e # Exit immediately if any command fails
############################################################################### # Root is required because this script modifies /etc/fstab (system mount
# 0. Root Check # config), installs packages with apt, and manages systemd services.
###############################################################################
if [[ $EUID -ne 0 ]]; then if [[ $EUID -ne 0 ]]; then
echo "ERROR: This script must be run with sudo (root privileges)." echo "ERROR: This script must be run with sudo (root privileges)."
exit 1 exit 1
@ -26,9 +39,16 @@ echo " Combined Setup: Drive Mount + Plex Installation"
echo "=======================================================" echo "======================================================="
echo echo
############################################################################### # ==================== Part A: Drive Mount Setup ====================
# 1. MOUNT DRIVE #
############################################################################### # Most media servers need a dedicated drive for storing movies, TV shows,
# music, etc. This section helps mount that drive permanently so it's
# available after every reboot.
#
# We use lsblk to show available drives, then ask the user to pick one.
# The mount is configured via /etc/fstab using the drive's UUID (not the
# device path like /dev/sdb1) because device paths can change if you add
# or remove drives, but UUIDs are permanent identifiers.
echo "-----------------------------" echo "-----------------------------"
echo " Part A: Drive Mount Setup" echo " Part A: Drive Mount Setup"
echo "-----------------------------" echo "-----------------------------"
@ -36,7 +56,8 @@ echo
echo "Below are the available drives/partitions on your system:" echo "Below are the available drives/partitions on your system:"
echo echo
# List block devices # Show drives with their sizes, types, current mount points, and filesystem
# types. The -p flag shows full device paths (e.g. /dev/sdb1 not just sdb1).
lsblk -p -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE lsblk -p -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE
echo echo
@ -49,7 +70,10 @@ if [[ ! -b "$DEVICE_PATH" ]]; then
echo "Aborting drive-mount portion." echo "Aborting drive-mount portion."
echo echo
else else
# Attempt to get UUID # Try to get the drive's UUID using blkid. UUID is preferred over device
# paths in /etc/fstab because /dev/sdX names can change between reboots
# (e.g. if you plug in a USB drive before the media drive). UUIDs are
# unique identifiers burned into the filesystem and never change.
UUID_FOUND=$(blkid -s UUID -o value "${DEVICE_PATH}" 2>/dev/null || true) UUID_FOUND=$(blkid -s UUID -o value "${DEVICE_PATH}" 2>/dev/null || true)
if [[ -z "$UUID_FOUND" ]]; then if [[ -z "$UUID_FOUND" ]]; then
echo "WARNING: Could not detect a UUID for '${DEVICE_PATH}'." echo "WARNING: Could not detect a UUID for '${DEVICE_PATH}'."
@ -76,7 +100,10 @@ else
echo "No additional exFAT packages will be installed." echo "No additional exFAT packages will be installed."
fi fi
# We'll keep it simple: use 'defaults' for mount options, pass=2 # Build the fstab entry. "defaults" enables standard mount options
# (read/write, auto-mount, etc.). The "0 2" at the end means:
# 0 = don't include in dump backups
# 2 = fsck checks this drive second (after the root filesystem)
MOUNT_OPTS="defaults" MOUNT_OPTS="defaults"
FSTAB_LINE="${DEVICE_IDENTIFIER} ${MOUNT_POINT} ${FS_TYPE} ${MOUNT_OPTS} 0 2" FSTAB_LINE="${DEVICE_IDENTIFIER} ${MOUNT_POINT} ${FS_TYPE} ${MOUNT_OPTS} 0 2"
@ -98,9 +125,15 @@ else
fi fi
fi fi
############################################################################### # ==================== Part B: Plex Media Server Installation ====================
# 2. INSTALL PLEX #
############################################################################### # Plex Media Server is a media streaming server that organises your movies,
# TV shows, music, and photos and streams them to any device on your network
# (or remotely over the internet).
#
# Plex distributes Linux packages as .deb files (for Debian/Ubuntu/Mint).
# The user must download it manually from plex.tv because it requires
# accepting their terms of service. We look for it in ~/Downloads/.
echo echo
echo "-----------------------------" echo "-----------------------------"
echo " Part B: Plex Installation" echo " Part B: Plex Installation"
@ -110,8 +143,9 @@ echo
read -rp "Would you like to install Plex Media Server now? (y/n): " INSTALL_PLEX read -rp "Would you like to install Plex Media Server now? (y/n): " INSTALL_PLEX
if [[ "$INSTALL_PLEX" =~ ^[Yy]$ ]]; then if [[ "$INSTALL_PLEX" =~ ^[Yy]$ ]]; then
# 2.1. Identify the original user for the Downloads folder # Figure out the real user's home directory. When running with sudo,
# (so we can find the .deb in ~<user>/Downloads) # $USER is "root" but $SUDO_USER is the person who typed "sudo".
# We need their Downloads folder, not root's.
if [[ -n "$SUDO_USER" ]]; then if [[ -n "$SUDO_USER" ]]; then
ORIGINAL_USER="$SUDO_USER" ORIGINAL_USER="$SUDO_USER"
else else
@ -119,15 +153,18 @@ if [[ "$INSTALL_PLEX" =~ ^[Yy]$ ]]; then
fi fi
DOWNLOADS_DIR="$(eval echo ~${ORIGINAL_USER})/Downloads" DOWNLOADS_DIR="$(eval echo ~${ORIGINAL_USER})/Downloads"
# 2.2. Update system packages # Update the package database and install prerequisites:
# curl: For downloading files (used by Plex internally)
# apt-transport-https: Allows apt to fetch packages over HTTPS
# gnupg: GPG key handling for package verification
echo "Updating package list..." echo "Updating package list..."
apt-get update -y apt-get update -y
# 2.3. Install dependencies
echo "Installing curl, apt-transport-https, gnupg..." echo "Installing curl, apt-transport-https, gnupg..."
apt-get install -y curl apt-transport-https gnupg apt-get install -y curl apt-transport-https gnupg
# 2.4. Find the Plex .deb file in ~/Downloads # Look for the Plex .deb in the user's Downloads folder. The filename
# typically looks like "plexmediaserver_1.40.0.1234-abcdef_amd64.deb".
PLEX_DEB=$(ls -1 "${DOWNLOADS_DIR}"/plexmediaserver*.deb 2>/dev/null || true) PLEX_DEB=$(ls -1 "${DOWNLOADS_DIR}"/plexmediaserver*.deb 2>/dev/null || true)
if [[ -z "$PLEX_DEB" ]]; then if [[ -z "$PLEX_DEB" ]]; then
@ -138,25 +175,32 @@ if [[ "$INSTALL_PLEX" =~ ^[Yy]$ ]]; then
echo "$PLEX_DEB" echo "$PLEX_DEB"
echo "Installing Plex Media Server..." echo "Installing Plex Media Server..."
# Attempt to install # Install the .deb package. dpkg -i may fail if dependencies are missing
# (it doesn't resolve them automatically). That's OK — apt-get -f install
# runs right after and pulls in any missing dependencies, then retries
# the failed package installation.
dpkg -i "$PLEX_DEB" || true dpkg -i "$PLEX_DEB" || true
# Fix missing dependencies if dpkg complained
apt-get -f install -y apt-get -f install -y
# Enable and start Plex # Enable the Plex service so it starts automatically on every boot,
# and start it now so the user can access it immediately.
echo "Enabling Plex Media Server on boot..." echo "Enabling Plex Media Server on boot..."
systemctl enable plexmediaserver.service systemctl enable plexmediaserver.service
echo "Starting Plex Media Server..." echo "Starting Plex Media Server..."
systemctl start plexmediaserver.service systemctl start plexmediaserver.service
# 2.5. Configure firewall # Open port 32400 in the firewall. This is Plex's default web interface
# port — without this, other devices on the network can't access the
# server. UFW (Uncomplicated Firewall) is the standard firewall on
# Ubuntu/Mint.
echo "Installing ufw (if not installed) and allowing port 32400/tcp..." echo "Installing ufw (if not installed) and allowing port 32400/tcp..."
apt-get install -y ufw apt-get install -y ufw
ufw allow 32400/tcp || true ufw allow 32400/tcp || true
# Optionally enable ufw (uncomment if desired): # Note: UFW is not enabled by default. Uncomment the line below if you
# want to activate the firewall. Be careful — this will block all ports
# except those explicitly allowed (like 32400 above and SSH port 22).
# ufw enable # ufw enable
echo echo