Setting Up rtorrent/rutorrent on Ubuntu 14.04 Using Ngnix

I recently put together a low-power Ubuntu server that I’ve been using to host a couple of my projects and manage my local backup storage. I’ve been trying to reduce my monthly energy bill as much as possible and this means I try to keep my desktop off unless I’m actively working on it. I ran into a situation where I had to download a very large file using BitTorrent, but didn’t want to waste a couple days’ worth of power to keep the desktop mostly idle. It was then that I decided to set up rtorrent/rutorrent on my low-power server.

I found a great tutorial put together by Ben over at vassie.me which can be found here. It got me about 90% of the way there, but a recent update to Ngnix broke a couple of things and I wanted to make sure that I documented what I learned from setting up my server. If you’re looking to set up rtorrent/rutorrent on Ubuntu 14.04 via SSH, then you’ve come to the right place!

Just for reference, the server I’m using has a couple other services running on it which are happily coexisting. They are:

  • Samba
  • Plex
  • Netatalk
  • Teamspeak3
  • rtorrent/rutorrent

Not bad for 65 watts!

The first thing we have to do is install all of our dependencies. This usually means entering them all into the terminal and letting Ubuntu handle the installation, but in this case we have to add a dependency that isn’t distributed with newer versions of the OS!

Let’s edit our sources list to add the PPA to our system and install our dependencies.

sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install rtorrent unzip unrar mediainfo ffmpeg curl php5-fpm php5-cli php5-geoip

I chose to set up my download and watch directories on my btrfs file system which is mounted in /mnt/main/ so I’ll be using that in my configuration. Feel free to change it as you see fit! I also chose to use my main administrator account to run rtorrent, but you could just as easily create a user, add the account to the www-data group, and make sure that it has read/write permissions to the data directories.

Let’s make our folders! In this section you can replace ~ (the symbol for the home directory of the current user) to your other user’s path (/home/MYUSER/…).

mkdir ~/rtorrent/.session
mkdir -p /mnt/main/rtorrent/{watch,download}

Great! We’ve got folders, now we need configuration files. Let’s make one in the .session directory you just created. I like to use nano to edit configuration files (and hate vi with a passion) so I’ll be using that!

nano ~/.rtorrent.rc

Paste the following into your new file. This is a very basic configuration for rtorrent which should get you up and running. Be sure to modify the sections shown in red below!

# This is an example resource file for rTorrent. Copy to
# ~/.rtorrent.rc and enable/modify the options as needed. Remember to
# uncomment the options you wish to enable.
 
# Maximum and minimum number of peers to connect to per torrent.
#min_peers = 40
#max_peers = 100
 
# Same as above but for seeding completed torrents (-1 = same as downloading)
#min_peers_seed = 10
#max_peers_seed = 50
 
# Maximum number of simultanious uploads per torrent.
#max_uploads = 15
 
# Global upload and download rate in KiB. "0" for unlimited.
#download_rate = 0
#upload_rate = 0
 
# Default directory to save the downloaded torrents.
directory = /mnt/main/rtorrent/download
 
# Default session directory. Make sure you don't run multiple instance
# of rtorrent using the same session directory. Perhaps using a
# relative path?
session = ~/rtorrent/.session
 
