Creating a Web Server on a Raspberry Pi
My Raspberry Pi
This web site is powered by a Raspberry Pi, a single-board computer the size of a credit-card.
The Raspberry Pi comes already-assembled, with all parts, for about $35 or $40. It comes in two models; mine is
a Model B Rev 2, which has more internal memory than the slightly-cheaper Model A, and two USB ports instead of 1,
and has an Ethernet controller, which I'd think is essential for a web server.
It runs several operating systems including Linux, which I selected for my web site. It uses an SD card for
a hard drive, and requires an external power supply that has a micro-USB connector and can supply about 1A of power.
I've read that it's good to get a power supply that can deliver 1.5 or 2 amps to improve the stability of the Raspberry Pi.
This is more than your typical phone charger delivers, but check your junk drawer because you might already have one.
I purchased the Raspberry Pi, a Transcend 8GB class 10 SDHC memory card, a Multicomp Clear Raspberry Pi Enclosure,
and an EasyAcc US 5V 2A Universal Travel AC Micro USB Wall Charger Adapter long cord for Tablet,
all for a total of $73.84 from Amazon.
I already had a spare Network cable to connect the Raspberry Pi to my home network.
I also already had an SD card writer that I could connect-up to my computer to burn the initial version of the operating system.
If you don't have an SD card writer, it's probably cheaper to buy an SD card that's pre-loaded with the operating system.
It's reasonably priced, but I don't know the speed of the card. Since I already had an SD card writer,
and I wanted a card that was faster than what the Raspberry Pi wants as a minimum speed, I decided to just buy a
blank class 10 card and burn it myself.
Additionally, for initial setup of the computer which took me about 4 hours, I needed to use these things I already had:
a USB Keyboard, a USB Mouse, an HDMI cable, and a monitor (or TV) with an HDMI input.
To setup the Raspberry Pi, using a Windows 7 computer follow these steps:
- Download a raw image for Raspbian Wheezy from http://www.raspberrypi.org/downloads
- Download Win32DiskImager from http://sourceforge.net/projects/win32diskimager/
- Run Win32DiskImager on your PC and burn the Raspbian Wheezy image to the SD card
- Plug the SD card into the Raspberry Pi
- Connect the USB keyboard and mouse, monitor, and network cable to the Raspberry Pi
- Connect the other end of the network cable into your router
- Connect the power supply to the Raspberry Pi and plug it into the wall. This should boot up the Raspberry Pi.
- The default user on the Raspberry Pi is "pi", and the default password, in case you ever need it, is "raspberry", both without the quotes.
- Once it boots up, the Raspberry Pi will run a text-based Raspberry Pi Software Configuration Tool (raspi-config). Here you should do the following things:
- Navigate to Expand Filesystem and hit enter.
- Navigate to Change User Password and hit enter. Select a good password so hackers can't harm you. We'll refer to this password in subsequent steps as [password]. When you see that, type in the password you used here (don't type in any brackets, unless your password really contains them)
- Navigate to Internationalisation Options and hit enter. Select the Locale and Keyboard Layout that makes you happy. Use the right-arrow key to navigate to <Back> and hit enter to return to the main menu
- Navigate to Enable Boot to Desktop/Scratch and changet to "Desktop Log in as user 'pi' at the graphical desktop" (assuming you want the Raspberry Pi to automatically login when you lose and regain power, or when you reboot it)
- Navigate to Advanced Options and hit enter.
- Set the Hostname to the name you'd like your Raspberry Pi to have on your local network
- Change the Memory Split to 16 to use as little memory as possible for graphics and leave it available for your web server
- Enable the SSH server if you'd like to administer the Raspberry Pi from your PC rather than leaving the keyboard, mouse, and monitor connected forever.
- Use the right-arrow key to navigate to <Finish> on the main menu and hit enter to exit the raspi-config tool.
- Reboot the device
- Much of the configuration on Linux is done using a command prompt, which you open by running LXTerminal on the Raspberry Pi, or after we've gone several steps down, by running an SSH Client on your PC.
- Get Debian up-to-date with these commands:
sudo dpkg-reconfigure tzdata
sudo apt-get update
sudo apt-get upgrade
- Install git to allow a firmware update with this command:
sudo apt-get install git-core
- Download the firmware update and run it, with these commands:
sudo wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
- The end of the update will tell you that you need to reboot. If you don't see any errors, reboot with this command:
sudo shutdown -r now
- You probably want to run the Raspberry Pi without a keyboard, a mouse, or a monitor. If so, you've presumably already enabled the SSH server, so you just need to install an SSH client on your PC. The steps are:
- If you didn't enable the SSH server and now need to, run raspi-config again:
- Install PuTTY on your PC, from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
- Run PuTTY on your PC, enter the Raspberry Pi's Hostname or IP address into the Host Name or IP Address, click Open, then enter a username of pi, and a password of [YourPassword]
- You should be able to run commands on the Raspberry Pi from your PC, just as if you were typing them into your Raspberry Pi's command prompt. Try the help command to be sure all is working.
- If you're able to connect and enter commands, you can disconnect the keyboard, mouse and monitor since you can now administer your Raspberry Pi from your PC.
- I also installed the free ServerAuditor on my iPad to SSH into the Raspberry Pi console from my iPad. It works the same as PuTTY on my PC for those rare occasions when I really really really need to change some configuration setting, but I can't manage to get off the couch.
- I thought I'd also want to be able to use the Linux graphical UI on the Raspberry Pi from my PC, so I performed these steps (but would not do so again since I've never found the need to run the graphical UI):
- Install xrdp on the Raspberry Pi with this command:
sudo apt-get install xrdp
- Run "Remote Desktop Connection" on your PC, leaving module set to sesman-Xvnc, Username=pi, password=[YourPassword]
- Install Apache2 and PHP5 with this command:
sudo apt-get install apache2 php5 libapache2-mod-php5
- If you'd like, install MySQL with this command (I didn't need SQL, so I didn't):
sudo apt-get install mysql-server mysql-client php5-mysql
- Make changes needed to Apache Configuration, and restart it if you made any changes
sudo nano /etc/apache2/apache2.conf
sudo service apache2 restart
- Make changes needed to PHP configuration
sudo nano /etc/php5/apache2/php.ini
- I want to develop and test my web site on my PC, but run it from my Raspberry Pi. The easiest way to sync updated web content from my PC to my Raspberry Pi is using some syncing software (I use GoodSync) over a file-sharing protocol, which you need to install on the Raspberry Pi.
- On the Raspberry Pi, install Samba to permit file-sharing:
sudo apt-get install samba
sudo apt-get install samba-common-bin
- Change the Samba configuration:
Those 9 lines will make a www share on the network within your Raspberry Pi. It points to the Apache home folder. Files uploaded here will be accessable through the web server.
sudo nano /etc/samba/smb.conf
set workgroup to the name of your network workgroup / NT domain
set realm to the name of your Raspberry Pi
set netbios name to the name of your Raspberry Pi
at the bottom of the file, paste these 9 lines:
comment = All users
path = /var/www
valid users = @users
force group = users
create mask = 775
directory mask 771
writable = yes
read only = no
- At this point, I was able to view web content from the Raspberry Pi over my local network, administer my Raspberry Pi from my PC, and update the web content on the Raspberry Pi from my PC by just copying files into the www share and any subfolders I made within it.
- To let the rest of the world access my web site I had to open up port 80 on my router and have it directed to my Raspberry Pi. On my router (yours is probably different) I needed to have the same local network IP address assigned to the Raspberry Pi each time it booted, then use Port Forwarding to have port 80 forwarded to that IP address.
- At this point, your friend should be able to type in your WAN IP address into a browser and see your web content.
- To be able to access your web content using a name rather than an IP address, you can use a paid solution which can give you a friendly name like JeffBondono.com or a free solution which gives you a slightly-ugly name like bondono.ignorelist.com
- For the paid solution, head over to NameCheap.com and buy a domain name (I paid $10.87 for the first year). In the settings pages for your new domain, go to the All Host Records page. Remove the A record they've setup for your www host name, and set one up, directing it to your WAN IP address. Specifically, you should follow directions like this:
- Login to your dashboard on NameCheap.com
- Under "More links" on the right side of the screen, click "Your Domains / Products"
- Tick the checkbox beside your domain, then click the "Edit Selected" button
- On the left side, under "Host Management" click "All Host Records"
- There should be an entry with hostname @ already. Change the "IP Address" to "http://yourDomain.com" and the "Record Type" to "URL Redirect". Note that IP address should not have www in it. If there isn't an @ entry already, you should be able to type one in to add it.
- Next edit or add the entry with hostname "www". Set the "IP Address" to your web server's public IP address (this is the thing your ddclient will change dynaically). Set the "Record Type" to "A (address)".
- Click on "save changes" at the bottom.
- If some of this is unclear, maybe it will be easier to understand at their help page
- For the free solution, head over to freedns.afraid.org and pick out a name (bondono, in my case) and a domain (ignorelist.com, in my case), and sign up. Have this forwarded to your WAN IP address.
- Of course, for either of these, there are numerous other providers. Just search for Domain Name Registration, go buy a domain name, or Dynamic DNS, to redirect a name to your WAN IP address.
- After setting up one of these solutions, verify that your page works by going to your named web site in your browser. If this takes you to your web site, then so far so good.
- The remaining problem is that your ISP probably assigns you a different WAN IP address from time to time, so the redirection from name to IP address will someday soon direct people from your name to someone else's computer.
- To fix this problem, you need your router or your Raspberry Pi to tell either afraid.org or NameCheap.com it's WAN IP address periodically. If your router supports Dynamic DNS, configure it to do so. If you need your Raspberry Pi to do it, use DDClient, which you can set up on your Raspberry Pi like this:
- Install using these commands
sudo apt-get update
sudo apt-get install ddclient
- During installation, a wizard will open. Answer whatever you like during this installation, we'll be modifying it all using a text editor anyway
- Edit your ddclient.conf using the command:
sudo nano /etc/ddclient.conf
- For NameCheap, my /etc/ddclient.conf looks like this:
login=xxxxxxxx (where you should replace xxxxxxxx with your domain name, like jeffbondono.com)
password=xxxxxxxx (where you should replace xxxxxxxx with your NameCheap dyndns password)
- For afraid.org, your /etc/ddclient.conf should look something like this:
login=xxxxxxxx (where you should replace xxxxxxxx with your domain name, like bondono.ignorelist.com)
password=xxxxxxxx (where you should replace xxxxxxxx with your password at afraid.org)
xxxxxxxx (where you should replace xxxxxxxx with your domain name, like bondono.ignorelist.com)
- For NameCheap.org, you might want to go back to that All Host Records page and direct your www host to 192.168.0.1, then run DDClient and be sure it changes the record back to your WAN IP address.
[Home] copyright (c) 2012-2017 by Jeff Bondono
Please report bugs or send comments to Jeff Bondono