Setting up Pi-hole and a Plex media server in a Raspberry Pi 4

For a while, I used one of my Raspberry Pi 4 (4GB Model + 256GB MicroSD card) as a NextCloud server using the NextCloudPi image, and although it was pretty neat, it was more of an experiment since I didn’t really feel the need for a local cloud with my current setup. This left me with a spare Raspberry Pi.

This bad boy needs some dusting.

So recently, I decided to repurpose this Raspberry Pi and give it 2 new purposes in life:

  1. I’ve been wanting to test out Pi-hole for a while as I wanted to see how effective it is at blocking ads and trackers at a network level. Pi-hole is a linux network-level ad and tracker blocker, which will act as a sinkhole and drop any DNS calls to known advertiser or tracker domains.
  2. I also have some media and old movies I wanted to be able to stream on any home device without the hassle of USB sticks or HDMI cables hanging around, so Plex sounded like the ideal solution. Plex offers a client-server media player solution, where we will be installing the server (containing all media files) in the Raspberry Pi storage (In mi case, a 256GB MicroSD card), and this media will be available and ready to be streamed through any Plex client app (PC, iOS, Android, Xbox Playstation, Chromecast, Amazon Fire TV, etc). This is extremely useful if you have some local movies or home videos you wish to be able to stream from any device in your household (Just remember, illegaly downloading movies is not cool!).

And whilst the installation and set up process of both solutions is pretty straightforward, I thought this could make an interesting post for anybody curious about these solutions or with a spare Raspberry Pi.

First, let’s install Raspberry Pi OS

Raspberry Pi OS (formerly called Raspbian) is the official, lightweight Debian Linux distribution for Raspberry Pi. You can download the official imager software, which is available for all major OS through the Raspberry Pi OS site and install locally on your machine.

The Raspberry Pi Imager is very easy to use; you will need to select the OS (Latest stable Raspberry Pi OS release) and the storage where to burn this image (your Micro SD card). Additionally I’d also highly recommend going to the advanced settings (gear icon) and configure a username and password, as well as a wireless LAN if available to save some extra time:

After this, you can click on Write and the imager should start burning this image for you – This process should only take a few minutes, depending on your type and volume of storage. Once the write process is complete, your Raspberry Pi is ready to go.

Now that Raspberry Pi OS has been installed, you can just plug it in and turn it on and it should be ready to use. First, we can get started with updating the existing repositories. To do this, open a new terminal and launch the following command:

jj@rasp:~
sudo apt-get update && sudo apt-get upgrade

How to assign a Static IP to a Raspberry Pi

For the purpose of this setup (Plex + Pi-Hole) we would need to make sure the Raspberry Pi uses a static IP address. To do this, we will need to gather a few details:

1. Determine your device’s current IP address. You can find which local IP v4 address has been assigned to your device by launching the following command:

jj@rasp:~
hostname -I

This should return the IP address that has automatically been assigned to your Raspberry Pi by your router:

2. Get your router’s IP address (if you don’t know it already). You can query your router’s IP address by using the command ip r:

jj@rasp:~
ip r

Important: This tutorial assumes the current network interface used for the configuration is the same that will be used for the final deployment. For my specific use case, I use the wireless default interface wlan0.

3. Get your default DNS IP address (This may sometimes be the same as your router’s IP address):

jj@rasp:~
grep "namesever" /etc/resolv.conf

With these details, we can now open the appropiate configuration file (dhcpcd.conf) and set a fix IP address for our Raspberry Pi.

4. Open /etc/dhcpcf.con with nano text editor:

jj@rasp:~
nano /etc/dhcpcd.conf

And add the following lines to the bottom of the file:

jj@rasp:~
interface wlan0
static routers=[Router IP here]
static domain_name_servers=[DNS IP1, DNS IP2]
static ip_address=[STATIC IP ADDRESS TO USE FOR THIS RASPBERRY PI]

Remember that the interface name wlan0 may be different for you depending on the network interfaces available and in use in your Raspberry Pi.

5. Save changes in nano by pressing CTRL + O, then exit with CTRL + X and reboot the device. With these changes, the Raspberry Pi will try to claim the static IP address requested in the dhcpcd.conf file.