# Watch a directory for new torrents, and stop those that have been
# deleted.
schedule = watch_directory,5,5,load_start=/mnt/main/rtorrent/watch/*.torrent
schedule = untied_directory,5,5,stop_untied=
 
# Close torrents when diskspace is low.
#schedule = low_diskspace,5,60,close_low_diskspace=100M
 
# Stop torrents when reaching upload ratio in percent,
# when also reaching total upload in bytes, or when
# reaching final upload ratio in percent.
# example: stop at ratio 2.0 with at least 200 MB uploaded, or else ratio 20.0
#schedule = ratio,60,60,"stop_on_ratio=200,200M,2000"
 
# The ip address reported to the tracker.
#ip = 127.0.0.1
#ip = rakshasa.no
 
# The ip address the listening socket and outgoing connections is
# bound to.
#bind = 127.0.0.1
#bind = rakshasa.no
 
# Port range to use for listening.
port_range = 49164-49164
 
# Start opening ports at a random position within the port range.
#port_random = no
 
# Check hash for finished torrents. Might be usefull until the bug is
# fixed that causes lack of diskspace not to be properly reported.
#check_hash = no
 
# Set whetever the client should try to connect to UDP trackers.
#use_udp_trackers = yes
 
# Alternative calls to bind and ip that should handle dynamic ip's.
#schedule = ip_tick,0,1800,ip=rakshasa
#schedule = bind_tick,0,1800,bind=rakshasa
 
# Encryption options, set to none (default) or any combination of the following:
# allow_incoming, try_outgoing, require, require_RC4, enable_retry, prefer_plaintext
#
# The example value allows incoming encrypted connections, starts unencrypted
# outgoing connections but retries with encryption if they fail, preferring
# plaintext to RC4 encryption after the encrypted handshake
#
encryption = allow_incoming,enable_retry,prefer_plaintext
 
# Enable DHT support for trackerless torrents or when all trackers are down.
# May be set to "disable" (completely disable DHT), "off" (do not start DHT),
# "auto" (start and stop DHT as needed), or "on" (start DHT immediately).
# The default is "off". For DHT to work, a session directory must be defined.
# 
# dht = auto
 
# UDP port to use for DHT. 
# 
# dht_port = 6881
 
# Enable peer exchange (for torrents not marked private)
#
# peer_exchange = yes
 
#
# Do not modify the following parameters unless you know what you're doing.
#
 
# Hash read-ahead controls how many MB to request the kernel to read
# ahead. If the value is too low the disk may not be fully utilized,
# while if too high the kernel might not be able to keep the read
# pages in memory thus end up trashing.
#hash_read_ahead = 10
 
# Interval between attempts to check the hash, in milliseconds.
#hash_interval = 100
 
# Number of attempts to check the hash while using the mincore status,
# before forcing. Overworked systems might need lower values to get a
# decent hash checking rate.
#hash_max_tries = 10
 
scgi_port = 127.0.0.1:5000

Next, let’s install Nginx, our web server! Once again, we have to add a repository in order to install. Copy and paste the following into the terminal.

sudo add-apt-repository ppa:nginx/stable
sudo apt-get update ; sudo apt-get install nginx

Next, let’s check to see whether you have a www directory or not. Unless you’re doing some sort of complex web server install, it should always be in /var/www

cd /var/www

If you can’t access the folder, go ahead and create one.

sudo mkdir /var/www
sudo chown www-data:www-data /var/www

If the folder does exist, only run the second line shown above. This command changes the ownership of the folder to the user www-data and the group www-data.

Next, let’s download rutorrent and its plugins to the home directory. The tar function will decompress the files from the archive and the folder location at the end of the command will move the files to their new home in /var/www

cd
wget http://dl.bintray.com/novik65/generic/rutorrent-3.6.tar.gz
wget http://dl.bintray.com/novik65/generic/plugins-3.6.tar.gz
sudo tar xvf rutorrent-3.6.tar.gz -C /var/www
sudo tar xvf plugins-3.6.tar.gz -C /var/www/rutorrent
sudo chown www-data:www-data -R /var/www/rutorrent

Now that we’ve got our files in place, let’s configure Nginx.

sudo nano /etc/nginx/sites-available/default

Inside this file, let’s find the line that begins with root and change it to our new root folder location.

root /var/www;

Next, let’s find the line that begins with index and make sure only the files below are shown. This should get rid of that pesky default webpage.

index index.php index.html index.htm;

Find the section under “#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000” and add the following. If editing this file seems a bit confusing, I’ve linked a copy of my configuration file below. Feel free to download it and compare it to your file, or just upload it to your server!

The “fastcgi_split_path_info” line is very important! Be sure to check that you don’t have any erroneous carriage returns or missing characters!! This is part of the fix for the updated Nginx server. More details are found here.

 #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 location ~ .php$ {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi.conf;
 }

Finally, just above the final curly bracket ‘ } ‘ and the block of text titled “Virtual Host configuration for example.com” paste the following in.

 location /RPC2 {
 include scgi_params;
 scgi_pass 127.0.0.1:5000;
 }

 location /rutorrent {
 auth_basic "Restricted";
 auth_basic_user_file /var/www/rutorrent/.htpasswd;
 }

These two commands give rtorrent access to the rutorrent front end, and force rutorrent to be password protected! Next we will create a user/pass combination used to access the server. Let’s run the following. Replace USERNAME with your desired user name.

sudo apt-get install apache2-utils
sudo htpasswd -c /var/www/rutorrent/.htpasswd USERNAME
sudo chown www-data:www-data /var/www/rutorrent/.htpasswd

After running the htpasswd command, you should be asked for a password. Keep in mind that whatever you enter will be used as your password to access the server!

We’re getting close to the end!! Let’s configure rutorrent next.

sudo nano /var/www/rutorrent/conf/config.php

Edit the file to match the lines listed below.

"curl" ==> '/usr/bin/curl', // Something like /usr/bin/curl. If empty, will be found in PATH.
"stat" ==> '/usr/bin/stat', // Something like /usr/bin/stat. If empty, will be found in PATH.

If you’re setting up your server using a single user account like I did, go ahead and add your user to the www-data group so you can read/write files!

sudo usermod -a -G www-data USERNAME

I have to be honest, I had never heard of Byobu before working on this project, but I gave it a shot and it’s awesome! Byobu is a session manager similar to screen, but with much nicer features! More info can be found here, and if you’re like me and are using puTTY to SSH into your server, then you’ll want to take a look at this page for a function key fix. Finally, here’s a cheat sheet of all the keyboard commands! In order to install it, just run the following commands.

sudo add-apt-repository ppa:byobu/ppa
sudo apt-get update ; sudo apt-get install byobu

And this command should enable it. Here’s a quick rundown of the commands: F2 = Open a New Session, F3 = Previous Window, F4 = Next Window, F8 = Rename Window, CTRL + D = Close Window, F6 = Detach Session

byobu-enable

Finally, let’s restart Nginx and php using the commands below and start rtorrent!

sudo service nginx restart
sudo service php5-fpm restart
rtorrent

Head over to http://YOURIPADDRESS/rutorrent and you should be greeted with a login page! Use the user/pass you created earlier and you should see the rutorrent interface!

Capture

 

Enjoy!

P.S. My Nginx configuration file is located here. You might want to open it up in Notepad++ or Sublime Text since Notepad makes a mess of things.

Comments

comments