FreeBSD install rtorrent and ruTorrent on lighttpd full guide.

This tutorial, described how to set up a torrent server on the FreeBSD operating system. The torrent server is based on rtorrent net-p2p/rtorrent. The GUI is ruTorrent. GUI is written in php. The server with php was lighttpd. www/lighttpd.

Start by installing the server.
Code:
pkg install lighttpd

Then edit the configuration file.
Code:
ee /usr/local/etc/lighttpd/lighttpd.conf

Change the port to 8080 from 80
Code:
server.port = 8080

Disable ipv6.
Code:
server.use-ipv6 = "disable"

Change the directory of the website to the directory where ruTorrent will be.
Code:
server.document-root = "/usr/local/www/lighttpd/ruTorrent"

Bind socket, which is not required when ipv6 is disabled.
Code:
#$SERVER["socket"] == "0.0.0.0:80" { }
That's all on this file. Save change.

Adds an entry to /etc/rc.conf
Code:
sysrc lighttpd_enable="yes"

Find and install highest version of php.
Code:
root@1234:/var/cache/pkg # pkg search php
root@1234:/var/cache/pkg # pkg install php81

Edit configuration file of /usr/local/etc/php-fpm.d/www.conf
Code:
ee /usr/local/etc/php-fpm.d/www.conf

Change the line:
Code:
listen = 127.0.0.1:9000

For the following entry
Code:
listen = /var/run/php-fpm.sock

Also change the following to what is shown below.
By uncommenting ; and assigning permissions.
Code:
listen.owner = www
listen.group = www
listen.mode = 0660
Save change

Copy the sample and put it in a proper folder.
Code:
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini

Adds an entry to /etc/rc.conf
Code:
sysrc php_fpm_enable="yes"

At the file /usr/local/etc/lighttpd/modules.conf
Add the modules.
Code:
ee /usr/local/etc/lighttpd/modules.conf

Add the following entries:
Code:
server.modules = (
        "mod_authn_file",
        "mod_auth",
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
        "mod_fastcgi",
)

