14 April 2013

Setup a Raspberry Pi LAMP web server from scratch (without monitor, keyboard or mouse attached)


Friday I received my Raspberry Pi. My intention is to use it as a webserver. When you receive a Raspberry Pi it doesn't do anything straight out of the box. In this blogpost I will give you an overview of how I set up my Raspberry Pi with just:

  • The Raspberry Pi itself
  • A 4GB SD card

So I never connected a screen, keyboard and mouse to it, to install it.

Prepare the SD card

Download the image (I used the Raspbian “wheezy”) and put it to an SD card. To make this process easy I used the Pi filler (for Mac OS X 10.5 through 10.8). Read more on how to set up your SD card here.

SSH to your Pi

Insert your prepared SD-card in your Raspberry Pi, connect your ethernet and hook it up to power. Your Raspberry Pi will boot.

Since the latest images SSH is enabled by default. This makes it possible to directly connect to your Raspberry Pi, without attaching a monitor, keyboard and mouse the first time.

You can find the IP-address of your Raspberry Pi in your router, or use a tool like Pi Finder.

If you open a terminal (e.g. Terminal on Mac) you can connect to your Raspberry Pi:
ssh pi@192.168.xxx.xxx
The default password is "raspberry".
But to make life easier, you can copy your SSH certificate to the Raspberry Pi to make logging in very easy:
cat ~/.ssh/id_rsa.pub | ssh pi@192.168.xxx.xxx 'cat >> ~/.ssh/authorized_keys'
Just type the password one last time and you're done!
After logging in,  you can change the default password:
sudo passwd pi <<<PASSWORD>>>

Basic Raspberry Pi config

Run the following, to configure your Raspberry Pi:
sudo raspi-config
raspi-config

I made the following settings:
  • Expand_rootfs, to have more storage space (makes it possible to use the full SD card)
  • memory_split, changed it to 16, because I don't need graphical memory for my webserver. 
  • boot_behaviour, "no", don't start the desktop
  • update, to update raspi-config

Update Debian

To update Debian:
  • sudo dpkg-reconfigure tzdata
  • sudo apt-get update
  • sudo apt-get upgrade

Install Git

sudo apt-get install git-core

Install automatic firmware updater and update

  • sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
  • sudo rpi-update
Now the firmware and software are up to date, reboot the Pi:
sudo shutdown -r now

Install Apache and PHP5

sudo apt-get install apache2 php5 libapache2-mod-php5
If you want to enable all error reporting you could do stuff like this:
  • echo 'error_reporting  =  E_ALL' > /etc/php5/conf.d/error_reporting.ini
  • echo 'display_errors = On' >> /etc/php5/cond./error_reporting.ini

Install MySQL

sudo apt-get install mysql-server mysql-client php5-mysql

Restart Apache

sudo service apache2 restart

That's it!

Now you have a running webserver with PHP5 and MySQL. You can put your PHP files in /var/www/.