LAMP stack on Raspberry Pi with Kodi running at startup

For most people, if they wanted to run LAMP stack on Raspberry Pi, they would probably install Raspbian via NOOBS. However, if they wanted a HTPC with Kodi, they would likely opt for OSMC or OpenELEC. I needed Kodi to run at startup with LAMP stack running behind and chose the NOOBS Raspbian route.

Hardware

I am using Sandisk 8GB Class 4 on Pi 2 Model B. Initially, I was using Sandisk 16GB Class 10 but I am re-purposing it for some other usage.

For my use case, the Class 4 card is not the best though certainly not a bad choice. Good Micro SD cards outperform spindle hard disks when measuring in seek time which allows quicker random access. Google for this subject and you should find many discussions.

As for raw throughput, the results of the benchmark:

sudo hdparm -tT /dev/mmcblk0
/dev/mmcblk0:
Timing cached reads: 738 MB in 2.00 seconds = 368.52 MB/sec
Timing buffered disk reads: 66 MB in 3.09 seconds = 21.38 MB/sec

I hooked a keyboard mouse combo, Logitech K400 and attached CAT5 Ethernet cable.

Installing Raspbian

Installing Raspbian Lite

Refer to Using Raspbian on 2GB micro SD card for setup instructions.

Raspbian Lite does not come with GUI and as such is more lightweight than the full Raspbian.

Installing Raspbian via NOOBS

Follow the NOOBS setup guide on Raspberry official homepage. I must emphasis that the SD card should be formatted using SD Formatter tool as specified in the guide because NOOBS threw an error saying size was different or something like that so be sure not to skip this step!

Raspbian comes with many apps that I do not need. To uninstall:

sudo apt-get purge -y libreoffice scratch minecraft-pi bluej greenfoot sonic-pi wolfram-engine idle idle3 claws-mail
sudo apt-get autoremove -y

To ensure remaining apps are up-to-date:

sudo apt-get update
sudo apt-get upgrade

Disable booting to desktop

This step is not needed if you opted to install Raspbian Lite.

Since I want Kodi to run at startup, there is no need for the desktop to show up.

Bring up Raspberry Pi configuration tool:

sudo raspi-config

Then go to Boot Options. Choose either Console or Console Autologin. The Autologin was useless to me because I usually manage my Pi via SSH.

You should change the default password. Go to Change User Password.

Installing Apache, MySQL and PHP

There are way too many guides of such. For simplicity, my recommendation is to follow the guide on Raspberry homepage, Build a LAMP Web Server with WordPress.

Setup firewall

The simplest tool is UncomplicatedFirewall (UFW). It encapsulates the complexity of iptables command.

sudo apt-get install ufw -y

To disallow all incoming traffic by default:

sudo ufw default deny incoming

To allow all outgoing traffic by default:

sudo ufw default allow outgoing

To allow all incoming traffic from any machine on LAN (with assumption you are on 192.168.0.x subnet otherwise, change accordingly):

sudo ufw allow from 192.168.0.0/24

To enable the firewall:

sudo ufw enable

If you got kicked out from the SSH session, you have misconfigured the LAN subnet in the previous step. The only way back in is to hook a keyboard and screen to Pi to fix the problem.

To allow incoming HTTP and HTTPS traffic from any IP:

sudo ufw allow http
sudo ufw allow https

Installing Kodi

Before you begin, the GPU on Pi 2 needs sufficient memory and many publicly available articles have pointed 160MB to be the sweet spot.

Bring up Pi configuration tool again:

sudo raspi-config

Then go to Advanced Options > Memory Split and enter 160.
You will be prompted to reboot, agree to it.

To install kodi:

sudo apt-get install kodi -y

Run Kodi on startup

There are many ways to achieve this.

The approach I used is to start it on boot via cron.

sudo crontab -e

Then add the following line:

@reboot kodi --standalone

Enabling Kodi remote

Referring to kodi.wiki, either edit guisettings.xml or advancedsettings.xml to achieve the same effect. For reasons I could not understand, editing the guisettings.xml does not seem to have any effect. The file gets reverted every time it starts again. Therefore, edit advancedsettings.xml instead.

nano ~/.kodi/userdata/advancedsettings.xml

Douglas Gross shared a comment suggesting to edit the following path instead. This is so that the advanced settings take effect on all users. Please note that sudo is required:

sudo nano /usr/share/kodi/userdata/advancedsettings.xml

If the file does not exist, nano will create it.

Then paste the following into it and save:

<advancedsettings>
    <services>
        <esallinterfaces>true</esallinterfaces>
        <webserver>true</webserver>
        <zeroconf>true</zeroconf>
    </services>
</advancedsettings>

Restart Kodi for this to take effect.

Internationalisation

If you need Raspbian to display Chinese, Japanese, Korean (and/or other) characters in file names, you need to enable some fonts. Run sudo raspi-config then go to Internationalisation Options > Change Locale. Add the following:

  • ja_JP.UTF-8
  • ko_KR.UTF-8
  • zh_CN.UTF-8
  • zh_TW.UTF-8

Be sure to keep en_US.UTF-8 as your default if you do want it to be.

In Kodi, go to Settings > Appearance > Skin > Fonts and select Arial.

Improving performance

If you have reached this point, you have working LAMP stack and Kodi. If you are interested in maximising the ability of Pi, read on.

Buy MPEG-2 license key

Kodi runs fine out of the box but I needed it to utilise the GPU so that the CPU can be offloaded to run LAMP quicker. For this, I purchased MPEG-2 license key from Raspberry and waited for a day to receive the license via email.

To activate the key, edit the boot config:

sudo nano /boot/config.txt

and add decode_MPG2=<license> at the end of file replacing <license> with the actual license.

Disable swap file

You may ask, why?

  1. To improve longevity of the micro SD card by not using it as virtual memory.
  2. To save space on the micro SD card by not having a swap file.

Convinced? Do the following:

sudo dphys-swapfile swapoff
sudo dphys-swapfile uninstall
sudo update-rc.d dphys-swapfile remove

To check available free memory:

free -m

I have in excess of 500MB on my Pi 2 Model B with LAMP stack and Kodi running.