Skip to content

Instantly share code, notes, and snippets.

@stephendarling
Last active September 14, 2023 02:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save stephendarling/37ef2fbba41389971eb4a12ca1392e37 to your computer and use it in GitHub Desktop.
Save stephendarling/37ef2fbba41389971eb4a12ca1392e37 to your computer and use it in GitHub Desktop.
Set up a Plex server on a headless Ubuntu 16.04 server

Ubuntu Plex Server (headless)

This gist walks through how to configure Plex Media Server on an Ubuntu 16.04 Server headless install. Through the rest of this tutorial, the server will be referred to as SERVER.

Assumptions

  • The SERVER is configured with SSH access, either via local credentials, ssh-keypair or some other federated identity
  • The SERVER is connected to the same LAN as all required data sources (if your storage is not on the same box, e.g. NAS)

Configure static IP address, netmask, gateway and dns-nameservers

You don't want the IP address of this SERVER (or any server) to change. Since it will be accessed only via internal IP address you can set that here. NOTE: If you change the CIDR block on your network you will need to update the static IP address of the SERVER here or switch the static method to dhcp and remove the address line. If you forget to do this the SERVER may be inaccessible.

# /etc/network/interfaces
=========================
...
# The primary network interface
auto eno1
iface eno1 inet static
        address $STATIC_IP_ADDRESS
        netmask $NETMASK
        gateway $GATEWAY
        dns-nameservers $PRIMARY_DNS $SECONDARY_DNS

Mount any remote drives

Your plex server will need to think that all of your media data is local.

Install CIFS Utils

sudo apt-get install cifs-utils

Create the media mount folder and set permissions

mkdir /mnt/media
sudo chown -R $USER:$USER /mnt/media

Append this to the end of the below file to configure the mount and set credentials

# /etc/fstab
============
...
//$REMOTE_SERVER_IP/PATH/TO/REMOTE/MEDIA/DIRECTORY /mnt/media cifs username=$USERNAME,password=$PASSWORD,iocharset=utf8,sec=ntlm,auto 0 0

And finally run this command to mount the drive(s)

sudo mount -a

Configure the SERVER to auto reboot at 4:00 AM daily

sudo su
crontab -e
# Add this line
0 4 * * * sudo reboot now

When you save this file crontab will automatically update and apply the new scheduled task.

Install Plex

Add the plex repository to the package manager and install

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 -
sudo apt update
sudo apt install plexmediaserver -y

Create the file below with all contents, replacing $USER with the name of the user you want to run Plex as

# /etc/systemd/system/plexmediaserver.service
=============================================
[Unit]
Description=Plex Media Server for Linux
After=network.target

[Service]
Environment="PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=/var/lib/plexmediaserver/Library/Application Support"
Environment=PLEX_MEDIA_SERVER_HOME=/usr/lib/plexmediaserver
Environment=PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS=6
Environment=PLEX_MEDIA_SERVER_TMPDIR=/tmp
Environment=LD_LIBRARY_PATH=/usr/lib/plexmediaserver
Environment=LC_ALL=en_US.UTF-8
Environment=LANG=en_US.UTF-8
ExecStartPre=/bin/sh -c '/usr/bin/test -d "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}" || /bin/mkdir -p "${PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR}"'
ExecStart=/bin/sh -c '/usr/lib/plexmediaserver/Plex\ Media\ Server'
Type=simple
User=$USER
Group=$USER
Restart=on-failure
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

Change permissions, substituting the $USER with the user you want to run Plex as

sudo chown -R $USER:$USER /var/lib/plexmediaserver
sudo systemctl --system daemon-reload
sudo service plexmediaserver start

Update the file below and change the $USER to the user you want to run Plex as

# /lib/systemd/system/plexmediaserver.service
=============================================
...
User=$USER
Group=$USER
...

At this point you can navigate to the IP of the server on port 32400 and configure plex. For remote access, set manual remote access to agree with the port you opened in the router To update, run the following command from command line

Update Plex

Every now and then you will see a banner across the top of Plex mentioning that an update is available. Follow the commands below to apply these updates.

Update local package manager with new files from remote respositories

sudo apt-get update

Install updates but only the upgrades

sudo apt-get install --only-upgrade plexmediaserver

NOTE: IF YOU FORGET THE --only-upgrade FLAG THE INSTALL COULD OVERRIDE YOUR CURRENT INSTALLATION AND ALL RELATED DATABSE RECORDS. DON'T FORGET THIS FLAG!

Stop and restart the Plex service

# Stops the service
sudo service plexmediaserver stop
# Starts the service
sudo service plexmediaserver start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment