Wednesday, October 10, 2012

Using the Raspberry Pi as a Web Server, Media Server, and Torrent Box

So you've set your Raspberry Pi up.
If not you might want to check out this page first.
So now you want to set your Raspberry Pi up as a media/web/everything else server.

Before any of these, make sure your raspberry pi ip address is static. You can set this by opening up a terminal and typing in:
sudo nano /etc/network/interfaces

(Feel free to use gedit or vim or whatever you want. I like vim)
You should now be looking at a file. Change the line:
iface eth0 inet dhcp
iface eth0 inet static

where the gateway is your router and the address is the ip address you want. Save the file and reboot (sudo reboot) and now your ip should be static.

Luckily, SSH is already enabled on the Raspbian image so we won't go into that. However, if you want to SSH externally, you should install something like SSHGuard for protection and then in your router config redirect port 22 traffic to your Raspberry Pi.

Section 1: Webserver

Installing apache is easy. Just open a terminal and type:
sudo apt-get install apache2

This will set up a webserver for you with the files at /var/www being your web directory. Modify those files how you feel like and if you want to make it publicly accessible then configure your router to forward port 80 to your raspberry pi's ip address.

Section 2: VNC

This is an optional thing since you can port X over SSH. But if you want vnc. All you have to do is open up a terminal and type:
sudo apt-get install tightvncserver

When it is finished installing it should ask for a password. If it doesn't or you need to change it. Just type vncpasswd to reset it.
Then type tightvncserver and a new instance will start. You can kill it by typing tightvncserver -kill :1

To use this externally, you will have to open up port 5801 (for the first instance, 5802 for the second, etc.)

Section 3: Media Server

Start off by mounting all of your external HDDs. If you have a Raid array that is great, if not, I have a handy little hack for you.
edit fstab by opening up a terminal and typing:
sudo nano /etc/fstab
Add each hard drive to the file, mine looks like this:
/dev/sda1 /media/Kingsley ntfs-3g defaults 0 0
/dev/sdb1 /media/HarvardMulligan ntfs-3g defaults 0 0
/dev/sdc1 /media/Moloch ntfs-3g defaults 0 0

(Make sure the folders in /media exist and their permissions are set properly with chmod, otherwise this won't work)

You really should set up a RAID array or something, but lets say you like to live by the seat of your pants and don't care if one of your HDDs fail and you want to access them all at the same time in one convenient directory. To do this you can use mhddfs. Install it by typing:
sudo apt-get install mhddfs

When its done installing, open fstab back up and after the lines with your HDD, type in something like what is shown below:
mhddfs#/media/Kingsley,/media/HarvardMulligan,/media/Moloch /media/ALLOFIT fuse defaults,allow_other 0 0

So my final fstab file has this at the bottom:
/dev/sda1 /media/Kingsley ntfs-3g defaults 0 0
/dev/sdb1 /media/HarvardMulligan ntfs-3g defaults 0 0
/dev/sdc1 /media/Moloch ntfs-3g defaults 0 0
mhddfs#/media/Kingsley,/media/HarvardMulligan,/media/Moloch /media/ALLOFIT fuse defaults,allow_other 0 0

where mhddfs mounts all of the drives to /media/ALLOFIT (which I previously created and set permissions for) and I can write to that and mhddfs will figure out where to put it for me.
Now just sudo reboot

Interesting tidbit: You can use sshfs (their is a Windows version call win-sshfs) to access these drives securely through ssh. This way you don't have to worry about Samba and you can access these files graphically using the internet.

Section 4: Torrent Box

So now you want to be able to torrent (let's assume completely legally) things directly to your server/external HDDs.
Simply install transmission by opening up a terminal and typing
sudo apt-get install transmission-daemon

Now we need to edit some settings, stop the daemon and open up the settings file by typing:
sudo service transmission-daemon stop
sudo nano /etc/transmission-daemon/settings.json

You will need to change a couple of settings.
Change your download directory to where you want your downloads, I use my external HDDs:  "download-dir": "/media/ALLOFIT",
Now to set up some security. Change the following text requires the ""s but numbers and true/false don't:
"rpc-authentication-required": true,
"rpc-enabled": true,
"rpc-password": "YourPasswordHere",
"rpc-port": 6669,
"rpc-username": "YourUserNameHere",

You can change the port to anything but it's probably a good idea to change it from the default to avoid brute force script kiddies.
Now start the daemon back up by typing
sudo service transmission-daemon start

Now open up a webbrowser on any computer in your network and type in your raspberry pi's ip address followed by :port. Ex:
If you want to access this externally, just forward the port you chose to your raspberry pi ip address in your router config.

Now you are done.

To use transmission to its fullest potential and automatically download media:

Check out my other Raspberry Pi Fixes/How tos:

Consider donating to further my tinkering.

Places you can find me


  1. Hi I've installed transmission and edited the settings file as specified but I can't stop or start the service. I'm new to Linux but have some computer experience on other systems. I have done a ps on my user (pi) and on root (ps -U root) and I can't see any transmission processes running. I get the following response to a start. I've edited the settings file to run the service under the pi user and restarted my pi but that made no difference. Am I missing something?

    sudo service transmission-daemons start
    transmission-daemons: unrecognized service


  2. Hi again - now realised there is a typo in the article - says daemons not daemon - all works when I correct that. Great article - thanks


  3. Whoops. I will fix that. I'm a bit lazy so I always tab-complete. So I just type:
    sudo servi[TAB] trans[TAB] start

  4. Transmission as your raspberry pi torrent server/client, really? Couldn't think of it. I found it better (for myself) to stick with deluge ony my raspberry pi with a clean webinterface.

    1. I've seen a lot of posts on why deluge is better but I tend to disagree. Transmission has a very similar web interface that is also password protected. It also is faster and has less of a footprint since it is written in C++. I also can use it through command line, the GUI, or the web interface.

      It's also useful since I can text message to it to make it download a torrent. Or use a script to automatically download a torrent:

    2. Interesting. I will check it out.

      Texting is probably no option (at least in Germany) where a short text message (160chars) is still about € 0,19 for contracted customers. I put my torrent files into a defined folder (might be public some time). As for CLI, GUI, Web, deluge and transmission are probably somewhat similar.

      The more interesting thing is the smaller footprint. Another point is that deluge on the current raspian image does not allow magnet links to be inserted.

      Many thanks, I will look into it!