In the same file find the following line and debind it (remove #)
Code:
 conf_dir + "/conf.d/fastcgi.conf"
Save changes

In the file /usr/local/etc/lighttpd/conf.d/fastcgi.conf
Code:
ee /usr/local/etc/lighttpd/conf.d/fastcgi.conf

Adds the following entries.

Code:
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_scgi" )

In the same file add the following entries.
Code:
fastcgi.server += ( ".php" =>
        ((
                "socket" => "/var/run/php-fpm.sock",
                "broken-scriptfilename" => "enable"
        ))
)

scgi.server = ( "/RPC2" =>
    ( "127.0.0.1" =>
        (
            "host" => "127.0.0.1",
            "port" => 6000,
            "check-local" => "disable"
        )
    )
)
Save changes

Port 6000 is the scgi port for rtorrent. It may be configured freely

Adds an entry to /etc/rc.conf
Code:
sysrc lighttpd_enable=yes

Enable installed modules.
Code:
root@1234:/var/cache/pkg # service php-fpm start
Performing sanity check on php-fpm configuration:
[18-May-2022 23:21:50] NOTICE: configuration file /usr/local/etc/php-fpm.conf test is successful

Starting php_fpm.
root@1234:/var/cache/pkg # service lighttpd start
Starting lighttpd.

Installs wget
Code:
pkg install wget

Wget the RuTorrent.
Code:
wget https://github.com/Novik/ruTorrent/archive/refs/heads/master.zip

Unzip them. When ask pres A and enter.
Code:
unzip master.zip

Code:
root@1234:/var/cache # unzip master.zip
Archive:  master.zip
   creating: ruTorrent-master/
   creating: ruTorrent-master/.github/
   creating: ruTorrent-master/.github/ISSUE_TEMPLATE/
replace ruTorrent-master/.github/ISSUE_TEMPLATE/bug_report.yml? [y]es, [n]o, [A]ll, [N]one, [r]ename:

It extracted, the folder that contains RuTorrent is called ruTorrent-master.
Renaming a directory to a shorter one.
Code:
mv ruTorrent-master ruTorrent

Move ruTorrent to the previously defined root directory using the move command
Code:
mv ruTorrent /usr/local/www/lighttpd/ruTorrent

Create missing the directory for cache/history added .torrent file.
Code:
mkdir -p /usr/local/www/lighttpd/ruTorrent/php/share/torrents

In ruTorrent directory create info.php file.
Code:
ee /usr/local/www/lighttpd/ruTorrent/info.php

And paste the content below.
Code:
<?php phpinfo(); ?>
Save changes

To complete the formalities, change the owner and group to www:www for all files, as shown below.
This is done with the chown command, with the -R option.
Code:
chown -R www:www /usr/local/www/lighttpd/ruTorrent

Ad permission.
Code:
chmod -R 0775 /usr/local/www/lighttpd/ruTorrent

Check if the php server is running.
Enter the address and port of the server,
For me: http://10.1.1.137:8080/info.php
There should be a php server prompt.
rutorrent01.jpg

Remove info.php from directory.
Code:
rm /usr/local/www/lighttpd/ruTorrent/info.php

Install the rtorrent program.
Code:
pkg install rtorrent

Install the screen program.
Code:
pkg install screen

Add a dummy user to run the torrent on
Code:
pw useradd rtorrent -s /sbin/nologin

Create working folders for the torrent
Code:
mkdir -p /usr/local/rtorrent/downloads
mkdir -p /usr/local/rtorrent/sessions
chown -R rtorrent:rtorrent /usr/local/rtorrent/

Once the program is properly installed, add the basic settings for the rtorrent daemon to /etc/rc.conf.
Code:
sysrc rtorrent_enable="YES"
sysrc rtorrent_scgi_port="127.0.0.1:6000"

Don't set anything else in /etc/rc.conf because it will be a mess.

Configure it to work as a daemon. The program running as a daemon does not use rtorrent.rc Based on the link below, the rtorrent daemon as built, which is also the main configuration file. https://anton-pribora.ru/articles/os/freebsd/rotrrent-web-panel-freebsd/ This is done this way, ot that way, because there is no way to save the torrent.rc file from within ruTorrent running on the server. Also, setting the ruTorrent settings on the client, works until reboot. In the daemon it is permanent. All functions from the manual RTORRENT(1)() are rewrited and can be changed in this file, between entries:

#START USER CONFIGURATION

and

#END USER CONFIGURATION

Single letter options, should work by adding an entry to

: ${rtorrent_args=""}

Create a deamon
Code:
ee /usr/local/etc/rc.d/rtorrent

And paste the following entry.
https://pastebin.com/YTeVgWqR
Because it is not possible to add an post larger than 25000 characters,
The daemon is attached at the end of the post as a text file.


After saving the file, enter the daemon once again and set the base configuration by editing the following entries.
Code:
ee /usr/local/etc/rc.d/rtorrent

This command must be set.
: ${rtorrent_user="rtorrent"} - Set user who running rtorrent
: ${rtorrent_directory="/usr/local/rtorrent/downloads"} - location of downloads directory
: ${rtorrent_session="/usr/local/rtorrent/sessions"} - lotation of session directory

Grant execution privileges to the torrent daemon.
Code:
chmod +x /usr/local/etc/rc.d/rtorrent

Starting deamon.
Code:
root@1234:/usr/local/www # service rtorrent start
[: missing ]
Starting rtorrent.
root@1234:/usr/local/www # service rtorrent status
[: missing ]
rtorrent is running as pid 53973.
root@1234:/usr/local/www #
As can bee seen. It works.

Configuring the ruTorrent client
Code:
ee /usr/local/www/lighttpd/ruTorrent/conf/config.php

And change follows.
Define scgi_port and scgi_host to what is specified earlier.
Code:
    $scgi_port = 6000;
    $scgi_host = "127.0.0.1";

In this block, there are given paths to programs that use ruTorrent plugin.
Code:
        $pathToExternals = array(
                "php"   => '/usr/local/bin/php',                        // Something like /usr/bin/php. If empty, will be found in PATH.
                "curl"  => '/usr/local/bin/curl',                       // Something like /usr/bin/curl. If empty, will be found in PATH.
                "gzip"  => '/usr/bin/gzip',                     // Something like /usr/bin/gzip. If empty, will be found in PATH.
                "id"    => '/usr/bin/id',                       // Something like /usr/bin/id. If empty, will be found in PATH.
                "stat"  => '/usr/bin/stat',                     // Something like /usr/bin/stat. If empty, will be found in PATH.
                "pgrep" => '/usr/bin/pgrep',
                "sox"   => '/usr/local/bin/sox',
                "python" => '/usr/local/bin/python',
                "unrar" =>  '/usr/local/bin/unrar',
                "mediainfo" =>  '/usr/local/bin/mediainfo',
                "ffmpeg" =>  '/usr/local/bin/ffmpeg',
        );

Install missing programs for ruTorrent plugins.
Code:
pkg install ffmpeg
pkg install mediainfo
pkg install unrar
pkg install python
pkg install sox

In this file provide the path to the program. These programs are used by ruTorrent plugins. If there are problems with the plugin, make sure the path to the file is correct. It may happen that newer versions of programs are in a different folder than they are now. Or new plugin will be created. If this happens, use the find command, and find the path to the program.

Sample:
Code:
root@1234:/usr/local/www # find / -name python
/usr/local/bin/python

At this point
Have ruTorrent with rtorrent on FreeBSD ready to run.

Is only need to disable the problematic plugins.
For this we go to the server ruTorrent address. http://10.1.1.138:8080 or http://localhost:8080
In the plugins tab, right-click to disable the following:

Code:
_cloudflare
_noty
_noty2
check_port

Check port queries the external server for our IP address.

rutorrent03.jpg

Support open source by offloading server load.
As can see, it works.
rutorrent04.jpg

At this moment the server is not password protected.
Be sure to block port 8080 on your firewall from outside access
.
Otherwise it is possible to connect to the ruTorrent client by giving the host IP (from ISP) address and port 8080.

============================================================================

Basic password protection.
To protect server resources from unauthorized access, add on the file /usr/local/etc/lighttpd/lighttpd.conf
Code:
ee /usr/local/etc/lighttpd/lighttpd.conf

Follow entry:
Code:
$HTTP["url"] == "/" {

auth.backend = "plain"
auth.backend.plain.userfile = "/usr/local/www/lighttpd/.lighttpdpassword"

    auth.require = (
        "/" => (
            "method" => "basic",
            "realm" => "Password protected area",
            "require" => "user=admin"
        )
    )
}


Where:
auth.backend.plain.userfile = "/usr/local/www/lighttpd/.lighttpdpassword" - path to file were username:mypassword is stored.
"require" => "user=admin" - admin must be set as username admin:mypassword on the .lighttpdpassword file.

Create the file and add username:mypassword entry.
Code:
echo "admin:mypassword" >> /usr/local/www/lighttpd/.lighttpdpassword

Restart lighttpd server.
Code:
service lighttpd restart
============================================================================

The files, including the dependiences from /var/cache/pkg They are attached below.
If a program is missing from the pkg. It is possible to install them from this package.
The package also contains ruTorrent. Command must be run in the directory where the package was extracted.
This is installed by the command pkg add programname

For example:
Code:
root@1234:/home/vbox/pkg # pkg add gzip-1.11.pkg
Installing gzip-1.11...
the most recent version of gzip-1.11 is already installed
root@1234:/home/vbox/pkg #


External links.
https://github.com/Novik/ruTorrent
https://anton-pribora.ru/articles/os/freebsd/rotrrent-web-panel-freebsd
https://wiki.archlinux.org/title/RTorrent/RuTorrent
https://www.digitalocean.com/community/tutorials/how-to-install-lighttpd-with-mysql-and-php-on-freebsd-11-0
Own work.
 

Attachments

  • rtorrent.txt
    13.4 KB · Views: 191
Last edited:
I'm not familiar with this plugin. To save torrents in a new/other location you have to specify the path:

In the file:
/usr/local/etc/rc.d/rtorrent

: ${rtorrent_directory="/home/sample/rtorrent/downloads"}
: ${rtorrent_session="/home/sample/rtorrent/sessions"}

And also grant privileges.
Code:
chmod a+rwx /home/sample/rtorrent/downloads
chmod a+rwx /home/sample/rtorrent/sessions

PS.
By the way, not all manual commands work. I do not know why maybe manual it is old.

PS2.
To restrict access to the server, and allow access from the local network and localhost, add the following entries:
Code:
$HTTP["remoteip"] == "127.0.0.1" {url.access-allow = ("")}
$HTTP["remoteip"] != "10.1.1.0/24" {url.access-deny = ("")}

Into file /usr/local/etc/lighttpd/lighttpd.conf
The whole thing together with the password should look like this:
Code:
$HTTP["url"] == "/" {

$HTTP["remoteip"] == "127.0.0.1" {url.access-allow = ("")}
$HTTP["remoteip"] != "10.1.1.0/24" {url.access-deny = ("")}

auth.backend = "plain"
auth.backend.plain.userfile = "/usr/local/www/lighttpd/.lighttpdpassword"

    auth.require = (
        "/" => (
            "method" => "basic",
            "realm" => "Password protected area",
            "require" => "user=rtorrent"
        )
    )
}

The problem is that it first asks for the password and then it drop the connections. It should be the other way around.

PS3.
There is a typo in the line:
Code:
[ -n "${rtorrent_max_downloads_global}" ] && rtorrent_args="-O ax_downloads_global='${rtorrent_max_downloads_global}' ${rtorrent_args}"

It should be:
Code:
[ -n "${rtorrent_max_downloads_global}" ] && rtorrent_args="-O max_downloads_global='${rtorrent_max_downloads_global}' ${rtorrent_args}"
 
Hey StephanieJones, thanks so much for your guide about how to install ruTorrent on FreeBSD. As one of the collaborators of the not for profit, open source ruTorrent project, I really appreciate you creating this guide about how to install ruTorrent from the master branch. We have put in a lot of work recently into making the project run better on all systems including FreeBSD.

Settings Directory Changes
I would like to reach out to you about the directory we use to store settings (i.e. caches, history etc.). It's not intended for this to be stored in the /php folder. This may cause the web client not to function as intended. Instead, it should be stored in the /share folder.

To accomplish this successfully, the user needs to create a directory to save torrents from the /share folder instead.
Code:
mkdir -p /usr/local/www/lighttpd/ruTorrent/share/torrents

They also need to ensure $profilePath variable is set correctly in the /conf/config.php file. We introduced a custom autoloader for php in version 4.0 with a new directory structure. It reads the profile path for the settings directory from the /php/utility/ folder. Therefore, the $profilePath variable needs to go back 2 directories instead of 1 to reach the root folder.
Code:
$profilePath = '../../share';

Optional Vmtouch Improvement
There is also a vmtouch package for FreeBSD the user can install to improve the stability and performance ruTorrent. When performing hash checks or under high disk load, the web client may become unresponsive or unreachable. To resolve this problem, it's possible install vmtouch and load the web client into memory. This will make ruTorrent run better at the cost of only 15MB of memory.

Install the package:

Code:
pkg install vmtouch

Run this command when initially starting rTorrent:
Code:
vmtouch -i '*.torrent' -m 125K /usr/local/www/lighttpd/ruTorrent/
 
  • Thanks
Reactions: veg
StephanieJones , there is something else you should add. When unraring a file there are mask permissions hidding them from other apps. This can easily be corrected by changing the unmask permissions by going into the jail and nano ~/.cshrc.

I do have a follow up question, im trying to edit the ports to have a specific one as well as turning off the randomizer. Any advice? Its not working When editing /usr/local/etc/rc.d/rtorrent

Also! As a heads up for anyone using this guide but also using a vpn in the same jail to traffic the traffic. If your VPN has all ports open BE CAREFUL. Even though you did not open a port in your router, the client will still be reachable by the vpn’s public ip.

We had another breakthrough.

If you Want that pluging to move files to work you need to do the following:

pkg install php81-extensions

I would also suggest installing vmtouch
: pkg install vmtouch

then going to /usr/local/etc/rc.d/rtorrent and adding

: ${rtorrent_args="-O execute2='vmtouch, -i, *.torrent, -m, 125K, /usr/local/www/lighttpd/ruTorrent/' ${rtorrent_args}"}
 
Last edited by a moderator:
StephanieJones the source code calls the port config with a Network. Would that be any different than port_change? In the rtorrent file that is there now?
Code:
CMD2_VAR_BOOL    ("network.port_open",   true);
  CMD2_VAR_BOOL    ("network.port_random", true);
  CMD2_VAR_STRING  ("network.port_range",  "6881-6999");
  CMD2_ANY         ("network.listen.port",        std::bind(&torrent::ConnectionManager::listen_port, cm));
  CMD2_ANY         ("network.listen.backlog",     std::bind(&torrent::ConnectionManager::listen_backlog, cm));
  CMD2_ANY_VALUE_V ("network.listen.backlog.set", std::bind(&torrent::ConnectionManager::set_listen_backlog, cm, std::placeholders::_2));
 
More additions:

If you run into issues where you cannot upload files larger than 2 mb please follow the steps bellow:

Step One:
Code:
Step One: Nano /usr/local/etc/php.ini

Step Two: locate the following values and change them

upload_max_filesize = 64M
max_file_uploads = 200
post_max_size = 128M

note: you can do this by using ctrl w

Step three: restart php service:

Code:
service php-fpm restart
 
Hey ! so i want to get this working in the rtorrent file
echo 'method.set_key=event.download.finished,cross_seed,"execute={'`pwd`/rtorrent-cross-seed.sh',$d.name=}"' >> .rtorrent.rc
any ideas?
 
I fixed the demon, and now it works normally using rtorrent.rc

Code:
!/bin/sh
# PROVIDE: rtorrent
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="rtorrent"
rcvar="rtorrent_enable"

load_rc_config $name

# START USER CONFIGURATION

: ${rtorrent_enable="NO"}
: ${rtorrent_scgi_port=""}
: ${rtorrent_args=""}
: ${rtorrent_user="rtorrent"}
: ${rtorrent_directory="/home/rtorrent/downloads"}
: ${rtorrent_session="/home/rtorrent/sessions"}
: ${rtorrent_rc_config="/usr/local/etc/rtorrent.rc"}

command="/usr/local/bin/rtorrent"
screen="/usr/local/bin/screen"

rtorrent_args="-d ${rtorrent_directory} -s ${rtorrent_session} ${rtorrent_args}"
[ -n "${rtorrent_scgi_port}" ] && rtorrent_args="-O scgi_port='${rtorrent_scgi_port}' ${rtorrent_args}"
[ -n "${rtorrent_rc_config}" ] && rtorrent_args="-O import='${rtorrent_rc_config}' ${rtorrent_args}"

start_cmd="echo Starting ${name}.; su -m ${rtorrent_user} -c \"${screen} -A -m -d -S rtorrent ${command} ${rtorrent_args}\""

run_rc_command "$1"

Copying a sample configuration file using the 'cp' command.
Code:
cp /usr/local/share/examples/rtorrent/rtorrent.rc /usr/local/etc/rtorrent.rc
 
The main advantage is that it works, Transmission had a problem with operation, after some time it stopped working, it sent only several kilobytes. I haven't tested the rasterbar, I don't know if it has a GUI. qBittorrent was too disjointed. By the way, I also added vmtouch support, vmtouch needs to be installed and it works a little better.

Code:
#!/bin/sh
# PROVIDE: rtorrent
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="rtorrent"
rcvar="rtorrent_enable"

load_rc_config $name

: ${rtorrent_enable="NO"}
: ${rtorrent_scgi_port=""}
: ${rtorrent_args=""}
: ${rtorrent_user="rtorrent"}
: ${rtorrent_directory="/home/rtorrent/downloads"}
: ${rtorrent_session="/home/rtorrent/sessions"}
: ${rtorrent_rc_config="/usr/local/etc/rtorrent.rc"}

command="/usr/local/bin/rtorrent"
screen="/usr/local/bin/screen"
vmtouch_command="/usr/local/bin/vmtouch"
torrent_directory="/usr/local/www/lighttpd/ruTorrent/"

rtorrent_args="-d ${rtorrent_directory} -s ${rtorrent_session} ${rtorrent_args}"
[ -n "${rtorrent_scgi_port}" ] && rtorrent_args="-O scgi_port='${rtorrent_scgi_port}' ${rtorrent_args}"
[ -n "${rtorrent_rc_config}" ] && rtorrent_args="-O import='${rtorrent_rc_config}' ${rtorrent_args}"

start_cmd="echo Starting ${name}.; su -m ${rtorrent_user} -c \"${screen} -A -m -d -S rtorrent ${command} ${rtorrent_args}\"; ${vmtouch_command} -i '*.torrent' -m 512M ${torrent_directory}"

run_rc_command "$1"


Code:
root@router:~ # service rtorrent restart
/usr/local/etc/rc.d/rtorrent: DEBUG: checkyesno: rtorrent_enable is set to YES.
Stopping rtorrent.
/usr/local/etc/rc.d/rtorrent: DEBUG: run_rc_command: doit: su -m rtorrent -c 'sh -c "kill -TERM 6859"'
Waiting for PIDS: 6859.
/usr/local/etc/rc.d/rtorrent: DEBUG: checkyesno: rtorrent_enable is set to YES.
/usr/local/etc/rc.d/rtorrent: DEBUG: run_rc_command: doit:  echo Starting rtorrent.; su -m rtorrent -c "/usr/local/bin/screen -A -m -d -S rtorrent /usr/local/bin/rtorrent -O import='/usr/local/etc/rtorrent.rc' -O scgi_port='127.0.0.1:6000' -d /home/rtorrent/downloads -s /home/rtorrent/sessions "; /usr/local/bin/vmtouch -i '*.torrent' -m 512M /usr/local/www/lighttpd/ruTorrent/
Starting rtorrent.
           Files: 2650
     Directories: 163
  Resident Pages: 0/4050  0/15M  0%
         Elapsed: 0.028769 seconds
root@router:~ #
 
Back
Top