Install and configure rtorrent + rutorrent + nginx + php-fpm on Arch Linux

    Background


    To work with torrents, I used ktorrent for a long time. This client fully satisfied my needs for convenient download management until I noticed that on popular torrents the processor load reached 50% ( and even higher with uTP ), and memory consumption in the already bold KDE became unpleasant to catch the eye.

    It was decided to change KDE to xfce ( this is another story ), and choose a program for torrents with good functionality and convenient management. After trying transmission, deluge and rtorrent, I settled on the latter.

    About how to configure rtorrent + rutorrent + nginx + php-fpm, and it will be under a cat.

    Why is that?


    I want to answer right away why the indicated tools are chosen.

    Arch Linux . You can talk about this distribution for a long time, I like its organization and philosophy in general, and I can use it for myself with maximum efficiency. Who cares to read more about him, look here and here .

    nginx could . I am impressed with how this little thing does its job, saving memory, flexibly tuning and providing all the functionality I need.

    php-fpm . You can configure the number of worker threads, in conjunction with nginx gives excellent performance.

    rtorrent . Low resource consumption, highly customizable.

    rutorrent. Actively developing, has a nice interface.

    Installing prerequisite software



    We will assume that Arch Linux is already on the computer, and the user is familiar with its package system.

    To install the web part of the bundle, we run the command:

    sudo pacman -S nginx php-fpm

    rtorrent and librtorrent I recommend installing from AUR, there is a wonderful PKGBUILD called rtorrent-color, which makes the boring console interface more enjoyable ( if you use it ), and libtorrent-extended, which has additional patches. Therefore, we execute the command:

    yaourt rtorrent-color

    and

    yaourt libtorrent-extended

    To get rutorrent, you need to clone it from svn with the command: Files will appear in the rutorrent-read-only directory, then we will pick them up from there.

    svn checkout rutorrent.googlecode.com/svn/trunk rutorrent-read-only




    Customization



    In the /etc/php/php-fpm.conf file, you need to set the following parameters:

    • listen = 127.0.0.1:9000 so that php-fpm listens on the specified network socket;
    • pm = static so that the number of worker threads is constant;
    • pm.max_children = 2 to set the number of worker threads equal to the number of physical threads ( I have a dual-core processor, so 2 is set here ).


    The file /etc/nginx/conf/nginx.conf can be reduced to this view:

    worker_processes 2;
    error_log  /var/log/nginx/error.log;
    pid        /var/run/nginx.pid;
    events {
    	worker_connections  1024;
    	use epoll;
    }
    http {
    	include		/etc/nginx/conf/mime.types;
    	default_type	application/octet-stream;
    	upstream backend {
    		server 127.0.0.1:9000;
    	}
    	upstream backendrtorrent {
    		server unix:/home/pf/.rtorrent.sock;
    	}
    	sendfile		on;
    	keepalive_timeout	65;
    	include /etc/nginx/conf/sites-enabled/*;
    	include /etc/nginx/conf/conf.d/*;
    }


    Note that the backend subsection points to php-fpm, and backendrtorrent points to the rtorrent socket file (more on that later ).

    Create the directories / etc / nginx / conf / sites-enabled and / etc / nginx / conf / sites-available . In the second, create the rutorrent.eternity configuration file with the following contents and make a symbolic link to it in the first directory:

    server {
    	listen 80;
    	server_name localhost;
    	access_log	/srv/http/nginx/rutorrent.eternity/logs/access.log;
    	error_log	/srv/http/nginx/rutorrent.eternity/logs/errors.log;
    	location / {
    		root /srv/http/nginx/rutorrent.eternity/htdocs;
    		index index.php index.html index.htm;
    	}
    	location /RPC2 {
    		include /etc/nginx/conf/scgi_params;
    		scgi_pass backendrtorrent;
    	}
    	location ~ /\.ht {
    		deny all;
            }
    	location ~* \.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
    		root /srv/http/nginx/rutorrent.eternity/htdocs;
    		access_log off;
    		expires 30d;
    	}
    	location ~ .php$ {
    		fastcgi_split_path_info ^(.+\.php)(.*)$;
    		fastcgi_pass	backend;
    		fastcgi_index	index.php;
    		fastcgi_param	SCRIPT_FILENAME	/srv/http/nginx/rutorrent.eternity/htdocs$fastcgi_script_name;
    		include fastcgi_params;
    		fastcgi_param	QUERY_STRING	$query_string;
    		fastcgi_param	REQUEST_METHOD	$request_method;
    		fastcgi_param	CONTENT_TYPE	$content_type;
    		fastcgi_param	CONTENT_LENGTH	$content_length;
    		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;
    	}
    }


    Pay attention to the location of the error and access logs ( /srv/http/nginx/rutorrent.eternity/logs ), as well as the web root directory docs ( /srv/http/nginx/rutorrent.eternity/htdocs) These directories must exist. Also pay attention to the location / RPC2 block , it is required for rutorrent. I do not provide a description of the remaining parameters here; detailed documentation on the Internet is sufficient.

    In the file / etc / hosts make this line:

    127.0.0.1 localhost.localdomain localhost eternity rutorrent.eternity

    eternity - the name of my system, you it may be different. In this case, it must be changed everywhere in the configuration files.

    You can run nginx and php-fpm: You can now test the bundle web part by throwing some simple php file into the root directory of web documents. When you navigate to rutorrent.eternity in a web browser, it should display correctly.

    sudo /etc/rc.d/nginx start
    sudo /etc/rc.d/php-fpm start




    Now you need to install rutorrent. Transfer the contents of the rutorrent-read-only / rtorrent directory to the root directory of web documents (I recall this is the directory /srv/http/nginx/rutorrent.eternity/htdocs ). Do the same with the rutorrent-read-only / plugins directory, copy it over the existing plugins directory in the rutorrent file tree. Trash of the form .svn can be removed.

    Open the file /srv/http/nginx/rutorrent.eternity/htdocs/conf/config.php and replace only two lines there: The socket file must match the above. The web interface is ready, now you need to configure rtorrent itself.

    $scgi_port = 0;
    $scgi_host = "unix:///home/pf/.rtorrent.sock";






    Create a .rtorrent.rc file in the home directory with the following contents: The line system.file_allocate.set = yes makes sense if libtorrent is compiled with the --with-posix-fallocate option, which on modern file systems makes it possible to instantly allocate the necessary space for the torrent. Options like * g_color apply only to rtorrent-color. Configure the directories, ports and speed as you wish. The final touch is a script to run rtorrent. Place the following contents in the /etc/rc.d/rtorrentd file : Naturally, screen must be installed. Adjust the rtorrent_user and rtorrent_socket variables to fit your environment. If rtorrent startup is needed, put it in a file

    scgi_local = /home/pf/.rtorrent.sock
    max_memory_usage = 268435456
    system.file_allocate.set = yes
    done_fg_color = 2
    done_bg_color = 0
    active_fg_color = 4
    active_bg_color = 0
    download_rate = 250
    upload_rate = 250
    directory = /home/pf/work/downloads/torrents
    session = /home/pf/work/downloads/torrents/.session
    port_range = 29292-29292
    check_hash = no
    use_udp_trackers = yes
    encryption = allow_incoming,try_outgoing,enable_retry,prefer_plaintext
    dht = auto
    dht_port = 6881
    peer_exchange = yes






    #!/usr/bin/env bash
    . /etc/rc.conf
    . /etc/rc.d/functions
    rtorrent_user="pf"
    rtorrent_socket="/home/pf/.rtorrent.sock"
    case "$1" in
    	start)
    		stat_busy "Starting rtorrent"
    		if [ -S $rtorrent_socket ]; then
    			rm $rtorrent_socket
    		fi
    		su $rtorrent_user -c 'LANG=uk_UA.UTF-8 screen -d -m -S rtorrent rtorrent' &> /dev/null
    		if [ $? -gt 0 ]; then
    			stat_fail
    		else
    			while [ ! -S $rtorrent_socket ]
    			do
    				printf "%10s \r" waiting
    			done
    			chmod 666 $rtorrent_socket
    			add_daemon rtorrent
    			stat_done
    		fi
    	;;
    	stop)
    		stat_busy "Stopping rtorrent"
    		killall -w -s 2 /usr/bin/rtorrent &> /dev/null
    		if [ -S $rtorrent_socket ]; then
    			rm $rtorrent_socket
    		fi
    		if [ $? -gt 0 ]; then
    			stat_fail
    		else
    			rm_daemon rtorrent
    			stat_done
    		fi
    	;;
    	restart)
    		$0 stop
    		sleep 1
    		$0 start
    	;;
    	*)
    		echo "usage: $0 {start|stop|restart}"
    esac
    exit 0


    /etc/rc.conf to the DAEMONS array .

    All. Launch rtorrent with a team,

    sudo /etc/rc.d/rtorrentd start

    go to the site rutorrent.eternity in a browser and enjoy.

    Also popular now: