How to install LAMP on Raspberry Pi

This article gives instructions how to setup Raspberry Pi as a web server by installing LAMP (Linux, Apache, MySql, PHP). Raspberry Pi is very fit to work as a continously running web server: it is cheap, low-powered, it has Ethernet and it is very reliable. And with its USB and GPIO you can easily build connections to real-life (sensors etc.)

Requirements

You need:

  • Raspberry Pi with power + Ethernet cables.
  • SD card (at least 2GB, preferrably something like 8GB, Class 4 or better, but do NOT user class 10 since they seem to have data corruption issues with Raspi).
  • Network router (Ethernet or WiFi) that has access to Internet. This can be for example your home ADSL/WiFi router.
  • Monitor/TV + HDMI cable + USB-keyboard (temporarely for initial setup).
  • Windows/Linux laptop (to remotely access and control the Pi).

Prepare SD card with Linux for Raspberry Pi

Raspberry Pi basic setup for web server use

  • Insert SD card to Raspberry Pi, connect HDMI to monitor/TV and USB keyboard, attach power to boot up Raspberry Pi.
  • Raspi-config is started automatically on the first boot. If not, log in and start it manually by typing “raspi-config”.
  • Select “Expand Filesystem” to use your whole SD card (instead of only 2GB).
  • Select “Change User Password” and select new password for user “pi”. This is really good idea to change the default password, because you will open the Raspberry Pi to the internet.
  • Select your timezone from “Internationalisation Options” / “Change Timezone“.
  • Select “Advanced Options” / “Memory Split“. Select only 16MB for GPU, because you will not be using the graphical UI.
  • Select “Advanced Options” / “SSH” and enable SSH server. This enables you to login remotely and forget the HDMI/USB-keyboard.
  • Select Finish, which closes raspi-config.
  • NOTE: at this phase, you can do the static IP address assignment from the command prompt using the USB keyboard (see instructions in the next section). Or, you can continue and log-in remotely using the DHCP-assigned IP address as explained in the next section.
  • Reboot.

Setup remote connection (SSH) and static IP address

  • Connect your Raspberry Pi to your local network router with the Ethernet cable, start it, and check its assigned IP address from your network router (DHCP/ARP tables). How to actually do this depends on the configuration UI of your network router.
  • Download and install Putty from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
  • Using Putty, login with SSH to Raspberry Pi using its IP address.
  • Assign static IP address to your Raspberry Pi editing its network configuration file:
cd /etc/network
sudo nano interfaces
  • Edit the line containing “iface eth0 inet dhcp” and replace it with following (obviously change addresses according to your network and make sure to select IP address outside the DHCP range):
iface eth0 inet static
  address 192.168.0.7
  netmask 255.255.255.0
  gateway 192.168.0.1
  • In case you are using WiFi adapter in the USB port, this is the correct configuration:
iface wlan0 inet static
  address 192.168.0.7
  netmask 255.255.255.0
  gateway 192.168.0.1
  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
  • In case you are using WiFi adapter you also need to edit file “/etc/wpa_supplicant/wpa_supplicant.conf”:
network={
  SSID="yourWifiName"
  password="yourWifiPassword"
}
  • Reboot (“sudo reboot“) and your Raspi should have the assigned static IP address. You can now connect to it using Putty SSH.
  • Different way to assign static IP address to your Raspberry Pi is to configure the network router so that it assigns the an IP address to your Raspi based on its MAC address. How to actually do this depends on the configuration UI of your network router.

Install software

  • To install some basic software and LAMP, issue the following commands in this order.
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install screen git-core

sudo apt-get install apache2
sudo apt-get install php5 libapache2-mod-php5
sudo /etc/init.d/apache2 restart
sudo apt-get install mysql-server
  • When asked, select password for user “root” for mysql. Note that this is different than user “pi” but you can also just use the same password if you want.
sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin
  • When asked, press space+enter to select “apache2″.
  • When asked, select password for user “phpmyadmin” to mysql. Note that this is again different user.
sudo /etc/init.d/apache2 restart
  • And you are done!

Test your installation

To test that the web server and MySql works, use your browser to access the following addresses:

http://192.168.0.7
http://192.168.0.7/phpmyadmin
  • To access phpmyadmin, login with user “root” and the previously selected password.
  • In phpmyadmin, create new databases/tables or import existing ones.
  • Copy/create HTML/PHP files to folder “/var/www“.
  • If you want your newly created web server to be accessable from the public internet, you need to configure your network router to forward traffic from port 80 to the local IP address of Raspberry Pi.¬†How to actually do this depends on the configuration UI of your network router.

Configure remote X connection (OPTIONAL)

If you want to use start GUI software and have its UI rendered remotely to your Windows computer/laptop using X server, follow these steps to configure the connection. This is a great way to use your Raspi remotely without attaching monitor/TV via HDMI and still be able to use GUI software.

  • Download, install and start Xming (X server for Windows) from http://sourceforge.net/projects/xming/
  • Open Putty and before connecting to Raspi, open connection configuration options (list on the left), select “Connection” / “SSH” / “X11” and check “Enable X11 forwarding” and for “X display location” enter “localhost:0“.
  • Save connection settings for later use and connect.
  • Now if you open any software on the Raspi, the UI should open to your computer/laptop screen.

Install MySql development libraries (OPTIONAL)

If you want to create (C/C++) software that enters data to your MySql databases, you need to install MySql development libraries:

sudo apt-get install libmysql++-dev
  

Comments are closed.