Post Thu Sep 10, 2015 3:43 pm

Install to Ubuntu 14.04 and 15.04 my HowTo

Hi Everyone. I'm Sparky and this is my first post here. I just want to share my complete, from beginning to end install procedure for getting Serviio running on Ubuntu. I wrote an entire blog at sparkyhenderson.wordpress.com , but this is the heart of my install procedure.

I use the devel libraries from the Ubuntu repos and build ffmpeg from source. I understand that Ubuntu has put ffmpeg back into the repos for their latest release, but I still think building it is a better idea.

My format here is that I write "Notes" that explain why I did it the way I did it and I write "Steps" to say what I did.

I hope this is useful to you.

********************************************************************************
Install Oracle Java
********************************************************************************
Notes:
If you already have Java 8 installed then you can get right to work building ffmpeg.

If you don't have apt installed I recommend you install and use aptitude. The options are just a little different from apt and apt-get, but you can accomplish all the same tasks. (I like aptitude for its safe-upgrade option.)

I'm using the server JRE from Oracle. It only works on 64 bit OSes and it does not install any Firefox / Iceweasel plug-ins. Use the workstation JRE if you can't work within these constraints.

I am using the /etc/alternatives symlink method make Java accessible by using the existing path.

The file and directory permissions are set appropriately from the tar file. If you feel the need to tweak them then in general you want the rw for user and ro for group. You want all the directorires to have the x flag set for all.

You will have to set ownership for the files. Don't worry! The instructions are all here.

It is possible to have multiple Java runtime engines on your system, but it will require a slightly different set of instructions to get Serviio running. The basics of that type of setup are 1 - do not remove the other Java runtimes, 2 - do not set any of the symlinks in these instructions, and 3 - either use an environment variable that works with serviio.sh or edit your serviio.sh . This is a bit outside the scope of these instructions, but I'll be happy to write up explicit instructions if you ask.


Steps:
First let's determine if any sort of Java is installed.
  Code:
java -version

- no double dash, only a single dash, I always forget

Find and remove or disable the existing Java. Here are a couple ways to find them. Cut and paste:
  Code:
apt list --installed | grep java
apt list --installed | grep jdk

- there are lots of other ways to do this too

Either remove with apt, remove from path, or un-symlink them. I chose to remove my OpenJDK completely. This worked for me:
  Code:
apt-get remove openjdk-7-jre openjdk-7-jre-headless java-common


Search for stray executable or symlinks of "java" and either get rid of them or point them to the new install a bit later. Cut and paste:
  Code:
find / -type f -name java -executable -exec ls -alF {} \;

- find executable files named java and display their locations
- Note - The space between the curly braces and the backslash is necessary.

  Code:
find / -type l -name java -executable -exec ls -alF {} \;

- If you find symlinks you should double check to what they are pointing. Check the executables you found.
- Note - The space between the curly braces and the backslash is necessary.

Double check your /etc/alternatives directory for a symlink to Java too just to be sure.

Download the appropriate xxxxxx-jre-8uxx-linux for your system (mine was server-jre-8u60-linux-x64.tar.gz). I go to Oracle.com -> downloads -> Java for developers (link on the left) then choose the server JRE for my environment.

Change directory into /usr/lib . Cut and paste:
  Code:
cd /usr/lib


Untar your Java 8 download into /usr/lib . Something like this:
  Code:
tar xf /some/path/to/the-jre/server-jre-8u60-linux-x64.tar.gz


Set the ownership and group to root.
  Code:
chown -R root:root /usr/lib/jdk-whatever-version-you-are-using


Make a few symlinks so we don't have to add anything to the path.
  Code:
ln -s /usr/lib/jdk1.8.0_60/bin/java /etc/alternatives/java
ln -s /etc/alternatives/java /usr/bin/java


You're done with this. Change to a directory off the path and run # java -version just to prove everything is OK.



Just a thought! - You virtual machine builders may want to take a snapshot here.



********************************************************************************
ffmpeg build
********************************************************************************
Notes:
Serviio provides ffmpeg-2.4.x.tar.bz2 from their download page. They seem to recommend that, and they may have patched it a bit too. The also have links to libRTMP 2.4+ and Lame MP3 Encoder (v. 3.99.5) to be built alongside the ffmpeg. I didn't need to build those in order to build ffmpeg. The libraries for the latest versions were already available from the Ubuntu repos for 14.04.3 LTS. You're mileage may vary. (See the supplemental information that follows these instruction to verify that I used the correct versions of these libraries.)

