Creating Ubuntu Torrent and Xbox 360 Media Server
Wow, it’s been awhile since i’ve posted something, but i think that just means that Ubuntu 9.10 has been really stable. It honestly has. I’m running it on multiple computers and compatibility has been a non issue. Now crazy tricks to get things to work properly. However this past weekend i went to work on a new Ubuntu project.
So recently i came upon an old Dell Optiplex GX240. For those who don’t know, this computer is fairly elderly with not a whole lot of useful tasks it can be utilized for any more. My original intention when i purchased the thing for $25 was to use it as a companion to my 42″ LCD since it had such a small case. I already have a HD PVR, so i wasn’t going to use it for that. But simply to browse the web, play flash movies, and a bit of nintendo emulation in a tight small case that doesn’t look like a total eye soar in my tv shelf. Unfortunately, this P4 1.5Ghz with 512MB RAM couldn’t handle it due to the pitiful 16MB AGP video card. And since it only takes low profile AGP cards… i wasn’t about to try and hunt one down.
So after blowing $25 and wanting to salvage my investment my idea changed to create a torrent server that i could stream movies to my Xbox without using the terribly unreliable Windows Media Center. After a good portion of a saturday and a lot of trial and error and swearing, i got myself an Ubuntu torrent and xbox 360 media server using TorrentFlux and uShare.
So here is the quick and easy instructions on how to create a web based torrent downloading client on an Ubuntu machine and stream your torrent downloads directly to your Xbox using Apache, PHP, MySQL, TorrentFlux and uShare.
First off, download the latest version of TorrentFlux which is basically your Bit Torrent download client that will be run on your Ubuntu server. You can find it at www.torrentflux.com
After you download TorrentFlux, take your freshly installed Ubuntu 9.10 machine (or an Ubuntu install you haven’t messed up too badly yet) and install Apache 2.
$ sudo aptitude install apache2
Install PHP 5
$ sudo aptitude install php5
Install MySQL
$ sudo aptitude install mysql-server
$ sudo aptitude install php5-mysql
Restart the apache server
$ sudo /etc/init.d/apache2 restart
LAMP stack is now installed, now move onto installing TorrentFlux (i used 2.4, but whatever is newest i’m sure will work fine). I chose to install in the root www folder of apache, but feel free to put it where you want. I untarred the torrentflux file on my desktop then moved the html folder of TorrentFlux into the root www folder of apache.
$ tar -xvzf torrentflux_2.4.tar.gz
$ sudo chown (user):(user) /var/www/
$ cd /home/user/Desktop/torrentflux_2.4/html/
$ cp -r * /var/www/
Now that we have TorrentFlux in the proper folder, it’s time to setup the database. Create a database called torrentflux.
$ mysqladmin -u root -p create torrentflux
Now run the sql file for torrentflux to get the database in the initial state to run TorrentFlux
$ cd /home/(user)/Desktop/torrentflux_2.4/sql
$ mysql -u root -p torrentflux < mysql_torrentflux.sql
Now you got to modify the TorrentFlux config file and set the database password you used when you installed MySQL.
$sudo gedit /var/www/config.php
modify these lines as you see fit.
$cfg["db_user"] = “root”; // username for your MySQL database
$cfg["db_pass"] = “”; // password for database
Now to set the file permissions on your torrent download folder.
sudo chmod -R 777 /var/www/downloads/
Now that TorrentFlux is now setup, go to http://localhost/index.php and make sure you get a login screen. If you did, that means you followed the steps correctly. Your first login is actually setting your admin username and password. So use this screen now to set that.
So now that we got that complete, we only have to setup the uPnP streaming server to stream the files to our xbox. To install ushare, do the following.
$sudo apt-get install ushare
Once that is installed, lots stop the ushare server and set the configuration.
$/etc/init.d/ushare stop
$sudo gedit /etc/ushare.conf
For my installation, this was probably the biggest pain of it all to get it to stream to the xbox correctly. Problem one, on default, despite what your settings may say, it doesn’t load into ushare using Xbox mode, and sometimes the port changes after you stop and start the server (we will remedy this later in the post when modifying the rc.local file). Not sure if i was doing something incorrectly or just bad luck, but this is the configuration that works 100% for me. The following are lines you should have set in the ushare.conf file. Although feel free to change the ushare_name as that’s the name it shows up as on your xbox. Also, if your downloads folder is a different location than mine, change that.
USHARE_NAME=Torrent
USHARE_PORT=49200
USHARE_DIR=/var/www/downloads
ENABLE_WEB=yes
ENABLE_TELNET=no
ENABLE_XBOX=yes
Next you are going to want to configure the mime types for video streaming.
$ sudo gedit /usr/share/mime/packages/freedesktop.org.xml
find:
type=”video/x-msvideo”
change to:
type=”video/x-ms-wmv”
One of the problems i encountered and mentioned above is that by default, it doesn’t want to take the ENABLE_XBOX settings, so on bootup, your xbox can’t see your torrent server. So to change this, i modified the rc.local file (file which runs whenever you boot linux) to run some root commands to shut down the server, and start it back up on a new port forcing xbox enabled.
$ sudo gedit /etc/rc.local
at the end of the file right before the line “exit 0″, write above it
/etc/init.d/ushare stop
ushare -p 49201 -x
The last couple hurdles i had to overcome was refreshing the share list and updating file permissions. One of the issues with uPnP streaming is that it indexes the list of files you are streaming. This means that even though you download new files, your xbox isn’t going to see them. Luckily ushare builds in a page where you can update it, so i added the link right inside the TorrentFlux interface for easy use. Only thing is, when you are watching a video on your xbox, it’s not accessible, so don’t refresh your list while you’re watching. The other issue i found was TorrentFlux gives it’s own owner to files it downloads. If you share that folder on your network and want to move or delete files, it likely won’t let you, so i just made a script that updated the permissions for every file in the downloads folder. I’m sure there is a better way to do this, but at this point, i was kind of lazy and didn’t want to spend a lot of time figuring this out, and i figured this way was pretty easy by adding the link into the TorrentFlux interface as well.
$ sudo gedit /var/www/index.php
at the very end of the file. but right above the body and html tags, insert this:
<p><center><a href=”http://<?php echo $_SERVER["SERVER_ADDR"]; ?>:49201/web/ushare.html” target=”_blank”>Refresh Share List</a><br><a href=”permissions.php” target=”_blank”>Refresh File Permissions</a></center>
now to create the script that will update the permissions.
$ sudo gedit /var/www/permissions.php
insert the following code.
<?php
echo “Fixing File Permissions…<p>”;
echo shell_exec(“chmod -R 777 /var/www/downloads/”);
echo “<p>Done”;
?>
And just like that you’re done. You now have a torrent client on a remote server you can access from anywhere and you are able to stream everything you download with ease to your xbox. All you gotta do on your xbox is select “My Xbox” and then “Video Library” and select the ushare name you set to your server. Have fun!
$ sudo chown user:user /var/www/torrentflux/
