Here is how I stream music via MPD wherever I go

A modern music streaming webservice playing some
music

Music streaming has become as easy and as affordable as it has never been before. However, if you try to avoid DRM or data harvesting by big tech companies you are mostly out of luck.

There are several really nice Free and Open Source self-hosted streaming services out there, such as koel, ampache or even Funkwhale. All of them are loaded with features and even support multiple accounts, simultaneous usage, etc…

What I needed however, was a simple, low-maintenance and low footprint solution that would enable me to stream music at home, on the go via mobile devices and maybe at work.

And here is, where the infamous Music Player Daemon (MPD) comes into play. For the longest time, I’ve been using MPD on a cheap, single core VPS streaming the current playback via HTTP and interacting with it via a commandline client through SSH.

This has its drawbacks of course, for once it is not always straight forward to SSH to your VPS on the go or using somebody else’s computer. Additionally, this may come as a security risk, as you need to enable authentication via password (rather than SSH-key) and having full shell access just to change the current playlist seems like overkill.

Luckily, MPD has a bunch of web-clients which seem like a perfect fit for my purposes. I tried several of those listed on MPD’s website, but the one that worked best for me was MyMPD. SPecifically, MyMPD looks relatively modern, is reasonable performant and can - on top of managing playlists and giving commands to MPD - play the audiostream.

Setting up the pieces

My setup contains of mainly three things:

  1. The Music Player Daemon (MPD)
  2. The Music Player Frontend (MyMPD)
  3. A Webserver to proxy and serve the content (Apache2)

My setup runs on a Debian VPS, so particularly the installation portion and the location of the config files may vary if you use something else.

MPD

I use the default MPD delivered via the Debian repositories, so installation is as simple as:

1
apt install mpd

The config file is located under /etc/mpd.conf, which you can edit with the text editor of your choice (I like vim or nano). Let’s discuss the interesting parts of that file:

1
music_directory         "/mnt/external_storage/Music"

This sets the location of your music files, so set this to the appropriate location. I got mine on an external storage device mounted on the VPS under /mnt/external_storage/Music, so set this to whatever location your music files live in. You have to make sure that mpd is allowed to read from the specified path before hand, for example by setting read-permission of the directory to “everyone”: chmod 755 -R /mnt/external_storage/Music

1
user                            "mpd"

MPD should never be run with root privileges, hence by default Debian will set up a user called mpd on your server. Above line makes sure that MPD is run as this user.

1
2
bind_to_address         "/run/mpd/socket"
port                            "6600"

This sets the address where the client can access MPD and give commands to it. Since we host the client on the same server as MPD itself, it makes sense to use a websocket here, rather than an IP-Address. The Port used in the above example is the default for MPD.

1
auto_update    "yes"

Above line makes sure that MPD updates the library index whenever files in your music directory change and is a good idea to enable in almost all cases.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
audio_output {
        type            "httpd"
        name            "HTTP Stream"
        encoder         "lame"
        port            "8000"
        bitrate         "320"
        format          "44100:16:1"
        always_on       "yes"
        tags            "yes"
}

Above chunk of configuration sets the output for the audio-stream. We choose a httpd type to make it streamable via HTTP. The lame encoder is known from MP3-files, so this is a relatively wide-spread format that can be streamed easily and has broad compatibility on almost all devices. The bitrate sets the stream quality, above configuration opts for the best 320kBit/s. Depending on bandwidth of your server and the devices you are going to be streaming to, you may want to choose a lower value here. MyMPD expects the always_on setting and media tags within the stream, so set both values to “yes”.

After saving the settings, enable MPD and start the service:

1
systemctl enable mpd --now

You can check whether it started correctly via:

1
systemctl status mpd

MyMPD

Unfortunately, not all distributions include MyMPD in their official repositories (yet). Such that manual installation procedures have to be taken.

The project itself makes binaries of MyMPD available for various Linux Distributions via the OpenSUSE Build system. Using these binaries involves a lot of trust into the developers and how they take care of their access keys. It is generally frowned upon to use such pre-built binaries. Instead, you could compile the application yourself from source.

