# FreeBSD install rtorrent and ruTorrent on lighttpd full guide.



## StephanieJones (May 19, 2022)

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.

```
pkg install lighttpd
```

Then edit the configuration file.

```
ee /usr/local/etc/lighttpd/lighttpd.conf
```

Change the port to 8080 from 80

```
server.port = 8080
```

Disable ipv6.

```
server.use-ipv6 = "disable"
```

Change the directory of the website to the directory where ruTorrent will be.

```
server.document-root = "/usr/local/www/lighttpd/ruTorrent"
```

Bind socket, which is not required when ipv6 is disabled.

```
#$SERVER["socket"] == "0.0.0.0:80" { }
```
That's all on this file. Save change.

Adds an entry to /etc/rc.conf

```
sysrc lighttpd_enable="yes"
```

Find and install highest version of php.

```
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

```
ee /usr/local/etc/php-fpm.d/www.conf
```

Change the line:

```
listen = 127.0.0.1:9000
```

For the following entry

```
listen = /var/run/php-fpm.sock
```

Also change the following to what is shown below.
By uncommenting *;* and assigning permissions.

```
listen.owner = www
listen.group = www
listen.mode = 0660
```
Save change

Copy the sample and put it in a proper folder.

```
cp /usr/local/etc/php.ini-production /usr/local/etc/php.ini
```

Adds an entry to /etc/rc.conf

```
sysrc php_fpm_enable="yes"
```

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

```
ee /usr/local/etc/lighttpd/modules.conf
```

Add the following entries:

```
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 #)

```
conf_dir + "/conf.d/fastcgi.conf"
```
Save changes

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

```
ee /usr/local/etc/lighttpd/conf.d/fastcgi.conf
```

Adds the following entries.

```
server.modules += ( "mod_fastcgi" )
server.modules += ( "mod_scgi" )
```

In the same file add the following entries. 

```
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

```
sysrc lighttpd_enable=yes
```

Enable installed modules.

```
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

```
pkg install wget
```

Wget the RuTorrent.

```
wget https://github.com/Novik/ruTorrent/archive/refs/heads/master.zip
```

Unzip them. When ask pres A and enter.

```
unzip master.zip
```


```
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.

```
mv ruTorrent-master ruTorrent
```

Move ruTorrent to the previously defined root directory using the move command

```
mv ruTorrent /usr/local/www/lighttpd/ruTorrent
```

Create missing the directory for cache/history added .torrent file.

```
mkdir -p /usr/local/www/lighttpd/ruTorrent/php/share/torrents
```

In ruTorrent directory create info.php file.

```
ee /usr/local/www/lighttpd/ruTorrent/info.php
```

And paste the content below.

```
<?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.

```
chown -R www:www /usr/local/www/lighttpd/ruTorrent
```

Ad permission.

```
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.



Remove info.php from directory.

```
rm /usr/local/www/lighttpd/ruTorrent/info.php
```

Install the rtorrent program.

```
pkg install rtorrent
```

Install the screen program.

```
pkg install screen
```

Add a dummy user to run the torrent on

```
pw useradd rtorrent -s /sbin/nologin
```

Create working folders for the torrent

```
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.

```
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

```
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.

```
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.

```
chmod +x /usr/local/etc/rc.d/rtorrent
```

Starting deamon.

```
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

```
ee /usr/local/www/lighttpd/ruTorrent/conf/config.php
```

And change follows.
Define scgi_port and scgi_host to what is specified earlier.

```
$scgi_port = 6000;
    $scgi_host = "127.0.0.1";
```

In this block, there are given paths to programs that use ruTorrent plugin.

```
$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.

```
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:

```
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:


```
_cloudflare
_noty
_noty2
check_port
```

Check port queries the external server for our IP address.



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


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

```
ee /usr/local/etc/lighttpd/lighttpd.conf
```

Follow entry:

```
$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.

```
echo "admin:mypassword" >> /usr/local/www/lighttpd/.lighttpdpassword
```

Restart lighttpd server.

```
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:

```
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 #
```






						pkg.zip
					






					drive.google.com
				




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.


----------



## Ac1dburn (May 31, 2022)

This guide is absolutely amazing ! Thank you so much. I do have a follow up StephanieJones im having problems with saving files to new locations using plug-in data dir. Im getting a internal error 500 when trying to post it. It says https://xxxxxxxx/plugins/datadir/action.php Are you having the same issue?


----------



## StephanieJones (May 31, 2022)

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.

```
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:

```
$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:

```
$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:

```
[ -n "${rtorrent_max_downloads_global}" ] && rtorrent_args="-O ax_downloads_global='${rtorrent_max_downloads_global}' ${rtorrent_args}"
```

It should be:

```
[ -n "${rtorrent_max_downloads_global}" ] && rtorrent_args="-O max_downloads_global='${rtorrent_max_downloads_global}' ${rtorrent_args}"
```


----------



## stickz (Jun 2, 2022)

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.

```
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.

```
$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:*

```
pkg install vmtouch
```

*Run this command when initially starting rTorrent:*

```
vmtouch -i '*.torrent' -m 125K /usr/local/www/lighttpd/ruTorrent/
```


----------



## Ac1dburn (Jun 2, 2022)

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}"}


----------



## Ac1dburn (Aug 26, 2022)

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? 

```
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));
```


----------



## diizzy (Aug 26, 2022)

Worth mentioning is that qBittorrent(or -nox11) have a WebUI out of the box and all is packaged in ports tree


----------



## Ac1dburn (Aug 28, 2022)

I do know This diizzy but even with the issues i highly prefer rutorrent


----------



## Ac1dburn (Aug 29, 2022)

More additions: 

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

Step One: 
	
	



```
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: 


```
service php-fpm restart
```


----------