Setting up Pi-hole

Now that we have defined a static IP address for the Raspberry Pi, we can get started with installing Pi-Hole. Pi-hole offers a very easy and convenient one-step automated install that we can use by using launching the following command:

jj@rasp:~
curl -sSL https://install.pi-hole.net | bash

This will launch the self-paced installation wizard, which will ask you a series of questions in order to configure Pi-hole.

By default, Pi-Hole will let you select a series of upstream DNS providers including Google, OpenDNS and Cloudflare among others, but you can also use a custom DNS provider by selecting Custom. The installation wizard will also ask you if you wish to go ahead with a recommended ad blocklist – I selected yes-, with installing a local admin web interface and a web server (Which will require additional PHP modules) – I selected yes -, as well as your preference for query logging. This may be important to you depending on which environment you are deploying the Pi-hole in, as the logs can show domains and devices. In my case, and since I am using this exclusively on my home network, I am comfortable logging all details for now. Once confirming all the details, the installation wizard will resume the installation and Pi-Hole will be ready to be used:

If you wish to change your default Pi-Hole Admin portal password, you can do so by launching the following command:

jj@rasp:~
pihole -a -p

Pi-Hole should be fully installed now and ready to be accessed through http://pi.hole/admin or http://[yourRaspberryPiIPAddressHere]/admin – If you are unable to access the Web Admin Portal, try rebooting the Raspberry Pi.

Now that Pi-Hole is fully installed, you would need to configure how you wish your local network to process its traffic. There are two main options:

1 – Set up Pi-Hole IP address as the only DNS entry in the router

If your router allows you to set a custom DNS server, you would simply need to log in to your router interface and use the static IP address configured for the Raspberry Pi as your DNS server. Pi-hole will do the rest and all your network traffic should now be processed and protected by Pi-Hole.

If your router doesn’t allow you to change the default DNS entry, see next step.

2 – Set up Pi-Hole built in DHCP Server

In most home environments, routers also act as DHCP server, which automatically assigns new IP addresses to any device connected to your network. Although your router may not allow you to use a custom DNS server, it may still allow you certain control over the DHCP settings. To configure Pi-Hole as your DHCP server and protect your network traffic, you will need to follow two steps:

1. Disable the DHCP server feature in your router. This is a critical step and you may receive a warning message:

2. Set up Pi-hole as your DHCP server (Settings > DHCP > DHCP Server Enabled):

After setting up Pi-Hole as your DHCP server, it will be on the Raspberry Pi to automatically assign new IP addresses to the devices in your home network next time they connect.

If none of these methods are available in your router interface, there are some additional ways you can configure your devices to connect through Pi-Hole, either by advertising Pi-Hole’s IP address via dnsmasq in the router, or manually configuring the DNS server of each of your devices to the Raspberry PI IP address. You can find more info about these options here.

