Ubuntu上装个seedbox玩

搞了个VPS,如果只是用来当VPN用的话有点浪费,于是想了想决定在上面搞一个seedbox下美剧用。。。然后发现网络上现成的教程多多少少有些问题,摸索了半天终于成功,决定记录下来,以备不时之需~~

VPS的系统用的是Ubuntu 12.04.3 LTS,seedbox是rtorrent和rutorrent的结合。

VPS拿到手之后的第一件事自然是更新。。。

1
apt-get update && apt-get upgrade -y

新建一个帐号运行rtorrent,就叫做rtorrent好了,否则用root来运行的话,总觉得略恐怖啊。

1
adduser rtorrent

这就是为什么我热爱Ubuntu。。。装软件太方便了。。。

1
apt-get install -y apache2 apache2-utils autoconf build-essential ca-certificates comerr-dev libapache2-mod-php5 libcloog-ppl-dev libcppunit-dev libcurl3 libcurl4-openssl-dev libncurses5-dev ncurses-base ncurses-term libterm-readline-gnu-perl libsigc++-2.0-dev libssl-dev libtool libxml2-dev ntp openssl patch libperl-dev php5 php5-cli php5-dev php5-fpm php5-curl php5-geoip php5-mcrypt php5-xmlrpc pkg-config python-scgi dtach ssl-cert subversion unrar zlib1g-dev pkg-config unzip htop irssi curl cfv mediainfo rtorrent

如果提示找不到unrar的话需要修改/etc/apt/sources.list,加入multiverse,然后

1
apt-get update

如果需要截图,那还需要ffmpeg。首先

1
apt-get install -y software-properties-common python-software-properties

然后

1
add-apt-repository ppa:jon-severinsson/ffmpeg

然后apt-get update,最后

1
apt-get install -y ffmpeg

调教Apache2。说明一下,因为TCP443端口被我拿来做SSL VPN了,所以https的端口被我改成了8443,如果希望继续使用默认的443端口的同学们自己相应修改。

启用必要的模块

1
2
3
a2enmod ssl
a2enmod auth_digest
a2ensite default-ssl

自签名一个证书

1
2
openssl req -new -x509 -days 365 -nodes -newkey rsa:2048 -out /etc/apache2/apache.pem -keyout /etc/apache2/apache.pem
chmod 600 /etc/apache2/apache.pem

加个密码(假设用户名是webui)输入下面一条命令之后会让你输入2次密码

1
htdigest -c /etc/apache2/htpasswd rutorrent webui

修改/etc/apache2/apache2.conf,Timeout改为30,增加一句ServerTokens Prod

修改/etc/apache2/sites-available/default为下面的内容,记得替换servername or IP部分:

<VirtualHost *:80>
	
	ServerAdmin webmaster@localhost
	
	DocumentRoot /var/www/
		<Directory />
			Options FollowSymLinks
			AllowOverride All
		</Directory>
		<Directory /var/www/>
			Options -Indexes FollowSymLinks MultiViews
			AllowOverride None
			Order allow,deny
			allow from all
		</Directory>
        
	ErrorLog /var/log/apache2/error.log
	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn
    
    CustomLog /var/log/apache2/access.log combined
    
    <Location /rutorrent>
    	AuthType Digest
    	AuthName "rutorrent"
    	AuthDigestDomain /var/www/rutorrent/ http://<servername or IP>/rutorrent
    	AuthDigestProvider file
    	AuthUserFile /etc/apache2/htpasswd
    	Require valid-user
    	SetEnv R_ENV "/var/www/rutorrent"
    </Location>

</VirtualHost>

修改/etc/apache2/sites-available/default-ssl为下面的内容,记得替换servername or IP部分:

<VirtualHost *:8443>
        ServerAdmin webmaster@localhost

        SSLEngine on
        SSLCertificateFile /etc/apache2/apache.pem

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/>
                Options -Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined
    <Location /rutorrent>
        AuthType Digest
        AuthName "rutorrent"
        AuthDigestDomain /var/www/rutorrent/ http://<servername or IP>/rutorrent
        AuthDigestProvider file
        AuthUserFile /etc/apache2/htpasswd
        Require valid-user
        SetEnv R_ENV "/var/www/rutorrent"
     </Location>
</VirtualHost>

修改/etc/apache2/ports.conf,主要是把443改成8443,以及在IfModule mod_ssl.c下加一句NameVirtualHost *:8443

最后就是

1
service apache2 restart

新建一个rtorrent的配置文件,可以从http://libtorrent.rakshasa.no/export/1303/trunk/rtorrent/doc/rtorrent.rc下载以后修改,记得下载之后文件名改为.rtorrent.rc,放在/home/rtorrent/下面。修改的要点是增加一行scgi_port = localhost:5000,其他按需修改即可。

新建一些目录,修正一些权限的问题:

1
2
3
4
5
chown rtorrent:rtorrent /home/rtorrent/.rtorrent.rc
mkdir /home/rtorrent/.session
chown rtorrent:rtorrent /home/rtorrent/.session
chmod 644 /home/rtorrent/.rtorrent.rc
chmod 755 /home/rtorrent/.session 

下载rutorrent

1
2
3
4
5
6
7
8
9
cd /tmp
svn checkout http://rutorrent.googlecode.com/svn/trunk/rutorrent
svn checkout http://rutorrent.googlecode.com/svn/trunk/plugins
rm -r rutorrent/plugins
mv plugins rutorrent/
mv rutorrent /var/www/
cd /var/www
sudo chown -R www-data:www-data rutorrent
sudo chmod -R 755 rutorrent

