Setting Up My RaspberryPi

For ages I was thinking of setting up a NAS and make all my music and series available in the network / use it as a backup. But apparently those NAS-systems are pretty expensive, so the cheapest choice was a RasPi. I guess that is kind of a traditional choice for starters.
However, since it was said there would be no Model 3 "in the near future" up until the end of January 2016, I bought it around New Years, played around with it a lot. Installed a ton of software, made me comfortable working with it.

Well, it's time to restart from scratch with a clean installation now and do what I wanted to do the first time already: to remember the first steps, write down what I do / need.
So I have it all in one place and don't need to search for each console command again.
How often do you do a re-install anyway? But that also means, I am merely putting some pieces together here for the lazy. *cough*

Content:

  1. Step 1: Installing Raspbian (OS)
  2. Step 2: Configuring WLAN
  3. Step 3: Updating Raspbian
  4. Step 4: Compiling ffmpeg
  5. Step 5: Mount External HDD
  6. Step 6: Installing Git
  7. Step 7: Bonjour Service
  8. Step 8: Installing Apache
  9. Step 9: Installing Cups
  10. Step 10: Installing Plex

Step 1: Installing Raspbian (OS) Back to Top

The easiest thing first: Get the SD card, put it into your computer, or a card reader. Download NOOBS, since I am going to use Raspbian, I am content with only downloading NOOBS. If you want to install any other OS, the Lite version will do and the respective OS.
After the download, unzip it and copy the content to your freshly formatted SD card. If you downloaded another OS than Raspbian, put the content of that (unzipped) OS into the "os" folder in NOOBS. Put the SD card into your RasPi, plug it in on a monitor boot it up and follow the instructions on screen and wait for NOOBS to finish the installation.

Step 2: Configuring WLAN Back to Top

Before we can do anything, we need to setup WLAN to have access to the network we're in (and web). The new model 3 has WLAN on board, but I assume it's still pretty similar for that. I use the (pretty much) standard Edimax WLAN Dongle for my model 2.
I set my RasPi to use a static IP, so I always know what IP address to connect to. Later, I am going to configure bonjour, where I do not necessarily need the IP but Android devices still have issues with that, that's why it's always good to know the actual IP address of your Pi.

I will presume the WLAN is available to your Pi and not extra check for a working WLAN stick or alike. The configuration for the WLAN is done in /etc/network/interfaces, but the Edimax Dongle has a sleep mode, potentially interrupting connections.
First let's deactivate that:
sudo nano /etc/modprobe.d/8192cu.conf

Put this inside and save it:
options 8192cu rtw_power_mgnt=0 rtw_enusbss=0

Now we can edit the interface itself to connect to our WLAN.
sudo nano /etc/network/interfaces

This file will have some default values already, important here is actually only the "wlan0" settings. If you have the RasPi on a cable, then perhaps eth0 is of importance for your (different) settings. But we want WLAN and a static IP, so the content should look similar to this:

auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.0.1

wpa-ap-scan 1
wpa-scan-ssid 1
wpa-ssid "Your WLAN name"
wpa-psk "Your WLAN password"

The address may look different for you, it depends on your router and the settings to be exact. My router uses 192.168.1.* by default. Yours may be 192.168.0.*.
On a Windows machine you could fire up a console and enter ipconfig and look what kind of IPv4 addresses your router shares. Possibly a good idea to do that anyway, since the "gateway" is listed there as well that you need to put in the file. Save it, now we need to restart the network connection (service) of the Pi, so that it knows, we can try to connect to the internet:
sudo service networking restart

Done, now we should be able to access the network.

Source: datenreise.de

Step 3: Updating Raspbian Back to Top

When you download the OS it should already be up to date, but let's just be on the safe side here and check it manually. After NOOBS is done with the installation and the Pi booted to the desktop, fire up a console and enter:
sudo apt-get update && sudo apt-get upgrade

It will update its repositories and potentially try to upgrade some things and ask you beforehand. If apt-get asks to install or update anything, simply hit the y key.

Step 4: Compiling ffmpeg Back to Top

I am going to use my RasPi for media and backup, that's why it's a good idea to install ffmpeg. But since that one is not available as binary for the RasPi, we need to compile it on our own. That one is a bit tricky. As it turns out this second time I got a sharing issue with a lib as I forgot to make that lib known to the linker. Oh, well, no biggy.

First, we need to install some libs, some of those should be already available, like git, but let's be on the safe side again:

sudo apt-get -y --force-yes install libmp3lame-dev libx264-dev yasm git autoconf automake build-essential libass-dev libfreetype6-dev libtheora-dev libtool libvorbis-dev pkg-config texi2html zlib1g-dev

Let it install, when it's done, we need to compile libfaac, let's do these in a row:

cd /usr/src
sudo wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz;
sudo tar -xvf faac-1.28.tar.gz;
cd faac-1.28/;
sudo nano +126 common/mp4v2/mpeg4ip.h;

The last command opens that header file on line 126, a line we need to delete.
Now, usual config/make/install and make it known to the linker with ldconfig. Don't forget the last one, without it, you probably run into the same sharing error as me.

sudo ./configure
sudo make
sudo make install
sudo ldconfig

Finally, we can get to ffmpeg itself.

cd /usr/src
git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
sudo ./configure --enable-libfreetype --enable-gpl --enable-nonfree --enable-libx264 --enable-libass --enable-libfaac --enable-libmp3lame --bindir="/usr/local/bin"
sudo make
sudo make install

All things should go smooth. But it's compiling, so something may go wrong, but it may take a bit of time to finish. Try out ffmpeg afterwards, if you see no errors, congrats.