After these changes, all your network traffic should be processed through Pi-Hole, and you should start seeing it in action in real time by logging in to the Pi-Hole Admin console (http://pi.hole or http://[yourRaspberryPiIPAddressHere]/admin)

Setting up Plex Media Server

Although Plex can be set up to be accessed outside your network, this is something I am not planning on doing – This guide only covers deploying Plex on a local network. I originally followed the guide from PiMyLifeUp.

1. To install Plex to the Raspberry Pi, we’ll first need to add the official Plex package repository, but before we do that, we’ll need to install the “apt-transport-https” package, as this will allow the “apt” package manage to retrieve packages over https, which Plex does. We can do this by running the following command:

jj@rasp:~
sudo apt-get install apt-transport-https

2. Now we will need to add the Plex repositories to the “apt” keyrings directory – This step is recommended to ensure the files downloaded are indeed from the Plex repository and signed by their key:

jj@rasp:~
curl https://downloads.plex.tv/plex-keys/PlexSign.key | gpg --dearmor | sudo tee /usr/share/keyrings/plex-archive-keyring.gpg >/dev/null

3. Once the key has been added, we are ready to add the official plex repository to our sources list:

jj@rasp:~
echo deb [signed-by=/usr/share/keyrings/plex-archive-keyring.gpg] https://downloads.plex.tv/repo/deb public main | sudo tee /etc/apt/sources.list.d/plexmediaserver.list

4. Since we have added a new repository to our sources list, it is recommended to update the package list:

jj@rasp:~
sudo apt-get update

5. Now that we have configured Plex’s official repositories in our Raspberry Pi, we are ready to install Plex Media Server. To install this package, we’ll need to launch the following command:

jj@rasp:~
sudo apt install plexmediaserver

This installation process should be pretty quick, and will create a new user and group which Plex will use to run its services. Both user and group are called plex.

After the installation is completed, the Plex server should be already up and running, and you should be able to access the web interface by opening a web browser and going to your IP address, followed by :32400/web – http://[yourRaspberryPiIPAddressHere]:32400/web (For example, http://10.0.2.15:32400/web). This would forward you automatically to the Plex sign up/sign in page, where you will have to create a new account or sign up with an existing account.

Once you sign in, you should now have access to the Plex portal, where you will be able to access any media you upload to the server. The next step will be setting up a name for the server:

As well as defining the folders in the library which will be used for each type of media. By default, Plex will create a new directory on your Raspberry Pi – /var/lib/plexmediaserver/Library. Plex also requires different folders for each type of media (Pictures, Movies, TV Shows…), so for example if we wish to create a new folder for TV Shows, we could define the route /var/lib/plexmediaserver/Library/tvshows inside the Plex server as the folder inside your media library which will be used exclusively for TV Shows:

Now that we have defined the route for TV Shows, it is time to create that folder and start uploading content.

When installing plex, all the relevant folders were created by the new user “plex“. Since the Library directory (/var/lib/plexmediaserver/Library) was also created by this user, you may experience issues uploading media or creating new folders inside the Library. For my specific use case, and since I am not going to expose this server to the outside, what I did was to change the permissions of this directory in order to allow external users to add media, so any other member of my household would be able to easily add media to this server – This may be different for you, or you may not wish to allow anybody else in your network to add media to Plex, so please consider whether this step may be insecure for you.

To do this, I changed the directory permissions with chmod by launching the following command in a Raspberry Pi terminal:

jj@rasp:~
sudo chmod 777 /var/lib/plexmediaserver/Library

And now we should be able to create the tvshows directory inside the Library:

jj@rasp:~
mkdir /var/lib/plexmediaserver/Library/tvshows

With this, any video files uploaded to the tvshows folder should be picked up by Plex automatically. Remember that certain types of files may require specific naming conventions – Here’s additional info on the naming convention and structure required for movies and TV shows.

How to upload media to Plex

Now that we have installed Plex and defined a folder for our TV Shows, we can start uploading content to the server. There are many ways to transfer files locally, but one of the easiest ways to do this would be through a sFTP client like Filezilla. This way, you will be able to transfer files through your network and from any other device like your main PC. To connect Filezilla to your Plex server and transfer content, you would only need the following details:

  • Host – Raspberry Pi IP address
  • Username – Raspberry Pi Username defined when creating the Raspberry Pi OS image
  • Password – Raspberry Pi username password defined when creating the Raspberry Pi OS image
  • Port – 22

Once connected, you would only need to navigate to /var/lib/plexmediaserver/Library and transfer any media to their respective folders.

After the media is added, you may need to scan your library files in Plex in order to display any recently added media. You can do this simply through the web interface by going to each library and clicking on the 3 vertical dots > Scan Library files:

With this, your files should be ready to be played from any device in your network! The last step would be to download a Plex client from any other device (Like your iOS or Android playstore, the Playstation or Xbox stores, etc) and sign in with the Plex account you created when logging in to the web portal for the first time. The Plex client will be able to detect a plex server in your network and allow you to stream any content seamlessly.

At this point, both Plex and Pi-Hole should be running succesfully on your Raspberry Pi, and both admin portals should be accessible within your home network, but if you experience any issues or have a question, feel free to leave a comment below!

Leave a Reply

Your email address will not be published. Required fields are marked *