# [HOWTO] btpd + btpd-webui

## dr4cul4

Hi all,

I just recently wanted to setup simple and easy to use torrent client on my server, which doesn't have X server. I couldn't find good client (I know mldonkey is nice, but I have issues with it and I just stopped trying) so I decided to try out btpd. After successful setup I downloaded web ui and I'm happy. If someone want to do it here it is how:

Do this as root.

```
emerge -v btpd
```

Next you need to correctly setup daemon (I think that original script is screwed up if you use nologin users) so do the following:

```
rm /etc/init.d/btpd

vi /etc/init.d/btpd
```

Or use other editor like nano and paste this:

```
#!/sbin/runscript

BTPDUSERHOME=`getent passwd ${BTPDUSER} | cut -d : -f 6`

BTPDHOME=${BTPDUSERHOME}/.btpd

BTPDSTARTUPLOG=${BTPDHOME}/startup.log

depend() {

        need net

}

checkconfig() {

        if [ -z ${BTPDUSER} ]; then

                eerror "Must edit /etc/conf.d/btpd first."

                return 1

        elif [ -z "`getent passwd ${BTPDUSER}`" ]; then

                eerror "Check /etc/conf.d/btpd's \${BTPDUSER}. '${BTPDUSER}' doesn't exist."

                return 1

        fi

}

start() {

        ebegin "Starting BitTorrent Protocol Daemon"

        checkconfig || return 1

        if pgrep -u ${BTPDUSER} btpd >/dev/null; then

                eerror "An instance of btpd is already running"

                return 1

        else

                env HOME="${BTPDHOME}" start-stop-daemon --start \

                -v --background \

                        --make-pidfile --pidfile /var/run/btpd.pid \

                        -c ${BTPDUSER} \

                        -x /usr/bin/btpd -- --no-daemon ${BTPDEXTRARGS} >${BTPDSTARTUPLOG}

                sleep 2

                if ! pgrep -u ${BTPDUSER} btpd > /dev/null; then

                eerror "BitTorrent Protocol Daemon couldn't be started ! Check logfile: ${BTPDSTARTUPLOG}"

                return 1

                fi

        fi

        eend $?

}

stop() {

        ebegin "Stopping BitTorrent Protocol Daemon"

        checkconfig || return 1

        env HOME="${BTPDUSERHOME}" btcli kill

        eend $?

}

restart() {

        svc_stop

        sleep 3

        svc_start

}
```

Next setup your configuration:

```
vi /etc/conf.d/btpd
```

Make it look like this (make sure you have a user created and set other values):

```
# owner of btpd process (must be existing)

BTPDUSER="p2p"

# Extra arguments for btpd

BTPDEXTRARGS="--bw-in 128 --bw-out 10 -p 6666"
```

Now we can run btpd by typing:

```
/etc/init.d/btpd start
```

To add to default run level type

```
rc-update add btpd default
```

Now to nicely use the client you have to setup an alias for it

edit file /etc/bash/bashrc:

```
vi /etc/bash/bashrc
```

And add at the bottom (you can use something else instead of bt, and of cource correct path to users home dir on which daemon is running):

```
alias bt='HOME="/home/p2p" btcli'
```

Now we will setup Web UI

To get one you must have svn installed or download manually some how. We assume that svn is installed:

```
cd

svn checkout http://btpd-webui.googlecode.com/svn/trunk/ btpd-webui-read-only

cd btpd-webui-read-only/

python setup.py install
```

In case of problems read requirements in file ~/btpd-webui-read-only/README

OK so we have almost working btpd-webui. Although it comes with btpd-webui service start stop script, it works only on current user. Means we run btpd on p2p user and btpd-webui would start as current user aka root. We need to create our own init script that would include that.

```
vi /etc/init.d/btpd-webui
```

Paste this:

```
#!/sbin/runscript

BTPDUSERHOME=`getent passwd ${BTPDUSER} | cut -d : -f 6`

BTPDUSERID=`getent passwd ${BTPDUSER} | cut -d : -f 3`

BTPDHOME=${BTPDUSERHOME}/.btpd

NAME=btpd-webui

BWHOMEDIR=${BTPDUSERHOME}/.btpd-webui

LOGFILE=$BWHOMEDIR/twistd.log

PIDFILE=$BWHOMEDIR/twistd.pid

TWISTD=`which twistd`

BTPDWEBUI=`which btpd-webui-server`

depend() {

        need net btpd

}

checkconfig() {

        # Ensure we have the twistd program

        if [ ! $TWISTD ]; then

                echo "Can't find twistd.  Do you have twisted installed?"

                return 1

        fi

        # Ensure we have btpd-webui-server

        if [ ! $BTPDWEBUI ]; then

                echo "Can't find btpd-webui-server. Is it installed and in your path?"

                exit 1

        fi

        # Make the btpd-webui home directory if needed.

        if [ ! -d $HOMEDIR ]; then

                mkdir --mode=770 $HOMEDIR

                chown ${BTPDUSER} $HOMEDIR

        fi

}

start() {

        ebegin "Starting BitTorrent Protocol Daemon Web UI"

        checkconfig || return 1

        if [ -f $PIDFILE ]; then

                echo "$NAME is already running."

        else

                echo -n "Starting $NAME via twistd..."

                env HOME="${BTPDUSERHOME}" $TWISTD --uid=${BTPDUSERID} --rundir=${BTPDUSERHOME} --logfile=$LOGFILE --pidfile=$PIDFILE --python=$BTPDWEBUI

                sleep 1

                test -f $PIDFILE

        fi

        eend test $?

}

stop() {

        ebegin "Stopping BitTorrent Protocol Daemon Web UI"

        checkconfig || return 1

        if [ ! -f $PIDFILE ]; then

                echo "$NAME is not running."

        else

                echo -n "Stopping $NAME via twistd..."

                kill `cat $PIDFILE`

                sleep 1

                test ! -f $PIDFILE

        fi

        eend $?

}

restart() {

        svc_stop

        sleep 3

        svc_start

}
```

You also need to configure this service:

```
vi /etc/conf.d/btpd-webui
```

We only need to setup user name:

```
# owner of btpd-webui process (must be existing)

BTPDUSER="p2p"
```

Now we need to test if that works:

```
/etc/init.d/btpd-webui start
```

In your browser type address: 

```
http://server.name:12321/
```

Login name and password btpd

To setup btpd-webui edit /home/p2p/.btpd-webui/config

To add btpd-webui to default run level type

```
rc-update add btpd-webui default
```

You can remove ~/btpd-webui-read-only directory.

Enjoy. Hope someone soon will make ebuild of btpd-webu and fix existing ebuild of btpd.

----------

## dr4cul4

You also could wand to add this line to /etc/init.d/btpt in stop() function

```
sleep 2

kill -9 `ps ax | grep '/usr/bin/btpd ' | cut -d ' ' -f 1`
```

----------