Source: bitpi.co

Step 5: Mount External HDD Back to Top

The next important thing is an external harddrive for all the data. The internal SD card is busy enough and holds only so much data. Plug in the HDD into USB and check if it is available for the Pi with sudo blkid.
It gives you a listing with all storages, the swap file, the SD card, some other stuff and this:
/dev/sda1: LABEL="Elements" UUID="..." TYPE="ntfs" PARTUUID="..."

I use a Western Digital Elements HDD and therefore know, it's that one. I also see, it's on /dev/sda1, but I want to mount that drive into /hdd, so I need to create that folder first, before I can mount the hdd into it.

sudo mkdir /hdd
sudo mount /dev/sda1 /hdd
sudo chmod 775 /hdd

The last command gives us full access to the drive. But this mount is only for this session, rebooting will not automount the HDD. Let's set this up then, enter sudo nano /etc/fstab and add the line /dev/sda1 /hdd ntfs defaults 0 0. Done.

In case you change the USB port your HDD is plugged in, you might not be able to reboot normally and be stuck on bootup. If you get a "Load Kernel modules failed" error and somehwere a 1:30 timer running up, you need to simply change the /etc/fstab file accordingly again by checking sudo blkid first and edit fstab respectively.

Source: modmypi.com

Step 6: Installing Git Back to Top

I want to use git as a backup for my projects. I already added a git push pa command to let git push to a local backup repo and the Pi repo in one go. For Git it's a good idea to create a new user to handle this. The benefit is, git being its own user has another password.
adduser --system --shell /bin/bash --gecos 'gitpi' --group --home /home/git git

You can give that new username a new password with sudo passwd git.
That's basically it for this step. We installed git either beforehand for compiling ffmpeg or is (as it should) installed already by default.

Step 7: Bonjour Service Back to Top

Bonjour is a service that gives your devices mnemonic names in your network. That way you can name your Pi how you want and use that name to connect to, instead of the IP. Sadly, this won't work for Android devices, if you want to connect to an Android device or from an Android device (like a tablet), you need to use the IP address, hence I use a static IP for the Pi.
However, per default the bonjour service is already installed, but let's make sure it is:
sudo apt-get install avahi-daemon

That daemon will handle the bonjour service. To make it autostart with the Pi, we're simply doing sudo update-rc.d avahi-daemon defaults

Now we can name our RasPi, default is raspberry I think. Type in sudo raspi-config, go to Advanced Options -> Hostname and enter your desired name. Mine is simply rpi and is therefore known in the network as "rpi.local" instead of "192.168.1.101", well, for non-Android that is.

Step 8: Installing Apache Back to Top

Not only is this RasPi going to do media related stuff, but also work as a testing ground for my web stuff and I need a running Apache for it. That way I have a working intranet, which is nice, too.

Installing apache is pretty straightforward:
sudo apt-get install apache2 php5 libapache2-mod-php5 php5-sqlite php5-gd python -y

This will install php5, including PDO_SQlite, gd and python (2), packages I know I will need. But since I don't want to use the default folder for apache, it's time to change the DocumentRoot:
sudo nano /etc/apache2/sites-available/000-default.conf

aaand now changing the old DocumentRoot in the main config (sudo nano /etc/apache2/apache2.conf) so that it looks like this:

<Directory /new/path/>
    Options Indexes FollowSymLinks ExecCGI
    AllowOverride None
    Require all granted
</Directory>

Mind the ExecCGI, that was added. Save and now enable CGI by typing sudo a2enmod cgi, we should almost be able to execute *.pl/*.py files as well in the apache webfolder. If you want to use .htaccess files you may want to change AllowOverride None to AllowOverride All so you can change things in your htaccess however you want and perhaps enable the rewrite engine to use rewrite rules, for that you need to enable it with sudo a2enmod rewrite.
Last but not least you need to add .pl/.py files to the AddHandler, do sudo nano /etc/apache2/mods-enabled/mime.conf and look for the line that is commented out, saying # AddHandler cgi-script .cgi. Change it to AddHandler cgi-script .cgi .pl .py

Save it, and restart the server: sudo apache2ctl restart. Done.
If you now want to make sure, apache is started automatically after boot, then do a simple sudo update-rc.d apache2 defaults.

Step 9: Installing Cups Back to Top

I want to attach my printer to the RasPi and be able to print from within my network. I could attach my printer on its own but the settings are weird, so let's use Cups to handle the printer sharing in the network. Waaay more simple.
sudo apt-get install cups

Hit yes, when you are asked. This will install some packages more than usual.
Afterwards we need to change some settings.
sudo nano /etc/cups/cupsd.conf
Search for the line Listen localhost:631 and change it to Port 631 or any other port, you wanna use. That way all network devices can access the printer.
But Cups yet denies access, so add the line BrowseAddress @LOCAL and change each Location by adding Allo @LOCAL:

<Location (...)>
    (...)
    Allow @LOCAL
</Location>

Now, let's add another user for the printer, this is technically not necessary but a good idea anyway: sudo usermod -aG lpadmin pi and now restart Cups with sudo service cups restart, open your browser and point it to rpi.local:631 and add your printer. Done.

You need to add the printer in your network devices, on Windows this can be done as described here.

Source: elektronik-kompendium.de

Step 10: Installing Plex Back to Top

Last but not least, Plex. The RasPi has decent enough power to run Plex. Not sure how that looks like in a household of a family with more than 3 heads, but that's not my use case. That one is a bit longer, so go check it here:
http://www.htpcguides.com/install-plex-media-server-on-raspberry-pi-2/