Cookbook - Building A LAMP Server On The Raspberry Pi Computer - GeoThread
Cookbook - Building A LAMP Server On The Raspberry Pi Computer - GeoThread
12/26/13
tool, not the Windows format programme. For advice on easy set up of the card, see http://elinux.org/RPi_Easy_SD_Card_Setup. Next, download the latest Pi Operating System, called Raspian Wheezy, from http://www.raspberrypi.org/downloads. If you download a windows file, you can unzip the contents to reveal the .img imagefile. Next, to write the image to the blank card, use the SDHC Image writer from https://launchpad.net/win32image-writer -(now at)> http://sourceforge.net/projects/win32diskimager/ Other scary options that some people use include the flashnul-1rc1 or the fedora-arm-installer-1.0.3-7-x32 tools. The flashnul-1rc1 in particular needs especial care and attention (being in Russian comrade!) Once Raspian is unpacked some files will be visible on the card in Windows but dont edit them in windows! Some files are hidden to Windows so it doesnt look like a full card. Insert the card then into the Pi and bootup. This should start up and kick off raspi-config. The default account/password is: User: pi Password: raspberry
You should be running in terminal mode, type startx to kick off the graphical user window environment (this is something a lot of guides seem to fail to mention!)
s t a r t x
Spend a little time now exploring the graphical interface. The menus and system options are available from a drop down menu in the top left corner. Note the very first time you run, you may be presented with the system configuration tool raspi-config, explained below. The rest of the article assumes an Ethernet cable connected to the Internet is plugged in. Note there are wireless USB dongles that should work with the Pi out of the box, instructions for configuring that are elsewhere here on Geothread see http://www.geothread.net/cookbook-configuring-wifi-on-raspberry-pi/. Basic Configuration You may find you are either running raspi-config immediately, or else this useful tool can be started at the command line withn the command sudo raspi-config. Note many of the commands in this tutorial are
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 2/12
12/26/13
preceded by sudo meaning run the command with root superuser authority. Once running raspi-config, you can select from a few useful options from its menu, thus:
e x p a n d _ r o o t f s
This option inflates the filesystem to fill SD card. This makes the whole capacity of the SDHC card available for storage a good idea!
s s hs e r v e rE n a b l e
The ssh secure shell allows programmes like putty on a PC to run a terminal session onto the Pi. However to do this the ssh server needs to be running and enabled on th Pi.
u p d a t e
Also select the option update to update the raspi-config itself Once you Finish, you will pass back to the command line. Next, you must update the core operating system files. You enter:
s u d od p k g r e c o n f i g u r et z d a t a s u d oa p t g e tu p d a t e s u d oa p t g e tu p g r a d e s u d oa p t g e td i s t u p g r a d e
This updates the system software if the Wheezy image was up to date this shouldnt take too long. If it wasnt time to make a cup of tea! Open source software gets updated FAR more frequently that proprietary software, so you will need to run the last three commands from time to time to keep the Pi up to date. To be able to access the Pi remotely, install the TightVNC server if required. This gives a graphical X-Windows
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 3/12
12/26/13
GUI onto Raspberry from remote laptop/PC/Mac. Note you also need to install TightVNC client on your separate laptop/computer that will be used to access the Pi.
s u d oa p t g e ti n s t a l lt i g h t v n c s e r v e r
Following the excellent instructions at http://www.neil-black.co.uk/raspberry-pi-beginnersguide#.UVWzqBfIbJY, next, configure the tightvncserver. Create a new file called tightvncserver in the init.d directory (nano is a text editor programme):
s u d on a n o/ e t c / i n i t . d / t i g h t v n c s e r v e r
# ! / b i n / s h #/ e t c / i n i t . d / t i g h t v n c s e r v e r V N C U S E R = ' p i ' c a s e" $ 1 "i n s t a r t ) s u$ V N C U S E Rc' / u s r / b i n / t i g h t v n c s e r v e r: 1 ' e c h o" S t a r t i n gT i g h t V N CS e r v e rf o r$ V N C U S E R" ; ; s t o p ) p k i l lX t i g h t v n c e c h o" T i g h t V N CS e r v e rs t o p p e d " ; ; * ) e c h o" U s a g e :/ e t c / i n i t . d / t i g h t v n c s e r v e r{ s t a r t | s t o p } " e x i t1 ; ; e s a c e x i t0
s u d oc h m o d7 5 5/ e t c / i n i t . d / t i g h t v n c s e r v e r
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 4/12
12/26/13
Now start or stop the service manually thus (this is the basic model used for dealing with all Debian processes)
s u d o/ e t c / i n i t . d / t i g h t v n c s e r v e rs t a r t
s u d o/ e t c / i n i t . d / t i g h t v n c s e r v e rs t o p
Make the TightVNC server start every time the Raspberry Pi starts.
s u d ou p d a t e r c . dt i g h t v n c s e r v e rd e f a u l t s 9 9
Setting an IP Address By default the Pi uses Dynamic Host Control Protocol (DHCP) meaning it may get a new IP address each time it boots up. If we are using the Pi as a web server/database server this could be a nuisance. If so, we can force the Pi to used a fixed IP address so it always remains the same. Following the excellent instructions at http://www.neil-black.co.uk/raspberry-pi-beginnersguide#.UVWzqBfIbJY, set the IP address from DHCP to fixed. This will make it easier to locate your webserver later. First, identify the current computer network IP address, type:
i f c o n f i g
On a home network, it is likely to be something like 192.168.1.xx. We will fix the Pi as 192.168.1.100. Edit the network interfaces configuration file with the text editor nano, thus:
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/
5/12
12/26/13
s u d on a n o/ e t c / n e t w o r k / i n t e r f a c e s
i f a c el oi n e tl o o p b a c k i f a c ee t h 0i n e td h c p
With
i f a c el oi n e tl o o p b a c k i f a c ee t h 0i n e ts t a t i c a d d r e s s1 9 2 . 1 6 8 . 1 . 1 0 0 n e t m a s k2 5 5 . 2 5 5 . 2 5 5 . 0 g a t e w a y1 9 2 . 1 6 8 . 1 . 1
Note we used 192.168.1.100 (this being a private network subnet address) See also http://my-music.mine.nu/images/rpi_raspianwheezy_setup.pdf for more guidance. Now reboot the Pi. To do this, type in:
s u d os h u t d o w nrn o w
This reboots the Pi now and then hopefully you can log on again, but this time potentially as a remote session with SSH or TightVNC from your laptop (where the laptop has a wireless connection to the router). Note the IP address is now fixed as noted above. If you have a laptop/PC on the same home network, you can open a command prompt and run ping 192.168.1.100 to see if you receive a response from the Pi. If you use tightvnc, rather than ssh, then on your laptop/pc vncclient installation, you need to enter in the address of the Pi for the connection, e.g. 192.168.1.100:1. Dont omit the :1 at the end of the address!! You can also explore the options to set the screen size geometry to 1024728 with a colour depth of 24bits.
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/
6/12
12/26/13
Linux Commands You need to be familiar with Linux commands. Learning Linux can be daunting some good documentation is here http://www.debian.org/doc/#manuals best is the one page reference card here http://www.debian.org/doc/#other. In the next steps, now the basics are in place, we proceed to the installtions of the web and database server software. See also http://www.wikihow.com/Make-a-Raspberry-Pi-Web-Server Apache Web Server and PHP To install the Apache web server and PHP (find out more here http://www.apache.org/ and here http://php.net/), type:
s u d oa p t g e ti n s t a l la p a c h e 2p h p 5l i b a p a c h e 2 m o d p h p 5
To restart, stop or start apache, use the following command (varying the last word)
s u d os e r v i c ea p a c h e 2r e s t a r t
Note the Apache webroot location for your webfiles is /var/www The Apache log files are put in /var/log/apache2 MySQL Database Server
s u d oa p t g e ti n s t a l lm y s q l s e r v e rm y s q l c l i e n tp h p 5 m y s q l
You need to enter in a database system password, such as raspberry! Once installed restart and you can hopefully access the MySQL command line prompt thus:
s u d os e r v i c em y s q lr e s t a r t m y s q l up i p
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 7/12
12/26/13
Check the interactive logon works as user pi. Try and use a database and select records:
u s em y s q l s e l e c t*f r o mu s e r s ;
Use <ctrl>+c to quit For logging, see http://serverfault.com/questions/71071/how-to-enable-mysql-logging PHP PHP should have been installed also. In the /var/www webroot, create a new file called testphp.php with the nano editor, and enter the following. Once created, run the file in the your laptop/pc browser to see the Pi PHP status:
s u d on a n o/ v a r / w w w / t e s t p h p . p h p
& l t ; ? p h pp h p i n f o ( ) ;? & g t ;
Your laptop/pc web browser should be able to see this web page served up. Enter the IP address we set earlier, plus this file, thus: In web browser, enter the URL http://192.168.1.100/testphp.php Hopefully you should see the PHP status page appear. Mysqli In PHP you should use the recommended MySQLi command extension to access the MySQL database. It
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 8/12
12/26/13
should be installed already but look down the phpinfo() web page above to find it and ensure it is shown as enabled. To learn more, see http://codular.com/php-mysqli Phpmyadmin To manage the MySQL database, we recommend the excellent phpmyadmin MySQL web management console. All the MySQL administration can then be undertaken through a web page. See http://www.dingleberrypi.com/2012/09/tutorial-install-phpmyadmin-on-your-raspberry-pi/ (but do check the phpinfo() output above first)
a p t g e ti n s t a l lp h p m y a d m i n
Select apache2 as the web server. Create a phpmyadmin password, such as raspberry (or whatever but dont forget it!) You should now be able to call up the phpmyadmion home page at: http://192.168.1.100/phpmyadmin login as root/raspberry Note user privileges to the MySQL database are all managed via phpmyadmin. Each user account needs to have specified the host from which the user will come to access the database. The % (all servers) option didnt work for us and so we needed to repeat a configuration for servers: 127.0.0.1, 192.168.1.100, localhost and raspberry pi. A bit fiddly essentially you open up the privileges for the user and duplicate the settings varying the host each time. To help with this, note that once phpmyadmin is installed, one can see the various options the phpmyadmin user has pre-assigned. Note that connecting to MySQL from the SSH terminal uses localhost, whereas connecting from a webserver can use 127.0.0.1 so the source varies according to source. One needs to experiment hmm! Remember later that if you ever receive a 500 error on the webserver when trying to access the database, it is likely due to the incorrect privileges. You may need to look at the webserver apache logfiles in a new window (/var/log/apache2) to actually see the error:
s u d om o r e/ v a r / l o g / a p a c h e
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/
9/12
12/26/13
or better still, to see only new lines as they appear in the logfile:
s u d ot a i l f/ v a r / l o g / a p a c h e 2
VSFTP We need the means to get web pages we write off the laptop/pc onto the Pi webserver. For this, we need an ftp daemon running to upload files onto Pi we suggest using vsftp, it is the best (but not the only) ftp server for debian.
s u d oa p t g e ti n s t a l lv s f t p d
Based on the instructions at http://www.instructables.com/id/Raspberry-Pi-Web-Server/step9/Install-anFTP-server/, now edit the vsftp configuration file.
s u d on a n o/ e t c / v s f t p d . c o n f
Search down through the file and change the following lines: anonymous_enable=YES Change To anonymous_enable=NO #local_enable=YES Change To local_enable=YES #write_enable=YES Change To write_enable=YES Also, add a line to the bottom of the file: force_dot_files=YES Quit the editor and restart the vsftp server.
s u d os e r v i c ev s f t p dr e s t a r t
We suggest NOT following the additional steps in some guides for messing (post install) with pi account line in passwd file etc. We trashed an installation doing this. You should be able to just sudo apt-get the vsftp server and reboot as we did second time round. Make the minor fixes to vsftp configuration file described above. But we suggest to do these edits and nothing else.
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 10/12
12/26/13
On your laptop/pc, you need an ftp client . We suggest the freely available filezilla ftp client (https://filezillaproject.org) for transferring files across by ftp. ftp://192.168.1.1 and Login to the Pi as user pi. Save the configuration to access the Pi to facilitate future access. Web files Set webroot file privileges
s u d oc h o w nRp i/ v a r / w w w
Now a set of website files can be copied from the PC/laptop with filezilla to this folder and accessed from the laptop/pc webbrowser thus: http://192.168.1.100 Disk capacity free Heres how you can establish how much disk space is left over. After all of the above we had used about 1.8Gb on the 4Gb SDHC card.
d f h
s u d os h u t d o w n hn o w
s u d os h u t d o w n rn o w
Epilogue The Raspberry Pi is an excellent training tool for LAMP installations and can even be serviceable for light
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/ 11/12
12/26/13
operational tasks. It is an educational tool though and a really great way to learn Linux, Apache, PHP, MySQL etc One great advantage is the SDHC card filesystem. You can keep several installations and configurations on different cards and, as long as machine is correctly powered down , swap cards and reboot as required. Backing up these SDHC cards is not easy and not always predictable. Tools like the win32diskimager and flashnul-1rc1 can image a card to a .img file, but the file may a. not fit back onto a formatted card of same capacity, and b. not work anyway after reboot. Googling shows there are a lot of approaches taken which is best though isnt clear! Have fun!
www.geothread.net/building-a-lamp-server-on-the-raspberry-pi-computer/
12/12