LAMP stack on Raspberry Pi with Kodi running at startup

  • Last updated on March 5, 2016
  • Web

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.


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
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 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

Since I want Kodi to run at startup, there is no need for the desktop to show up. 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

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.

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

If you prefer Kodi to run on startup, you need to edit /etc/default/kodi and change ENABLED=0 to ENABLED=1. The simplest editor is nano.

sudo nano /etc/default/kodi

Verify that Kodi can start as a service: sudo service kodi start

Noticed that in /etc/default/kodi, the user to run Kodi as is kodi.

In order for a full-sized keyboard to behave as it should in Kodi, you need to grant permission: sudo nano /etc/udev/rules.d/99-com.rules and add KERNEL=="tty[0-9]*", GROUP="tty", MODE="0660". Reboot to test that it takes effect.


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, 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.