按需修改conf/config.php和conf/plugins.ini,我觉得默认的足够好了,没事不用动它们。

新建/etc/init.d/rtorrent,输入下面内容。。。吐槽一下其实这个才是难点,找了好多脚本都不能用。。。终于找到一个差不多能用的修改了一下。。。

  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
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/sh 
############# 
###### 
############# 
# This script depends on screen. 
# For the stop function to work, you must set an 
# explicit session directory using ABSOLUTE paths (no, ~ is not absolute) in your rtorrent.rc. 
# If you typically just start rtorrent with just "rtorrent" on the 
# command line, all you need to change is the "user" option. 
# Attach to the screen session as your user with 
# "screen -dr rtorrent". Change "rtorrent" with srnname option. 
# Licensed under the GPLv2 by lostnihilist: lostnihilist _at_ gmail _dot_ com 
############## 
###### 
############## 

####################### 
##Start Configuration## 
####################### 
# You can specify your configuration in a different file 
# (so that it is saved with upgrades, saved in your home directory, 
# or whateve reason you want to) 
# by commenting out/deleting the configuration lines and placing them 
# in a text file (say /home/user/.rtorrent.init.conf) exactly as you would 
# have written them here (you can leave the comments if you desire 
# and then uncommenting the following line correcting the path/filename 
# for the one you used. note the space after the ".". 
# . /etc/rtorrent.init.conf 

#Do not put a space on either side of the equal signs e.g. 
# user = user 
# will not work 
# system user to run as 
user="rtorrent" 
# the system group to run as, not implemented, see d_start for beginning implementation 
# group=`id -ng "$user"` 

# the full path to the filename where you store your rtorrent configuration 
config="`su -c 'echo $HOME' $user`/.rtorrent.rc" 

# set of options to run with 
options="" 

# default directory for screen, needs to be an absolute path 
base="`su -c 'echo $HOME' $user`" 

# name of screen session 
srnname="rtorrent" 

# file to log to (makes for easier debugging if something goes wrong) 
logfile="/home/$user/rtorrent-init.log" 
####################### 
###END CONFIGURATION### 
####################### 
PATH=/usr/bin:/usr/local/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin 
DESC="rtorrent" 
NAME=rtorrent 
DAEMON=$NAME 
SCRIPTNAME=/etc/init.d/$NAME 

checkcnfg() { 
exists=0 
for i in `echo "$PATH" | tr ':' '\n'` ; do 
if [ -f $i/$NAME ] ; then 
exists=1 
break 
fi 
done 
if [ $exists -eq 0 ] ; then 
echo "cannot find rtorrent binary in PATH $PATH" | tee -a "$logfile" >&2 
exit 3 
fi 
if ! [ -r "${config}" ] ; then 
echo "cannot find readable config ${config}. check that it is there and permissions are appropriate" | tee -a "$logfile" >&2 
exit 3 
fi 
session=`getsession "$config"` 
if ! [ -d "${session}" ] ; then 
echo "cannot find readable session directory ${session} from config ${config}. check permissions" | tee -a "$logfile" >&2 
exit 3 
fi 
} 

d_start() { 
[ -d "${base}" ] && cd "${base}" 
stty stop undef && stty start undef 
su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "screen -dm -S ${srnname} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 
# this works for the screen command, but starting rtorrent below adopts screen session gid 
# even if it is not the screen session we started (e.g. running under an undesirable gid 
#su -c "screen -ls | grep -sq "\.${srnname}[[:space:]]" " ${user} || su -c "sg \"$group\" -c \"screen -fn -dm -S ${srnname} 2>&1 1>/dev/null\"" ${user} | tee -a "$logfile" >&2 
su -c "screen -S "${srnname}" -X screen rtorrent ${options} 2>&1 1>/dev/null" ${user} | tee -a "$logfile" >&2 
} 

d_stop() { 
session=`getsession "$config"` 
if ! [ -s ${session}/rtorrent.lock ] ; then 
return 
fi 
pid=`cat ${session}/rtorrent.lock | awk -F: '{print($2)}' | sed "s/[^0-9]//g"` 
if ps -A | grep -sq ${pid}.*rtorrent ; then # make sure the pid doesn't belong to another process 
kill -s INT ${pid} 
fi 
} 

getsession() { 
session=`cat "$1" | grep "^[[:space:]]*session[[:space:]]*=" | sed "s/^[[:space:]]*session[[:space:]]*=[[:space:]]*//" ` 
echo $session 
} 

checkcnfg 

case "$1" in 
start) 
echo -n "Starting $DESC: $NAME" 
d_start 
echo "." 
;; 
stop) 
echo -n "Stopping $DESC: $NAME" 
d_stop 
echo "." 
;; 
restart|force-reload) 
echo -n "Restarting $DESC: $NAME" 
d_stop 
sleep 1 
d_start 
echo "." 
;; 
*) 
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2 
exit 1 
;; 
esac 

exit 0

这样就可以用service rtorrent start/stop/restart等来控制了。开机自启的话运行update-rc.d rtorrent defaults即可。这样就大功告成啦!打开浏览器输入http://xxx/rutorrent应该就好了。

参考资料:

http://forums.rutorrent.org/index.php?topic=256.0

http://ubuntuforums.org/showthread.php?t=1464021

updatedupdated2013-12-252013-12-25