Raspberry Pi 2 + rTorrent/ruTorrent + NGINX/PHP-FPM = Awesome

Every time I go to rebuild a home server of any type I always forget which guides I used in the past and what steps I had to take to get any software running. Now most of what I need is easy to setup but some things take a few more steps. One guide I am always looking for is a mostly from scratch install of rTorrent with ruTorrent on NGINX with PHP-FPM. Since I never seem to find one that meets my specific case I decided it was time for me to write the guide I needed.

In this guide I will be building on top of a base install of ArchLinux ARM on a Raspberry Pi 2. If you have any questions about the steps below, please feel free to comment!

Preparing the Environment

I always feel the best place to start is to go ahead and install all the applications and utilities we will need and create a space to build anything not already provided. We will also go ahead and setup any users and groups we need for running the application.

1
2
3
4
5
6
7
8
sudo pacman -S base-devel xmlrpc-c libsigc++ openssl cppunit screen unzip mediainfo ffmpeg unrar nginx php-fpm
mkdir ~/build
cd ~/build
sudo groupadd rtorrent-socket
sudo useradd -G rtorrent-socket rutorrent
sudo useradd -m -G rtorrent-socket rtorrent
sudo usermod -G rtorrent-socket http
sudo passwd rtorrent

Now we will build and install rTorrent with the xmlprc extension which allows ruTorrent to control rTorrent. The main application consists of two parts: libTorrent and rTorrent. Both of these can be built, installed, and configured with the next few sections.

Get libTorrent

1
2
3
wget http://rtorrent.net/downloads/libtorrent-0.13.6.tar.gz
tar zxvf libtorrent-0.13.6.tar.gz
cd libtorrent-0.13.6

Build & Install libTorrent

1
2
3
4
5
./autogen.sh
export CXXFLAGS="${CXXFLAGS} -fno-strict-aliasing"
./configure --prefix=/usr --disable-debug
make
sudo make install

Get rTorrent

1
2
3
4
cd ~/build
wget http://rtorrent.net/downloads/rtorrent-0.9.6.tar.gz
tar zxvf rtorrent-0.9.6.tar.gz
cd rtorrent-0.9.6

Build & Install rTorrent

1
2
3
4
5
./autogen.sh
export CXXFLAGS="${CXXFLAGS} -std=c++11"
./configure --prefix=/usr --enable-debug --with-xmlrpc-c
make
sudo make install

Download & Install rTorrent Configuration File

1
2
wget https://winston.milli.ng/blog/Raspberry-Pi-2-rTorrent-ruTorrent-NGINX-PHP-FPM-Awesome/rtorrent.rc.example
sudo cp rtorrent.rc.example /home/rtorrent/.rtorrent.rc

Download & Install rtorrent.service File

1
2
wget https://winston.milli.ng/blog/Raspberry-Pi-2-rTorrent-ruTorrent-NGINX-PHP-FPM-Awesome/rtorrent.service
sudo cp rtorrent.service /etc/systemd/user/rtorrent.service

Now that we have an install of rTorrent ready and configured let’s go ahead and setup the webserver and its configuration files.

Update the PHP Configuration Files

Modify /etc/php/php.ini and update the following two lines:

  • Add /usr/share/nginx/ to the list in open_basedir open_basedir = /srv/http/:/usr/share/nginx/:/home/
  • uncomment the following line ;cgi.fix_pathinfo=1

Modify /etc/php/php-fpm.conf and do the following:

  • Uncomment ;include=/etc/php/fpm.d/*.conf. (Remove the ;)

Create an ruTorrent Configuration for PHP-FPM

Using your favorite text editor create rutorrent.conf in the /etc/php/fpm.d/ directory and add the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
[rutorrent]
user = rutorrent
group = rutorrent
listen = /run/php-fpm-rutorrent.sock
listen.owner = rutorrent
listen.group = http
listen.mode = 0660
pm = static
pm.max_children =2
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /

Update the NGINX Configuration

Next we need to modify the NGINX configuration file. In this example I am modifying the main nginx.conf file, though you can always create site specific configurations. Open /etc/nginx/nginx.conf with your favorite text editor for the next few tasks.

In the http section add the following:

1
2
3
4
5
6
upstream backendrutorrent {
        server unix:/run/php-fpm-rutorrent.sock;
}
upstream backendrtorrent {
        server unix:/home/rtorrent/.rtorrent.sock;
}

In the server section add the following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
location /rutorrent {
  access_log /var/log/nginx/rutorrent.access.log;
  error_log /var/log/nginx/rutorrent.error.log;
  location ~ .php$ {
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_pass    backendrutorrent;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root/rutorrent$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout         60;
    fastcgi_send_timeout            180;
    fastcgi_read_timeout            180;
    fastcgi_buffer_size             128k;
    fastcgi_buffers                 4       256k;
    fastcgi_busy_buffers_size       256k;
    fastcgi_temp_file_write_size    256k;
  }
}

location /RPC2 {
  access_log /var/log/nginx/rutorrent.rpc2.access.log;
  error_log /var/log/nginx/rutorrent.rpc2.error.log;
  include /etc/nginx/scgi_params;
  scgi_pass backendrtorrent;
}

Install ruTorrent

Now on to installing the ruTorrent web UI for rTorrent. I will be using the default web accessible directory for NGINX which was specified in the /etc/nginx/nginx.conf file.

1
2
3
4
5
6
7
8
wget http://dl.bintray.com/novik65/generic/ruTorrent-3.7.zip
sudo unzip ruTorrent-3.7.zip -d /tmp/ruTorrent/
sudo mkdir /usr/share/nginx/html/ruTorrent/
sudo mv /tmp/ruTorrent/ruTorrent-master/* /usr/share/nginx/html/ruTorrent/
sudo chown -R http:rtorrent-socket /usr/share/nginx/html/ruTorrent/
sudo chmod -R 775 /usr/share/nginx/html/ruTorrent/
sudo mkdir /usr/share/nginx/tmp/
sudo chmod 777 /usr/share/nginx/tmp/

Find and replace the following lines in /usr/share/nginx/html/ruTorrent/conf/config.php.

1
2
3
4
5
6
7
8
$scgi_port = 0;
$scgi_host = "unix:///home/rtorrent/.rtorrent.sock";
$log_file = "/usr/share/nginx/tmp/errors.log";
$tempDirectory = "/usr/share/nginx/tmp/";
$pathToExternals = array(
  "curl" => '/usr/bin/curl',
  "stat" => '/usr/bin/stat'
);

Starting Everything on Boot

Now it is time to get everything setup to run on boot. We will have some steps that need to be done as the rTorrent user and a few as a sudo user. First login as the rtorrent user for the following commands:

1
2
3
mkdir .rtorrent-session
mkdir downloads
systemctl --user enable rtorrent.service

And as a sudo user run the commands below:

1
2
3
4
sudo loginctl enable-linger rtorrent
sudo systemctl enable php-fpm.service
sudo systemctl enable nginx.service
sudo shutdown -r now

After that the Raspberry Pi should reboot and be ready to go. Try navigating to the internal IP address given to your Raspberry Pi with the ruTorrent subdirectory (e.g. http://192.168.1.100/ruTorrent/). If you have any trouble with anything in the guide above, please let me know in the comments below.

Share this post if you like it and feel free to hit me up with any comments at Winston@Milli.ng.
Disclaimer: Any and all opinions presented here are my own and not representative of my employer(s); past, present, and future.