Raspberry Pi Desktop Media Server
We recently wrote up our experiences with the excellent new Raspberry Pi DIY Desktop Kit from Element14. For our article about this kit we looked at how to build and set up this kit to construct a Raspberry Pi Desktop. However, while building the kit we also considered how we might actually be able to use it.
One of the really great features of this kit is that it allows a mSATA SSD to be fitted to the Raspberry Pi. This feature means that the kit would make an ideal NAS drive or media server. So, with this in mind we considered what sort of size drive would be most appropriate. We wanted a drive that would give a good trade off between capacity and cost. With this in mind, we decided that for a reasonable size media library, a 250GB SSD would work well.
Setting Up The Hardware
Building the Raspberry Pi DIY Desktop Kit is quite straightforward, and is fully described in our previous article.
Setting Up The Software
Some of the software set up is also described in our article about setting up the Raspberry Pi Desktop Kit. However, the software mentioned in the article relates only to that required for the Desktop Kit itself, to make the Desktop Kit work and to clone the SD card to the SSD.
However, for our NAS drive and media server we will require a few more bits of software to be installed and set up. Many of the bits of software required are described in another of our articles about streaming media using JustBoom audio HATs. However, for clarity we’ll cover each them individually here.
Media Server Software
Our first step is to set up the software required for the media server. As with our previous media streaming project, we used the miniDLNA software package. This is installed using the terminal commands:-
sudo apt update sudo apt upgrade sudo apt install minidlna
Once installed, miniDLNA must be configured by editing the configuration file /etc/minidlna.conf. This is done by using the terminal command:-
sudo nano /etc/minidlna.conf
Within this configuration file, the following parameters need to be modified:-
The friendly_name parameter can be set to whatever you wish to use as the name for your media server; I chose the name ‘Pi Media Server’ for ours.
The media_dir parameter can be repeated any number of times within the configuration file, to define a list of folders. For now, I just wanted a single folder containing folders of audio files, so I defined a single media_dir parameter:-
Having saved the changes to the configuration file, it would normally be necessary to restart the miniDLNA service and force a reload of its media database. This is done use the terminal commands:-
sudo service minidlna restart sudo service minidlna force-reload
At this stage, although the media server is visible, there is actually no media available. So, the next stage is to get some media copied into the media folder. This can be done in a number of ways. Probably the easiest way is to attach a USB drive containing the media, and then copy the media files into the /home/pi/Music folder which we set up for our audio media files. In my case, I wanted to copy my audio library from a NAS drive. This meant having to mount the NAS drive as a network drive, and then copy the files across the network.
Mounting a Network Drive
To mount a network drive, you will need to install a couple more software packages. These are smbclient and cifs-utils. Install these using the following terminal commands:-
sudo apt update sudo apt install smbclient sudo apt install cifs-utils
Once installed, we need to create a local folder to mount the network drive to; in our case /media/NAS. To do this use the terminal command:-
sudo mkdir /media/NAS
Then, having created this folder, mount the network drive using the following terminal command:-
sudo mount -t cifs -o username=<username>,password=<password> //192.168.1.10/Music /media/NAS
We only need to mount this drive while we copy our media files across the network. However, if we wanted to mount this drive each time we reboot the Raspberry Pi we could do this by modifying the file /etc/fstab using the command:-
sudo nano /etc/fstab
and adding the line:-
//192.168.1.10/Music /media/NAS cifs username=<username>,password=<password>,workgroup=WORKGROUP,users,auto,user_xattr,_netdev,x-systemd.automount 0 0
Once our network drive has been mounted, we can copy our media files across the network using the following terminal command:-
rsync -rv /media/NAS/ /home/pi/Music/
Depending on the amount of media to be copied, this could of course take quite some time. However, once completed we can issue the command to reload the miniDLNA media database:-
sudo service minidlna force-reload
Our media server is now set up, complete with a library of audio files. To check that things are working so far, I used BubbleUPnP media client Android app to browse the Pi Media Server media library.
WiFi Access Point Software
Because of the way I wanted to access our media server, I needed to set it up as a WiFi access point. This would allow me to stream media wirelessly from the media server, and would also provide network access if the Raspberry Pi was connected to a wired network. This would give access to internet radio, and allow software updates to be applied to the Pi.
In order to set up the Raspberry Pi as a wireless access point, I followed the excellent guide written by Phil Martin. I’ve just listed the steps that need to be done, but for a fuller explanation of this process please refer to Phil’s guide.
Please note at this point, that you will not be able to carry out all of the following steps using a SSH connection to the Raspberry Pi, since the network access will be unavailable during some of the steps. For this reason, you will need to carry out this stage of the installation with the Pi connected to a terminal with keyboard and mouse.
The first thing is to install the software packages required. These are hostapd and dnsmasq. To install these software packages, use the following terminal commands:-
sudo apt update sudo apt install hostapd sudo apt install dnsmasq
Configuring the Wireless Interface
The next step is to configure the wireless interface (wlan0) with a static IP address. Be sure to use an address that is outside the range of addresses that are allocated by your router. My router allocates DHCP addresses in the range 192.168.1.2 to 192.168.1.128, so I chose the address 192.168.1.130 for the static IP address for this device. As part of this, we also need to ensure that Raspbian does not automatically allocate an IP address to wlan0. To do this, edit the file /etc/dhcpcd.conf using the following terminal command:-
sudo nano /etc/dhcpcd.conf
At the bottom of this file, add the following line:-
To configure the static IP address for this device, modify the /etc/network/interfaces file using the terminal command:-
sudo nano /etc/network/interfaces
In this configuration file, edit the section relating to wlan0 to add the following:-
allow-hotplug wlan0 iface wlan0 inet static address 192.168.1.130 netmask 255.255.255.0 network 192.168.1.0 broadcast 192.168.1.255 # wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Note that you will need to adjust the address, network and broadcast lines depending on the IP range of your router.
Once these changes have been made, restart the dhcpcd service using the following terminal commands:-
sudo service dhcpcd restart sudo ifdown wlan0 sudo ifup wlan0
The next step is to configure the wireless access point. This is done by creating a new hostapd configuration file. To do this, enter the following terminal command:-
sudo nano /etc/hostapd/hostapd.conf
The following lines need to be added to this file:-
interface=wlan0 driver=nl80211 ssid=Pi3-AP hw_mode=g channel=6 ieee80211n=1 wmm_enabled=1 ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40] macaddr_acl=0 auth_algs=1 ignore_broadcast_ssid=0 wpa=2 wpa_key_mgmt=WPA-PSK wpa_passphrase=raspberry rsn_pairwise=CCMP
Modify the ‘ssid’ and ‘wpa_passphrase’ lines as appropriate to reflect your required access point SSID and password.
To check that this access point works ok, enter the following terminal command and check to see if the wireless access point is visible:-
sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf
You will need to use ctrl+c to stop this once you’ve checked that the access point is visible.
To get the hostapd service to use this configuration file, edit the default hostapd configuration file using the terminal command:-
sudo nano /etc/default/hostapd
Change the line:-
The dnsmasq has a large configuration file, most of which is not required for our purposes. For this reason, this configuration file should be moved, and a new one created. To move the default configuration file, use the terminal command:-
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
Now, create a new configuration file using the terminal command:-
sudo nano /etc/dnsmasq.conf
and add the following lines to the configuration file:-
interface=wlan0 listen-address=192.168.1.130 bind-interfaces server=220.127.116.11 domain-needed bogus-priv dhcp-range=192.168.1.131,192.168.1.140,24h
This configuration will cause the wlan0 interface to listen on address 192.168.1.130, and will allocate dhcp addresses in the range 192.168.1.131 to 192.168.1.140, with a 24 hour lease time. These values may need to be modified depending on the configuration of your router, to ensure that the DHCP server on the Raspberry Pi does not allocate addresses in the same range as the router.
Setting up IPV4 Forwarding
The last stage in setting up the wireless access point is to set up IPV4 forwarding. To do this, edit the /etc/sysctl.conf file using the terminal command:-
sudo nano /etc/sysctl.conf
and remove the comment mark (#) at the beginning of the line:-
Also, to enable sharing of the internet connection, the following commands need to be entered:-
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
To ensure that these address translation rules are applied every time the Pi is rebooted, enter the following command to save the rules to a file:-
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"
And, to load these rules each time the Pi is rebooted, edit the /etc/rc.local file using the terminal command:-
sudo nano /etc/rc.local
and add the following line just above the line that says ‘exit 0’:-
iptables-restore < /etc/iptables.ipv4.nat
Once all these changes have been made, reboot the Pi using the terminal command:-
Once the Raspberry Pi has rebooted, you should have a wireless access point that you can connect to. You can check that everything is working correctly by connecting a device to the access point. Provided that the Raspberry Pi is connected to a network cable, this should also give us access to the internet.
Checking It All Works
The steps covered so far should provide everything that is needed for a fully function DLNA media server. To check this out, I used the BubbleUPnP app from an Android phone. By connecting the phone to the Pi3-AP wireless access point, the BubbleUPnP app could see the ‘Pi Media Server’.
Bonus Feature – NAS Drive
For many media clients, simply having the DLNA server is sufficient to enable media to be streamed to it. For example, using the BubbleUPnP app allows me to stream audio to a media client that supports DLNA rendering, such as the excellent JustBoom Player.
However, for media clients that do not support DLNA rendering, it is useful to have the media server set up as a NAS drive that can be accessed by the media client. This allows the media client to access the media library as a NAS device.
Setting up the Raspberry Pi as a NAS device is quite straight forward, and involves installing and configuring a couple of software packages. To install the required software packages, use the following terminal commands:-
sudo apt update sudo apt install samba samba-common-bin
Having installed Samba, it is necessary to define the configuration for the drive we want to share. To do this, edit the Samba configuration file using the following terminal command:-
sudo nano /etc/samba/smb.conf
In this configuration file, we need to configure the folder that we want to share as a NAS device. For our purposes, we added the following to the smb.conf file:-
[Music] comment = Music Folder path = "/home/pi/Music" valid users = @users force group = users create mask = 0660 directory mask = 0771 read only = yes
After saving this configuration file, restart Samba using the following terminal command:-
sudo /etc/init.d/samba restart
Next, we need to add an account to allow network users to access this NAS device. We set up an account ‘media’ using the following terminal commands:-
sudo useradd media -m -G users sudo passwd media
The second of these commands will prompt for a password and password confirmation.
Finally, we needed to set up the Samba password for this media account using the following terminal command which will prompt for a password and password confirmation:-
sudo smbpasswd -a media
One this NAS device is set up, any media client should now be able to access this NAS drive by attaching to the wireless access point, and mounting the NAS device as a drive.