IMO, if your server does not hold mission critical or private data or services, the harm to be done here is minimal in those cases.

Whatever way you choose to install MyMPD, the configuration afterwards is pretty much the same in all cases.

By default, all configuration files reside in /var/lib/mympd/config/. Each configuration is its own file with the according value within. The important files are:

  • /var/lib/mympd/config/http_host: Set this to 127.0.0.1, i.e. only your server itself should have access to MyMPD, no outside connections. This is important, since we put access control to MyMPD via our proxy server later.
  • /var/lib/mympd/config/http_port: Choose whatever IP is still available, I went with 8080.
  • /var/lib/mympd/config/ssl: Set this to false, since our proxy server handles the SSL certificate.

Afterwards, start up MyMPD with following command:

1
systemctl enable mympd --now

And check its status with:

1
systemctl status mympd

Proxy Server

Now we need a web server which is able to work as a proxy between our local MyMPD install, the audio stream and the internet. I personally like Apache2, so that’s what I am showing here.

Again, as with MPD, I use the default Apache2 binaries delivered via the Debian repositories, so installation is as simple as:

1
apt install apache2

Next, the configuration file of the proxy has to be created. Again, any text editor can be used for this, for example nano. Let’s call the configuration file mympd.conf and place it here:

1
/etc/apache2/sites-available/mympd.conf

The content of this file should look similar to this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<VirtualHost *:80>

    # Set your Domain Name here
    ServerName music.your-domain.com

    # Proxy Settings
    ProxyRequests Off
    ProxyPreserveHost on
    ProxyVia On
    
    # Web Frontend
    ProxyPass http://127.0.0.1:8080/
    ProxyPassReverse http://127.0.0.1:8080/
    
    # Stream and MPD
    <LocationMatch "/ws/?">
        ProxyPass ws://127.0.0.1:8080
        ProxyPassReverse ws://127.0.0.1:8080
    </LocationMatch>

    # Authentication
    <Location "/">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpassword
        Require valid-user
    </Location>

</VirtualHost>

Make sure to exchange the ServerName value right at the top of the configuration. It is also important to point the according (sub-) domain to your server via your DNS settings. This highly depends on your DNS provider, so this bit is not covered here, but your provider can help you with that.

Since we are using Apache2 as a proxy, we need to enable the according modules as well. Additionally the authentications module is required:

1
a2enmod proxy proxy_http proxy_ws auth_basic

Add an authentication user, so you can actually access the site. Upon running the command below, you’ll be asked to supply and then confirm a password. Make sure to exchange USER with the login name you want to use:

1
htpasswd -c /etc/apache2/.htpasswd USER

Now it is time to enable the site and take a look at it!

1
2
a2ensite mympd.conf
systemctl restart apache2

Open up the (sub-) domain which you configured above in your browser. A pop-up will appear asking for your login-credentials (the ones you just set). Upon entering, you are greeted with MyMPD.

The MyMPD webinterface showing the library view with blurred out
information

There is little reason to not use encryption and all the more reasons to actually use it. On top of that, projects like Let’s Encrypt make it super easy and gratis to set up TLS-certificates.

The EFF created a nifty tool, that does all the heavy lifting for your: certbot, so let’s start by installing it:

1
apt install certbot

Afterwards, creating your certificate is as easy as:

1
certbot -d music.your-domain.com --apache

Make sure, to place your actual (sub-) domain after the -d parameter. You can also put multiple domains there, each separated by a ,, if you have several domains on that server. Also notice the --apache flag at the end, which directly interacts with our proxy server to set everything up automatically.

During the creation of the certificate, you’ll be asked whether the http site should redirect to https, which you should probably agree to in almost all cases.

Now, next time you open up MyMPD in your browser, it should be using https by default, which you can see either in your browser bar or a little lock-icon right next to it.

That’s it!

One last thing: MyMPD is first and foremost a client to control the Music Player Daemon (MPD), if you want to actually listen to the playback, you can enable the audiostream in the bottom right corner. If you configured multiple outputs in MPD, they will all show up here and you can choose from them.

The little streaming button in the lower right corner of
MyMPD