Sunday, June 2, 2013

Setting up a Webserver on the Raspberry Pi

Why set up a web server?

If you only remote the Raspberry Pi, you'll want a way to access files, logs, web cam images/video... you can set up a web server and then access these files from a web browser.

How to set it up

If you have your Raspberry Pi set up then just simply run:
sudo apt-get install apache2 php5 libapache2-mod-php5

That will set up everything you need.

File Structure

The location of your files that go to the public are in:
/var/www

You can make directories under /www/* and /or drop files there, and you'll be able to simply point your browser to your IP address of your Pi and you'll see the files. I.e. if you run "motion" you can configure motion to drop all the jpg's and video to a sub folder under /www.


Raspberry Pi Packet Sniffer Install

Packet Sniffing is a method of capturing data flow on a network interface.  This data (packets) can be dumped to a file, or filtered.  You can even do some nifty stuff, like rebuild the data into human readible/audible format... for example, I've used packet sniffers to rebuild the audio stream of a VOIP phone call, for automated testing purposes (testing VOIP quality.)

The most useful packet sniffer I've found is Wireshark.  Wireshark though runs in a GUI, so that limits automation.  However, Wireshark also comes in a command line version called Tshark. 

To install thsark on Raspberry Pi, you simply run:
sudo apt-get install iw tshark

Using tshark:
if you just type sudo tshark it will be capturing data to the screen, on your default network interface (eth0.)

You can output data to a file with the -w parameter.  You can also set durations of capture, as well as other useful filtering methods (i.e. only capture packets from SIP sources.)

Why use Raspberry Pi with Tshark? 
Raspberry Pi is the cheapest linux box I can find.  It's a nice way to run something easily that can be used in a testing framework.  Since I do a lot of VOIP tests, Tshark is very useful, and I can drop a raspberry pi box into the testing for a very low cost. 

I did find some problems though....

When SSH'd to the Raspberry Pi, and running tshark, I was disconnected at times - for what appeared to be too much data coming through and blowing my connection.

Perhaps outputting to a file would resolve that.

Setting Up a Raspberry Pi

I'm new to the whole Raspberry Pi scene.  Figured I'd start a blog to collect my notes on my work with it.

For starters, I bought it on Amazon from this seller:
http://www.amazon.com/Raspberry-Pi-Model-Revision-512MB/dp/B009SQQF9C/ref=sr_1_1?m=A2WTVCOLVN15GP&s=electronics&ie=UTF8&qid=1370166075&sr=1-1&keywords=raspberry+pi

The seller got it to me in 2 days from the order.

I picked up a SD Card at Best Buy (PNY 16GB class 6 HDSD card.)  The reason the type is important, there are some SD cards that do not work with the Pi.  Best to check out the latest details to see what's currently working with the Pi.

I already had HDMI cables, but I needed a power source.  I picked this up on Amazon:
http://www.amazon.com/gp/product/B00828VG3W/ref=oh_details_o01_s00_i00?ie=UTF8&psc=1

 First Things First

Setting up the SD Card is probably the first thing you'll do.  This was a big pitfall for me.  I burned a lot of wasted time on this.

For starters the Official "wheezy" distro for Raspberry Pi is corrupted (as of this post) on the direct download.  I downloaded it several times. the file size was correct, BUT windows said the file was corrupt.  Not believing windows, I used 7zip to unarchive it.  It did... but when I attempted to put the image from the archive, to the SD card, the SD card became corrupt.

Any attempt to boot the Raspberry Pi from that SD card, would result in a error something like:
Failed to mount fs disk due to kbd problem.

Ugh!

I finally caved in and installed a torrent client and downloaded the official Wheezy release from the torrent.  I hate torrents because in the past the clients just brought so much adware to me.  In fact in this case, the first client I attempted to install was spyware.  The second (bit torrent) installed but added app's I didn't want (like registry scans)... once I got the torrent I uninstalled bit torrent.

The torrent downloaded about 10x faster then the direct link.

The torrent also worked.... whereas the direct link was corrupt.

Once I got the image set up on the SD card, I plugged it into the Raspberry Pi, and plugged in my power source.

Configuration

Once you get it working you'll see a Bios like boot menu.  It will have several options.
At the configuration screen, you'll want to:
1. set the thing to use the entire space on the SD card, it's the first option in this boot menu.
2. after that, if you want to boot to a desktop, go to that option, if you use a console only, skip it.
3. To turn on SSH (so you can remote into the box via SSH) you need to go to advanced settings and then choose SSH and then Enable.
4. If this is a public facing device, you might want to change the password as well - it's a option on the boot menu.
5. I also changed the hostname (it defaults to 'raspberrypi')

After that I chose to finish and reboot.

From here on out, you'll log in as your user "pi" with the password you picked... or use the default (raspberry.)  Or if you choose to use the GUI, you'll get a desktop.

SSH

To SSH to this box, first get it's IP by running ifconfig from the command line.  You'll see "eth0" and in there a line inet addr:.  take that address.

From another box, using either terminal on mac, or Putty/supper putty on PC, you can just ssh over to:
ssh [ip]
login as user pi and input your pass.

Installing Software from the Command Line

If you're used to installers in Linux (like Centos' YUM) then you'll get the same thing here, but it's apt-get.  You'll sudo each command like this.  So: sudo apt-get iw tshark would install iw and tshark (for packet sniffing.)

Find the tools you want and they should give you the proper apt-get install commands.

Shutting Down the Raspberry Pi


Also another important thing.  The official site for Raspberry Pi, says to shut it down you just "unplug it" from what I've read this could damage the SD card.  So... you must run an official shutdown:

sudo shutdown -h now
From the UI, you click the red icon and choose shutdown.  Wait for the proper screen statement "system halted," and then remove the power cable.