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. {% codeblock lang:bash %} 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 {% endcodeblock %}

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

{% codeblock lang:bash %} wget http://rtorrent.net/downloads/libtorrent-0.13.6.tar.gz tar zxvf libtorrent-0.13.6.tar.gz cd libtorrent-0.13.6 {% endcodeblock %}

Build & Install libTorrent

{% codeblock lang:bash %} ./autogen.sh export CXXFLAGS=“${CXXFLAGS} -fno-strict-aliasing” ./configure –prefix=/usr –disable-debug make sudo make install {% endcodeblock %}

Get rTorrent

{% codeblock lang:bash %} 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 {% endcodeblock %}

Build & Install rTorrent

{% codeblock lang:bash %} ./autogen.sh export CXXFLAGS=“${CXXFLAGS} -std=c++11” ./configure –prefix=/usr –enable-debug –with-xmlrpc-c make sudo make install {% endcodeblock %}

Download & Install rTorrent Configuration File

{% codeblock lang:bash %} 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 {% endcodeblock %}

Download & Install rtorrent.service File

{% codeblock lang:bash %} 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 {% endcodeblock %}

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: {% codeblock /etc/php/fpm.d/rutorrent.conf lang:ini %} [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 = / {% endcodeblock %}

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: {% codeblock /etc/nginx/nginx.conf lang:nginx %} upstream backendrutorrent { server unix:/run/php-fpm-rutorrent.sock; } upstream backendrtorrent { server unix:/home/rtorrent/.rtorrent.sock; } {% endcodeblock %}

In the server section add the following: {% codeblock /etc/nginx/nginx.conf lang:nginx %} 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; } {% endcodeblock %}

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. {% codeblock lang:bash %} 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/ {% endcodeblock %}

Find and replace the following lines in /usr/share/nginx/html/ruTorrent/conf/config.php. {% codeblock /usr/share/nginx/html/ruTorrent/conf/config.php lang:php %} $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’ ); {% endcodeblock %}

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:

{% codeblock lang:bash %} mkdir .rtorrent-session mkdir downloads systemctl –user enable rtorrent.service {% endcodeblock %}

And as a sudo user run the commands below: {% codeblock lang:bash %} sudo loginctl enable-linger rtorrent sudo systemctl enable php-fpm.service sudo systemctl enable nginx.service sudo shutdown -r now {% endcodeblock %}

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