I used ffmpeg-2.7.2.tar.bz2 from ffmpeg.org and the available dev libraries from the Ubuntu repos. I have tested the 2.4.x from Serviio, and it works fine, but 2.7.2 will be my primary focus.

Ubuntu 14.04 does not include ffmpeg in the repositories. If you have a later version of Ubuntu you might be able to avoid building ffmpeg, if and only if it already has all the options you need built in; not too bloody likely. I chose to build the latest of ffmpeg here because they fix a lot of bugs at ffmpeg.org . 2.4 from Serviio might be a more conservative way to go.

I adapted (and slightly expanded) the build instructions from http://wiki.serviio.org/doku.php?id=build_ffmpeg_linux . I owe the authors of this linked web page a debt of gratitude. Once I opened my mind you taught me a ton. Thank you very much!

The options for the apt-get and configure parameters are in order of appearance (mostly) from ffmpeg's ./configure --help


Steps:
Install the necessary tools and dev libraries (The web page linked above listed more but they were not necessary for my build. It won't hurt a thing if you want to add them.) Cut and paste:
  Code:
apt-get install build-essential yasm pkg-config libass-dev \
libfaac-dev libmp3lame-dev libopencore-amrnb-dev \
libopencore-amrwb-dev librtmp-dev libspeex-dev libtheora-dev \
libvorbis-dev libx264-dev libxvidcore-dev libx11-dev


Download and untar your chosen ffmpeg source code. (I'm using the latest from ffmpeg.org here.) Change into the directory. Something like:
  Code:
tar xf ./ffmpeg-2.7.2.tar.bz2
cd ffmpeg-2.7.2


Now we have to generate the make files with all the options we want enabled. (This is fast.) Cut and paste:
  Code:
./configure --enable-gpl --enable-version3 --enable-nonfree \
--enable-postproc --enable-fontconfig --enable-libass \
--enable-libfaac --enable-libfreetype --enable-libmp3lame \
--enable-libopencore-amrnb --enable-libopencore-amrwb \
--enable-librtmp --enable-libspeex --enable-libtheora \
--enable-libvorbis --enable-libx264 --enable-libxvid \
--enable-x11grab

- There should be no errors, but perhaps a few warnings (warnings are not usually show stoppers).
- Compare your results to mine in the supplemental info below.

Now we run the actual make process. Our heavy lifting is done so now it's the compiler's and linker's turn to work. Just set it loose and go get a nice cup of coffee or tea, maybe with a splash of milk. (This might take a while.) Cut and paste:
  Code:
make

- You will have some warnings, but they're probably OK. It's the errors that must be fixed.
- Compare your results to mine in the supplemental info below.

Did you have errors? No? Jump down to the next instruction. Yes? You will have to try to figure out what went wrong. I think the most likely problem you would have at this point is not having all the right tools installed. Leave a comment and I'll do my best to help you out.

Error free? Great! We're finally ready to install this thing. If you want to make a deb file for future installs skip to the very next step. Cut and paste:
  Code:
make install

- This should be fast and easy. Now you're done. Jump on down to the Serviio installation

Optional - Here's an optional install method that not only installs ffmpeg on your system, but also generates a deb file for future installs. Take note that I have not tested the subsequent use of the deb. Give it a try and let me know how well it works for you. Cut and paste:
  Code:
apt-get install checkinstall
checkinstall --pkgname=ffmpeg \
--pkgversion="99:$(date +%Y%m%d%H%M)-git" --backup=no \
--deldoc=yes --fstrans=no --default

- Note - I didn't go beyond makeing sure that the ffmpeg application installed on my system and that the deb file got created. I did not test using the deb file for other installs.
- If you want to save the deb file and roll back to your previous snapshot to avoid having those devel libraries hanging around, now is the time to give it a try.



Just a thought! - You virtual machine builders may want to take a snapshot here.



********************************************************************************
Serviio server installation
********************************************************************************
Notes:
This is the easiest part of the whole process. You're probably wishing you had gone with the Windows or Mac version by now, but you're here now so let's do this.

I have chosen to install the Serviio under the /opt directory structure. Why? Because Serviio store its data under its installation directory and I HATE, HATE, HATE, when programs store data in any of the ./bin or ./sbin directories (/, /usr, or /local). I see it as FHS directory polution. Just my opinion. Some days I'm just a little cranky. But seriously, install it whereever you want. Just substitute your path as appropriate.

I create a symlink in /opt called /opt/serviio. This lets me completely change the actual Serviio software to a different version or do a clean installation without having to change my desktop shortcuts or other scripts. Great for doing updates while preserving your database and settings! Just change the symlink in one place to the new / desired Serviio directory and everything else just works.

Highly recommended - Perform the optional steps below to create a dedicated user account with restricted rights to run Serviio programs. If you don't then you will need to set the appropriate file and directory permissions the way you like them.


Steps:
I decided that RAW image support would be nice. If you want this too then install dcraw. Cut and paste:
  Code:
apt-get install dcraw


Do I have to say to download Serviio? Nah, but I will anyway. Here's how I did it. Make sure you get the most recent version. You may do something like this:
  Code:
wget http://download.serviio.org/releases/serviio-your.version.goes.here-linux.tar.gz


Untar your serviio-whatever-the-tar-is-called to /opt. You will do something like this:
  Code:
cd /opt
tar xzf serviio-your.version.goes.here-linux.tar.gz


Make a symlink in /opt from your specific version directory to a generic directory link called /opt/serviio/. Do somtehing like this:
  Code:
ln -s /opt/serviio-your.version.goes.here /opt/serviio


If you don't want to create a restricted user account then you're done. You will find the server and console start scripts in /opt/serviio/bin . Enjoy!



Just a thought! - You virtual machine builders may want to take a snapshot here.



********************************************************************************
Optional - Create a restricted user account to run Serviio.
********************************************************************************
Notes:
We will create a limited user account, set the ownership and permissions, and finally create a startup script and configure it to run at boot.

The way I'm doing it here, only the user serviio and root will be able to get to the log and config files. If you have a designated user that needs to get to these files just add them to the serviio group.

Important -> Did you make the symlink I suggested? All the commands below rely on that symlink you created.

chown does not follow symlinks by default. Add the -L flag.

chmod always follows symlinks.

Do not test run Serviio from the root account. Even though we're creating a user with no default shell, you can still use that restricted account to launch Serviio from the command line. I'll show you how. You do not need to test by using the root account. Don't do it! (OK, it is your system, so go ahead if you must. It's not my place to tell you what to do, but I wouldn't do it. I actually did test as root the very first time I started this write up, but then I rolled back to my previous snapshot.)

Compound commands for adding the user and setting the permisions are right here for your convenience. See the individual commands below for explainations.
# adduser --system --group --disabled-password serviio && chown -R -L serviio:serviio /opt/serviio && chmod -R o= /opt/serviio && find -L /opt/serviio -type d -print0 | xargs -0 chmod u=rwx,g=rwxs && chmod -R ug+rw /opt/serviio

Steps: (Did you see the compound convenience command just above?)
Add a user with very limited rights that the daemon will run as.
# adduser --system --group --disabled-password serviio
Add a user named serviio. (Whatever you want to call it is fine.)
Setup the new user as a system user that, by default, will have no login shell.
Create a group with the same name and use as primary.
The password is disabled for tighter security.

Set the ownership
  Code:
chown -R -L serviio:serviio /opt/serviio


Revoke all rights from other
  Code:
chmod -R o= /opt/serviio


Make the group ownership sticky for all the directories, just toggle the gid sticky flag but nothing else
  Code:
find -L /opt/serviio -type d -print0 | xargs -0 chmod g+s

- this format which uses xargs handles spaces in path and file names very well

Make sure that the user and group file permissions are both read and write. Leave execute alone.
  Code:
chmod -R ug+rw /opt/serviio




Just a thought! - You virtual machine builders may want to take a snapshot here.



********************************************************************************
Optional - Run Serviio from the command line using our restricted user account.
********************************************************************************
Notes:
Do not run as root. Don't do it! Even though our limited serviio user has no default shell and the password is disabled, there is an easy way to run the program. You don't need root for this.


Steps:
Run Serviio like this:
  Code:
su -s /bin/bash -c /opt/serviio/bin/serviio.sh serviio

- use the super user command
- tell it to use the bash shell
- tell it to run the command /opt/serviio/bin/serviio.sh
- tell it to use the user serviio

Ctrl+C to break out of the server. It will close cleanly.

Run the Serviio Console and start adding some "stuff" to your library. Either run the console as root or add your chosen user to the serviio group.
  Code:
/opt/serviio/bin/serviio-console.sh


If you need a refresher on adding a user to the serviio group, here's a refresher:
  Code:
usermod -a -G serviio sparky

- adds user sparky to the serviio group so sparky can run the console
- log the user out and then back in to take affect



(A little off topic.)
An easy way to test audio transcoding fast is to set your device's profile to DirecTV and restart Serviio. This profile transcodes nearly everything, so pick a non-mp3 audio file and see if it gets delivered as an mp3 to your renderer.



********************************************************************************
Optional performance tweak - setup a RAM drive for transcode operations
(this will be 10x or more faster than any SSD)
********************************************************************************
Notes:
I do not know what the behavior with videos that are larger than the RAM disk is. I didn't test that aspect. Do some research and please tell me how it worked out for you.

This assumes you will use the system /tmp for transcoding. If you will transcode to a different directory you will have to modify the line you add to fstab.

Beware that if your total RAM is too low then all kinds of ugly swap file action will take place and your system will slow to a crawl. I tested with 3GB of RAM, I think 2GB would work too.

The noexec option is not appropriate if you will use this machine for compiling and building other apps. Build tests will frequently use /tmp to save executable files and then try to run them. I use the noexec parameter because my "NAS Gizmo" is not my development machine. I also use noexec on the entire /home directory tree.


Steps:
Edit your /etc/fstab and add the following line. Cut and paste:
  Code:
none   /tmp   tmpfs   nodev,noexec,size=25%   0   0

- There is no source device.
- Assumes you are mounting at /tmp (make this match whatever you set as the transcode directory).
- The tmpfs file system type is dynamic and only takes as much RAM as it needs without preallocating.
- The options nodev and noexec help with security a little bit.
- I used size=25%, which uses up to 25 percent of the total RAM.
- 0 0 just standard dump etc. options.



********************************************************************************
Optional - Upstart - Let's make Upstart start Serviio when the computer starts.
********************************************************************************
Notes:
This script is only for system using Upstart. It's standard on Ubuntu 14.04 and some of the earlier versions. It's not for Ubuntu 15.04 and above because Ubuntu now uses systemd for its init process. (We'll get to systemd next.)

Read the comments in the script for explanations about why I chose any particular option. I had my reasons. Sure, they may be wrong, but Ubuntu has abandoned Upstart anyway.

Steps:
Go to the /etc/init direcotry and create a file called serviio.conf and copy the script commands below into it. (I use nano.)
  Code:
cd /etc/init
nano -w serviio.conf


Here's what goes into your script:
  Code:
# Sparky's Serviio Upstart startup script 20150910
# no copyright asserted, released to the public domain

description "Serviio Media Server"

# only run during normal startups
start on runlevel [2345]
# wait for the network interface(s) to come up
start on started networking
# Optional - Wait until some directory is mounted. Especially
# useful for slow mounts like cifs or nfs.
# To wait for a mount uncomment the next line and edit.
# start on mounting MOUNTPOINT=/home/pubshare/AVmedia
stop on shutdown


# If you need to set the JAVA_OPTS environment variable you should
# probably just change it in the serviio.sh script instead of here
# because the script doesn't check to see if it has already been set.

# Set the user and group to the  account Serviio should run as.
setuid serviio
setgid serviio

# Use respawn carefully, Serviio has bombed out on me a few times
# so I'm going to use respawn. If your system is unstable then using
# respawn might not be a great idea. Instead, you should check the
# logs and fix the root cause.
respawn

# If everything in this script can function using your restricted
# user account then you don't need the start-stop-daemon helper
# that I've seen used in other Upstart scripts.
exec /opt/serviio/bin/serviio.sh

# end serviio.conf




Just a thought! - Everything running the way you like? Now might be a good time to export your VM to a template file (OVA, OVF, etc.). You can delete those pesky snapshots too; they're really not appropriate for a production machine.