Nov 25, 2024 3 min read

Plex: The Full Guide to Self-Hosting Anywhere

Plex: The Full Guide to Self-Hosting Anywhere
Table of Contents

Plex is a powerful, self-hosted media server application that allows you to organize, stream, and share your media library across devices. Its customizable nature, full control over your data, and robust feature set make it an excellent choice for tech-savvy users, developers, and system administrators. In this guide, we’ll cover everything from installing Plex to configuring Nginx for secure access, managing logs, and leveraging its advanced features.

Installing Plex

πŸ“¦ Docker/Docker Compose Setup

Using Docker is one of the easiest ways to deploy Plex. Here’s how to set up Plex with Docker Compose.

Create a docker-compose.yml file:


version: '3.8'

services:

plex:

image: lscr.io/linuxserver/plex:latest

container_name: plex

environment:

- PUID=1000 # Set this to your user's ID

- PGID=1000 # Set this to your group's ID

- VERSION=docker

volumes:

- /path/to/config:/config # Plex configuration files

- /path/to/media:/media # Media library

ports:

- 32400:32400/tcp # Main Plex interface

- 3005:3005/tcp

- 8324:8324/tcp

- 32469:32469/tcp

- 1900:1900/udp

- 32410:32410/udp

- 32412:32412/udp

- 32413:32413/udp

- 32414:32414/udp

restart: unless-stopped

Deploy Plex with the following commands:


docker-compose up -d

This will start the Plex container. You can access Plex at http://<your-server-ip>:32400.

πŸš€ Manual Installation

To install Plex manually on a Linux server:

  1. Add the Plex repository to your system:

echo "deb https://downloads.plex.tv/repo/deb/ public main" | sudo tee /etc/apt/sources.list.d/plexmediaserver.list

curl https://downloads.plex.tv/plex-keys/PlexSign.key | sudo apt-key add -

  1. Update your package list and install Plex:

sudo apt update

sudo apt install plexmediaserver

  1. Start and enable Plex:

sudo systemctl start plexmediaserver

sudo systemctl enable plexmediaserver

Access Plex at http://<your-server-ip>:32400.

Configuring Nginx as a Reverse Proxy

🌐 Nginx Configuration

Set up Nginx to forward traffic to Plex. Create an Nginx server block:


server {

listen 80;

server_name plex.yourdomain.com;

location / {

proxy_pass http://localhost:32400;

proxy_set_header Host $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-Proto $scheme;

}

}

Save this file in /etc/nginx/sites-available/plex and enable it:


sudo ln -s /etc/nginx/sites-available/plex /etc/nginx/sites-enabled/

sudo nginx -t

sudo systemctl reload nginx

πŸ”’ SSL/TLS Setup

Secure your Plex instance with SSL using Let's Encrypt:

  1. Install Certbot:

sudo apt install certbot python3-certbot-nginx

  1. Obtain and configure an SSL certificate:

sudo certbot --nginx -d plex.yourdomain.com

  1. Automate certificate renewal:

sudo systemctl enable certbot.timer

πŸ› οΈ Testing and Reloading Nginx

Verify and reload the Nginx configuration:


sudo nginx -t

sudo systemctl reload nginx

Now, you can access Plex securely at https://plex.yourdomain.com.

Logging and Debugging Plex

πŸ—ƒοΈ Enabling Debug Logs

To enable debug-level logging in Plex, edit the server configuration. Open the Plex web interface and navigate to Settings > Server > Show Advanced > Debug and enable debug logs.

πŸ“„ Viewing Logs

View Plex logs directly:

For Docker-based installations:


docker logs plex

For manual installations:


cat /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/Logs/Plex\ Media\ Server.log

πŸ› οΈ Troubleshooting Common Issues

Use logs to identify issues such as port conflicts or permission problems. For example, if ports are blocked:


sudo netstat -tuln | grep 32400

Resolve file permission issues:


sudo chown -R plex:plex /path/to/media

πŸ“€ Exporting Logs

Export Plex logs for external analysis:


tar -czvf plex-logs.tar.gz /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/Logs/

You can then send the archive to an ELK stack or other monitoring tools.

Backup and Restore

πŸ—‚οΈ File-Based Backups

Back up Plex configuration files:


tar -czvf plex-config-backup.tar.gz /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/

πŸ”„ Database Backups

Export the Plex database file:


cp /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/Plug-in\ Support/Databases/com.plexapp.plugins.library.db /backup/location/

Restore it by copying it back to the same location.

πŸ“… Automated Backup Scripts

Automate backups with a cron job:


crontab -e

Add the following line to back up daily:


0 2 * * * tar -czvf /backup/plex-config-$(date +\%F).tar.gz /var/lib/plexmediaserver/Library/Application\ Support/Plex\ Media\ Server/

Updating and Upgrading Plex

⬆️ Updating Docker Images

Update Plex in Docker:


docker-compose pull plex

docker-compose up -d

πŸ› οΈ Manual Updates

For manual installations, update Plex with:


sudo apt update

sudo apt upgrade plexmediaserver

πŸ” Checking for Updates

In the Plex web interface, navigate to Settings > General to check for available updates.

Leveraging Plex’s Unique Features

πŸ”§ Enabling APIs

Plex supports APIs for automation. You can generate an API token by visiting Settings > Account > Advanced > Plex Token. Use this token to access media data:


curl -X GET "http://<server-ip>:32400/library/sections" -H "X-Plex-Token: YOUR_TOKEN"

🌟 Advanced Configurations

Enable hardware transcoding for faster media processing. Edit your Docker Compose file:


devices:

- /dev/dri:/dev/dri

Or, configure transcoding in the Plex web interface via Settings > Server > Transcoder.

Wrapping Up

In this guide, we’ve detailed the steps to install, configure, secure, and manage Plex on your server. From Docker deployments to advanced Nginx setups, we’ve covered everything you need to self-host Plex and leverage its extensive features for managing your media. With these actionable steps, you’re now ready to harness the full potential of Plex. Happy streaming!

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Selfhosted Ninja.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.