Monday, September 19, 2011

Ampache Music Server

Maybe it's because iTunes has pissed me off one too many times, or maybe it's just for the sick, nerdy thrill, but I'm working on setting up my own Ampache music server.

Here's the specs:
  • Old Gateway all-in-one: Pentium III 1.2GHz, 20GB drive, 128MB RAM
  • Two 120GB external WD drives
  • Ubuntu 8.04.2 LTS
  • MySQL 5.0
  • Apache2
  • PHP5
  • Ampache 3.5.4 Stable
This post is just as much for my own documentation purposes as it is to share what I've learned with the net. Still, if you have any questions, feel free to comment.

Server

Let's proceed from a fresh install, shall we? For the record, this is from a vanilla command-line system install of Ubuntu 8.04 LTS -- no X, no GNOME, nothing other than the base system. Step 2: install OpenSSH, grab a beer, and head back downstairs.

Note: Apparently there's an ampache package in the repositories. Who knew? However, I prefer to keep my server and web apps separated. Just a personal preferrence--IT intuition I guess. Proceeding with the install of LAMP packages...

sudo apt-get install apache2 mysql-server php5
We can let apt handle all the dependencies, so that should cover it.

Storage

With a large collection, I need some way to "combine" the two 120GB external drives into one volume. To accomplish this, I used LVM. To install:

# install
sudo apt-get install lvm2
# test the install
sudo modprobe dm-mod
# auto-load the module on boot
sudo echo "dm-mod" >> /etc/modules

Then, using some tips from here, I gathered together the following commands:

# prepare the drives
sudo pvcreate /dev/sda1 /dev/sdb1
# create the volume group "vg" and add the two drives
sudo vgcreate vg /dev/sda1
sudo vgextend vg /dev/sdb1
# get the max size of the vg
sudo vgdisplay vg | grep "Total PE"
# use the "Total PE" amount above to size the logical volume
sudo lvcreate -l 57234 vg -n data
# format (ext3)
sudo mke2fs -j /dev/vg/data
# check the results
df -h
# add the mount command to /etc/fstab
echo "/dev/vg/data /media/data auto relatime,errors=remount-ro 0 1" >> /etc/fstab

Ampache

Grab the latest version of the Ampache source. At the time of this writing, version 3.5.4 was the latest stable build.I used these next commands to copy the download over to my music server:

scp ampache-3.5.4.tar.gz remoteuser@remotehost:/home/remoteuser
tar -xvf ampache-3.5.4.tar.gz

and then create a link between the extracted files and the Apache root:

sudo ln -s /home/remoteuser/ampache-3.5.4 /var/www/ampache

Now a quick trip over to http://remotehost/apache to kick-off Ampache's install wizard.

Tip: For album art support in Ampache, we need the PHP graphics library:

sudo apt-get php5-gd

Troubleshooting Tip: I had some inexperience with Apache which required me to restart the service a couple of times during debugging.

sudo /etc/init.d/apache2 restart

Even after restarting Apache, I was still having issues with Firefox offering to download my Ampache install instead of serving me the page. Clearing the browser cache fixed the issue.

Maintenance

I don't know why exactly, but out of all the steps in this series, this one took me the longest to figure out.

See, the music collection of any true aficionado will always be in a state of evolution. Songs get added, duplicates get deleted, tags get fixed, album art found, ratings assigned, etc. So the first step will be updating the music collection on the music server with any updates to the music collection on the file server. You might say I need some type of "remote syncing" utility. Hmm...

rsync --exclude="*.m4v" --exclude="*.mp4" --archive --verbose --compress --delete remoteuser@remotehost:/path/to/fileserver/collection/ /path/to/musicserver/collection

For those wishing to know more about rsync, RTFM. Suffice to say, this command will keep my collection on the music server an exact mirror copy of the source copy on the file server (except for my m4v/mp4 movies, which cause Ampache to barf on catalog updates).

To automate the process, we'll need to integrate some SSH and scripting. First, we generate a public/private key pair, and then we add the public key to the set of authorized keys on the file server.

ssh-keygen -t rsa -f my_key
scp my_key.pub remoteuser@remotehost:/home/remoteuser/.ssh

Then from the file server:

cat /home/remoteuser/.ssh/*.pub > /home/remoteuser/.ssh/authorized_keys

We can then use a cron job to execute the following script.

#!/bin/sh

RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/path/to/my/private/key
RUSER=remoteuser
RHOST=remotehost
RPATH=/path/to/remote/music/collection/
LPATH=/path/to/local/music/collection

$RSYNC --exclude="*.m4v" --exclude="*.mp4" --archive --verbose --compress --delete --rsh="$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH

Now that we have our files on the music server up-to-date, we need to refresh the Ampache catalog. Lucky for us, some enterprising soul wrapped up the catalog update logic in an easy-to-call script.


Note: To run a PHP script manually, install the PHP command-line utility:

sudo apt-get php5-cli

Then from the Ampache Wiki:

sudo -u www-data php /var/www/ampache/bin/catalog_update.inc [catalog_name] -a -c -v

The php command needs the path to catalog_update.inc. We can restrict the actions to "add", "clean", or "verify" with the flags at the end. The "sudo -u www-data" bit runs the whole shebang with the rights of the Apache user, thus mimicking what would be done via the UI.

Troubleshooting Tip: If the catalog update is failing, follow the instructions here to enable Ampache logging. It involves editing the config/ampache.cfg.php file. Don't forget to let the apache/www-data user have rights to the logging folder:

sudo mkdir /var/log/ampache
sudo chown www-data /var/log/ampache

Chances are the update routine came upon a file with bad tag, or a tag with some weird character in it, or some other such garbage. Could be Ampache/PHP ran out of memory; happened to me once with a 33MB Allman Brothers m4a file. In that case, adjust the "memory_limit" constant in config/ampache.cfg.php. Don't forget the "memory_limit" in /etc/php5/apache2/php.ini.

Troubleshooting Tip: If you have a good number of m4a files like I do, you'll find that Ampache doesn't much like reading the ID3/quicktime tags. This is a bummer if you want your track info correct in your catalog. One workaround is establishing a good file naming convention for your songs (/path/to/music/[artist]/[album]/[track#] - [track title].[file ext]), and then matching it to the correct "Filename Pattern" and "Folder Pattern" tokens in the the catalog setup. This way, Ampache doesn't even have to read the tags; it can just pick up the info from the file path & file name.

But then what about all that carefully maintained album art? Ampache to the rescue again: if there is a graphic file (jpg, png, etc.) in the album folder, Ampache will identify that as the album art. Your job, should you choose to accept it, is now to store your album art on the file system. There are tools a plenty to accomplish this. I used a dougscript to have iTunes do it for me.

Usage

If you've made it this far, you're done. Head over to your server and enjoy some PHP web-appy goodness.
For those with an iPhone, check out iAmpache from the App Store. It's a pretty basic app, not too many features; but it's free and it gets the job done. When iPhone OS 4.0 comes out, any iPhone models that can multi-task will be able to stream their raging tunes while Getting Things Done.

Update: Extreme bummer. Due to restrictions place on the app by Apple (probably as a proxy for AT&T), the iAmpache will not stream over 3G, only WiFi. Lame. There are plenty of other apps that stream audio over the cell connection, but I guess since neither Apple nor AT&T could control the bit rate/bandwidth usage, they put their foot down.

Update: For those with an Android phone, try Amdroid.

No comments:

Post a Comment