Skip to main content

Part 2 - Updating the VPS

You have prepared your backup script in Part 1 - How to restore your VPS in minutes, and your two backups (Plex database and VPS settings) are safely stored in the (Google) cloud.

You wiped your VPS, or you signed up with a new provider. Either way, you are now looking at a brand new, clean VPS, waiting to be installed.

What is the first thing you usually do? You configure Ubuntu to install updates, install the apps you can't live without, set your clock and firewall, change your root password and create your user. Basically, all the steps covered in part 2 of the first tutorial.

Notice how these steps never vary? You perform them without thinking, each and every time, always the same way. The perfect candidate for automation!

Creating a script to update the VPS

So, without further ado, here is how you create a script to automate this. So go ahead, open Notepad++ on your local computer and create a new (empty) file. Make sure you change the bottom bar to show "Unix (LF)", as shown in the screenshot. To do this you need to right-click "Windows (CR/LF)" and select "Unix (LF)" instead.

Paste the following into your file:


# Initial update

sudo apt-get -y --force-yes update
sudo apt-get -y --force-yes upgrade

# Install unattended upgrades

sudo apt-get -y --force-yes install unattended-upgrades

if [[ ! -f /etc/apt/apt.conf.d/20auto-upgrades.bak ]]; then
    sudo cp /etc/apt/apt.conf.d/20auto-upgrades /etc/apt/apt.conf.d/20auto-upgrades.bak
    sudo rm /etc/apt/apt.conf.d/20auto-upgrades
    echo "APT::Periodic::Update-Package-Lists \"1\";
    APT::Periodic::Download-Upgradeable-Packages \"1\";
    APT::Periodic::AutocleanInterval \"30\";
    APT::Periodic::Unattended-Upgrade \"1\";" | sudo tee --append /etc/apt/apt.conf.d/20auto-upgrades

# Set language

test "$LANG" = "en_US.UTF-8" \
    || echo "en_US.UTF-8 UTF-8" >>/etc/locale.gen \
    && locale-gen --lang en_US.UTF-8

# Install apps

sudo apt-get -y install \
  fail2ban \
  nano \
  unzip \
  wget \
  curl \
  fuse \
  ufw \
  git \
  socat \
  denyhosts at sudo software-properties-common

# Configure firewall

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 32400
sudo ufw allow 33400
sudo ufw allow 8181
sudo ufw --force enable

# Set timezone

sudo dpkg-reconfigure tzdata

# Change root password

echo -e "You will now need to change your root password"

# Create user

read -e -p "Your preferred username: " -i "plexuser" yourusername
read -e -p "Your preferred password: " -i "Pa$$w0rd" yourpassword

adduser $yourusername --gecos ",,," --disabled-password
echo "$yourusername:$yourpassword" | chpasswd
usermod -a -G sudo $yourusername
echo -e "$yourusername\tALL=(ALL)\tNOPASSWD:ALL" > /etc/sudoers.d/$yourusername
chmod 0440 /etc/sudoers.d/$yourusername

# Cleaning up

sudo rm /tmp/update-vps

# Reboot

read -e -p "Reboot (Y/n)? " -i "Y" choice

case "$choice" in
  y|Y ) sudo reboot;;
  * ) echo "Manual reboot required.";;

# End message

echo ""
echo "==================================================="
echo " Please reboot! Then proceed with the installation "
echo "==================================================="
echo ""

# Switch to user

su $yourusername

# To run this file copy the line below without the # and paste in PuTTY

# sudo wget -O /tmp/update-vps && sudo chmod +x /tmp/update-vps && sudo /tmp/update-vps

Notice the lines in red. Here is an explanation on what to do with each of them:

If you use Plex without CloudFlare, leave it the way it is. However, if you have followed the tutorial to setup CloudFlare with Plex, make sure you change the port from 32400 to 8443.

plexuser / Pa$$w0rd
Change the parts in red to your own username and password.

Leave the last line in red (the Dropbox link) the way it is for now.

Getting the file link

Once done, save your script with any filename you want, and upload it to Dropbox. I named my file 1-update-vps.bash, but the name you choose doesn't matter.

Pro tip: the reason I save the script files with the extension .bash is so that I can set them to always open with Notepad++ and make them easier to read.

The reason I suggest Dropbox is because you will need public access to your newly created script, but you won't want other people to be able to find it. Dropbox is perfect for that!

You now need to copy the Dropbox link. The easiest way is just right-click the file, then click "Copy Dropbox link".

Now open your file again (right-click the file, then click "Edit with Notepad++"). Then replace the red part in the last line with the link you just copied from Dropbox and save your file again.

Running the script

Once you have either rebuilt your VPS or moved to a new server, all you need to is open the script on your local computer, then copy (Ctrl-C) the entire last line without the red part and without the #. In the example above, that would be:

sudo wget -O /tmp/1-update-vps && sudo chmod +x /tmp/1-update-vps && sudo /tmp/1-update-vps

The last step is to paste this in PuTTY (I'm sure I won't have to remind you that pasting in PuTTY is done by right-click) to run the script.

Here is an explanation of what this script will do:

  • Your VPS will update itself with all the latest security patches and upgrades.
  • The VPS will always remain up to date by installing unattended upgrades.
  • It will set the language to US English. You can remove this section if you have a VPS in the country which language you speak.
  • It will install the applications you use. Feel free to add or remove what you want there.
  • It will configure your firewall with the ports needed for Plex, and PlexPy.
  • You will be asked to pick your local timezone.
  • You will be asked to set your strong root password.
  • Your personal user will be created with the password you define.
  • Optionally, but highly recommended, you will be asked to reboot.

Your VPS is ready for the Plex installation, so once it's back online, you can login as your own user and proceed with Part 3 - Installing Plex, Rclone and Plexdrive.