Install and Configure ruTorrent on Raspberry Pi

Install and Configure ruTorrent on Raspberry Pi

If you want to download torrents to Raspberry Pi, it's possible. There are a few options to do so, but the most lightweight option is rTorrent with ruTorrent. I prefer using WebGUI to manage torrents from anywhere (thanks to DDNS). 

If you are planning to buy a new Raspberry Pi, I recommend the Raspberry Pi 4B with 2GB RAM, I am perfectly satisfied with it.

This article will show you how to install and configure rTorrent, ruTorrent, and Apache on Raspberry Pi.

Note: I don't recommend installing them with a script from the Internet, I tried it once.  Long story short: I had to reinstall my Raspbian.

Note2: Do not download to an SD card, it'll ruin it quickly. Buy one HDD and connect it instead.

In this article we will install these packages (and dependencies):

  • rTorrent - torrent service
  • ruTorrent - WebGUI to manage torrents
  • Apache - web service to serve ruTorrent

Let's get started!

Note: feel free to change the path or username in these commands, but be careful and change them in every relevant command! I used /mnt/Torrent path where I want to download the content of torrents to, the username is "rtorrent", and /var/www/rutorrent/ path where all ruTorrent files are stored.

  1. Open SSH connection to your Raspberry Pi

  2. Update all packages
    sudo apt update

  3. Create "rtorrent" user, it will run rtorrent service
    sudo adduser --disabled-login --disabled-password --home /mnt/Torrent --system rtorrent

  4. Install all required packages
    sudo apt -y install php php7.3-cli php7.3-json php7.3-curl php7.3-cgi php7.3-mbstring libapache2-mod-php libapache2-mod-scgi libapache2-mod-xsendfile apache2 libav-tools ffmpeg mediainfo curl screen sqlite3 git net-tools sox rtorrent libtorrent20 libxmlrpc-core-c3 git

  5. Create and set correct permission for "session" folder
    sudo mkdir -p /mnt/Torrent/session
    sudo chmod 777 -R /mnt/Torrent/session
    sudo chown rtorrent:rtorrent -R /mnt/Torrent/session

  6. Create a new file called "rtorrent.rc", this is the configuration file of rTorrent
    sudo nano /mnt/Torrent/.rtorrent.rc

    network.scgi.open_port = "127.0.0.1:5000"
    throttle.min_peers.normal.set = 10
    throttle.max_peers.normal.set = 50
    throttle.min_peers.seed.set = 20
    throttle.max_peers.seed.set = 60
    throttle.max_uploads.set = 30
    network.http.dns_cache_timeout.set = 0
    system.file.allocate.set = 1
    pieces.preload.type.set = 2
    throttle.global_down.max_rate.set_kb = 0
    throttle.global_up.max_rate.set_kb = 0
    directory.default.set = /mnt/Torrent
    session.path.set = /mnt/Torrent/session
    schedule2 = session_save, 1200, 43200, ((session.save))
    network.port_range.set = 55001-55001
    network.port_random.set = no
    pieces.hash.on_completion.set = no
    trackers.use_udp.set = yes
    protocol.encryption.set = allow_incoming,try_outgoing,enable_retry
    dht.mode.set = disable
    dht.port.set = 6881
    protocol.pex.set = no
    execute2 = {sh,-c,/usr/bin/php /var/www/rutorrent/php/initplugins.php torrent &}
    network.http.max_open.set = 50
    network.max_open_files.set = 600
    network.max_open_sockets.set = 300
    method.set_key = event.download.erased,delete_erased,"execute=rm,-rf,--,$d.base_path="
  7. Set correct permission for ".rtorrent.rc"
    sudo chmod 777 /mnt/Torrent/.rtorrent.rc

  8. Create folder for ruTorrent
    sudo mkdir -p /var/www/rutorrent/

  9. Download ruTorrent from GitHub
    sudo git clone https://github.com/Novik/ruTorrent.git /var/www/rutorrent/

  10. Grant permission for this new folder
    sudo chown -R www-data:www-data /var/www/rutorrent

  11. Edit ruTorrent configuration file
    sudo nano /var/www/rutorrent/conf/config.php

    Replace these values in this file:
    $topDirectory = '/mnt/Torrent';
    "php"   => '/usr/bin/php',
    "curl"  => '/usr/bin/curl',
    "gzip"  => '/bin/gzip',
    "id"    => '/usr/bin/id',
    "stat"  => '/usr/bin/stat',
  12. Secure ruTorrent WebGUI with password (change the username)
    sudo htpasswd -c /var/www/rutorrent/.htpasswd username

    Do not forget the username and password!

  13. Configure Apache to use this new ".htpasswd" file
    sudo nano /var/www/rutorrent/.htaccess

    AuthUserFile /var/www/rutorrent/.htpasswd
    AuthName "ruTorrent_login"
    AuthType Basic
    require valid-user
    <Files ~ "^.*\.([Hh][Tt][Aa])">
            Order allow,deny
            Deny from all
            Satisfy all
    </Files>
    Options -Indexes
  14. Configure Apache to serve ruTorrent web interface (Change the path in 2nd line - security reason against scanner bots)
    sudo nano /etc/apache2/sites-available/rutorrent.conf

    <IfModule alias_module>
    Alias /rutorrent        /var/www/rutorrent/
            <Directory /var/www/rutorrent/>
                    Options -Indexes -Includes -FollowSymLinks +MultiViews
                    AllowOverride all
                    Require all granted
            </Directory>
    </IfModule>
  15. Enable this new website
    sudo ln -s /etc/apache2/sites-available/rutorrent.conf /etc/apache2/sites-enabled/rutorrent.conf

  16. Enable necessary Apache modules
    sudo a2enmod auth_digest authn_file xsendfile scgi

  17. Edit Apache config file for scgi
    nano /etc/apache2/sites-available/rtorrentscgi.conf
    <LocationMatch "/RPC2">
    AuthType        Basic
    AuthName        "rtorrentscgi"
    AuthUserFile    /var/www/rutorrent/.htpasswd
    Require         valid-user
    BrowserMatch    "MSIE"  AuthDigestEnableQueryStringHack=On
    Require ip 127.0.0.1
    </LocationMatch>
    SCGIMount /RPC2 127.0.0.1:5000
  18. Enable scgi in Apache
    ln -s /etc/apache2/sites-available/rtorrentscgi.conf /etc/apache2/sites-enabled/rtorrentscgi.conf

  19. Create systemd service for rTorrent
    nano /etc/systemd/system/rtorrent.service

    [Unit]
    Description=rTorrent
    After=network.target

    [Service]
    UMask=002
    Type=forking
    RemainAfterExit=yes
    KillMode=none
    User=rtorrent
    Restart=on-failure
    RestartSec=5
    StartLimitInterval=60s
    StartLimitBurst=3
    ExecStartPre=-/bin/rm -f /mnt/Torrent/session/rtorrent.lock
    ExecStart=/usr/bin/screen -d -m -fa -S rtorrent /usr/bin/rtorrent
    ExecStop=/usr/bin/killall -w -s 2 /usr/bin/rtorrent
    WorkingDirectory=/mnt/Torrent/

    [Install]
    WantedBy=multi-user.target
  20. Start and enable rTorrent service
    systemctl start rtorrent
    systemctl enable rtorrent.service

  21. Check the WebGUI
    Check IP address of Raspberry Pi:
    hostname -I

    Replace the IP and try to open in the browser like this:
    192.168.0.102/rutorrent

    Note: if you have changed the default path in step 14, do not forget to replace it as well as in here.

    It should work :) Login info provided by you in the step 12.

    If it doesn't work for some reason, check the Apache and rTorrent services.
    systemctl status apache2 rtorrent

  22. Let's continue with optimization steps

    Secure Apache by adding the below lines at the end of apache2.conf file
    nano /etc/apache2/apache2.conf

    ServerSignature Off
    ServerTokens Prod
    <IfModule mod_headers.c>
    Header unset ETag
    </IfModule>
    FileETag None
  23. Remove the "index.php" file and create an empty "index.html" instead
    rm /var/www/html/index.php
    echo “” > /var/www/html/index.html
    chmod 444 /var/www/html/index.html


  24. Disable useless logging (extend MicroSD lifetime)
    a2disconf other-vhosts-access-log
    systemctl reload apache2

  25. Disable unnecessary Apache modules (save memory)
    a2dismod proxy_http
    a2dismod proxy
    a2dismod status
    systemctl restart apache2


  26. That's all. Have fun!

  27. Optional: Install RPI-Monitor to check Memory and HDD usage from the web browser.

    If it doesn't work for some reason, check the Apache and rTorrent services.
    systemctl status apache2 rtorrent


    If you have a question, feel free to leave a comment below.

Comments