Matt Coneybeare

MC

How to Map Plex Media Server to Your Home Domain

| Comments

Being the Über Geek that I am, reaching my home server from anywhere in the world is trivial. I have all the appropriate port-forwarding in place for ssh, ftp, vnc, and other miscellaneous ports for various different tasks and programs. One such program is Plex, a media server that houses, manages and maintains my media collection. When I want to access the Plex web client from teh internetz, I can simply type my home IP, port and path into any browser from anywhere in the world, login and Boom-Wow-Kapow™, I am in.

The Problem

While this process is great and easy for me as a software-engineer, my wife, who is !software-engineer should be able to watch Plex as easily as I can. She shouldn’t have to figure out the IP address of our home machine, remember which port to use or remember the relative path of the server. She should be able to enter a simple url, such as http://plex.example.com to get to Plex, so let’s figure out how to do that.

Complex
http://68.194.31.155:32400/web
Easy
http://plex.example.com

The Solution

The first step is to make it easy for her to reach the home machine. This one is easy as there is an entire infrastructure older than I am to match easy-to-remember names to hard-to-remember computer IP addresses: DNS. First, I added a plex subdomain to one of my existing domain names. Then, I use a service called DynDNS to keep this subdomain up-to-date with my home’s external IP address. I could have written a script to do this for me, but I already had DynDNS setup for other reasons. Now we are looking good:

Good
http://plex.example.com:32400/web

The next step is to get rid of that pesky port. To accomplish this, instead of having my router forward port 32400 to the server, I set it up to forward port 80 instead. Port 80 is the default http port, so omitting a port in the url is the same as using port 80. I wasn’t using the server to host any other webpages, so hijacking the whole system to serve Plex was fine for me.

Port forwarding is only half the problem though. Plex does not have a configuration setting to listen on port 80, so I had to reconvert traffic on port 80 to redirect back to port 32400. You do this in your apache httpd.conf file on the server. From here on out the instructions are Mac OS X 10.8 specific. It may work on *nix machines, but your mileage may vary. I added the following code to /private/etc/apache2/httpd.conf.

1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost *:80>
  ServerName plex.example.com

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyRequests Off
  ProxyPreserveHost On
  ProxyPass / http://127.0.0.1:32400/
  ProxyPassReverse / http://127.0.0.1:32400/
</VirtualHost>

Then restart your apache by opening Terminal.app and running sudo apachectl restart to apply the changes. This is taking all traffic on port 80, then internally passing it off to port 32400. Now we are looking better:

Better
http://plex.example.com/web

The next step is getting the /web off the end. In the httpd.conf file from above, add this snippet just before the closing VirtualHost tag.

1
2
3
4
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/web
RewriteCond %{HTTP:X-Plex-Device} ^$
RewriteRule ^/$ /web/$1 [R,L]

The X-Plex-Device line is a clever bit from Spike Grobstein that allows the internal Plex Media Server communication to work still as it requires the Web service url to be unaltered. Thanks for the comment Emmanuel Papin!

Then restart your apache by opening Terminal.app and running sudo apachectl restart to apply the changes. This takes any url that does not start with /web and rewrites it to /web. Now we are looking perfect:

Perfect
http://plex.example.com

The final step is a bit of security. Because I have now opened up this server to the world, I need to put some password protection on it. There are no bank passwords or crazy secrets on this media server, not even private home videos, so BASIC HTTP Auth will do fine. If you are following along this tutorial to setup your own server, I suggest you do the same. In the httpd.conf file from above, add this snippet just after the ServerName line.

1
2
3
4
5
6
<Location />
  AuthType Basic
  AuthName "Though shalt not pass!"
  AuthUserFile /private/etc/apache2/htpasswd
  Require valid-user
</Location>

If you do not have a htpasswd file created already (you probably don’t), you can create one by running the following commands in Terminal.app

  1. cd /private/etc/apache2
  2. sudo htpasswd -bc htpasswd [username] [password] (replace [username] and [password] with your desired username and password.)

Then restart your apache by opening Terminal.app and running sudo apachectl restart to apply the changes. Now you have an easy-to-remember, publicly accessible, secure URL for you home media server to watch The Walking Dead from anywhere in the world. Enjoy!

TL;DR

  1. Setup a DNS entry for plex.yoursite.com box using DynDNS.
  2. Setup port forwarding on port 80 to your Mac server’s port 80.
  3. Add the following code within private/etc/apache2/httpd.conf then restart the server by running sudo apachectl restart
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
<VirtualHost *:80>
    ServerName plex.yoursite.com

    <Location />
        AuthType Basic
        AuthName "Restricted area"
        AuthUserFile /private/etc/apache2/htpasswd
        Require valid-user
    </Location>

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:32400/
    ProxyPassReverse / http://127.0.0.1:32400/

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/web
    RewriteCond %{HTTP:X-Plex-Device} ^$
    RewriteRule ^/$ /web/$1 [R,L]
</VirtualHost>

Lastly, I run a small software company called Urban Apps. It pays the bills so I can take the time to write helpful posts like this one. If you found this posting helpful at all, I would really appreciate it if you would check out my Apps on the iTunes App Store.

Comments

My name is Matt Coneybeare, I design and develop for iOS (iPhone, iPad and iPod Touch), Mac OS X and the Web out of New York. In 2008 I started a software company called Urban Apps that has made some pretty popular apps such as Ambiance and Hourly News. My current Stack Overflow reputation is about 27k.

I was a Rockstar a decade ago, but then went back to school and collected a Bachelor's Degree in Computer Science from U.C. Berkeley. Now I am settled down with my beautiful wife Di and our two doggies Hamachi and Foxy. While coding, I walk several miles/day on my Treadmill Desk. When not at my desk, I love exploring New York City as a Yelp Elite, or training for marathons.

Contact information

Name
Matt Coneybeare
Email
Website
Twitter
Instagram
GitHub
Google+
LinkedIn