Search This Blog

Getting the most out of your Raspberry Pi with colocation


When I wrote about routing web traffic to a Raspberry Pi, I mentioned that most residential ISPs block port 80 -- which is the common web traffic port. Well, there's a great way to get around ISP troubles: Colocating your Raspberry Pi. And pcextreme has you covered with Raspberry Pi Hosting.
Raspberry Pi Hosting is a colocation service where you send in your Raspberry Pi and they house it with many other Pi's while giving proper care and feeding -- 500 GB of bandwidth and power, that is. You get a 100 Megabit upload connection, a free power supply and easy reboots via pcextreme's control panel. And you don't have to worry about your home connection going down or arbitrary port blocking getting in your way when doing some serious business with your Raspberry Pi.
All you need to get started is a Raspberry Pi, a case for it, and a configured OS installed. They say they already have 1,392 Pi's online and another 725 on the way. Apparently, they were previously herding Raspberry Pi's for free, but had to start charging due to demand. The current cost to colocate a Raspberry Pi is €36.00, or about $48 USD, per year. It's hard to find a reliable VPS service for that price. So, what are you waiting for? Ship that Pi out and put it to some good use!

Raspberry Pi web server: nginx and php


I covered setting up nginx on a Pi - Setting up nginx on a Raspberry Pi. That will get you ready to serve static html webpages from your Raspberry Pi. However, you might be interested in doing to some server-side (or Pi-side) programming to create dynamic websites that run on your Pi. PHP is a great language to get started in this area. To start using PHP on a Raspberry Pi, you will need to install PHP FPM. Open a terminal on your Pi, or SSH to it and enter this command:
sudo apt-get install php5-fpm
That will install the program necessary to use PHP and nginx togehther on the Pi. Next, we want to edit our nginx configuration to interpret PHP files when they are requested. Add a server block to /etc/nginx/sites-enabled/default that looks like this:
server {
    listen 80;
    server_name yoursite.com;
    root /usr/share/nginx/www/yoursite.com;
    index index.php;
    location ~ \.php$ {
        fastcgi_pass    unix:/var/run/php5-fpm.sock;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}
You might also need to change php5-fpm to listen on a unix socket rather than a TCP socket. Make sure that /etc/php5/fpm/pool.d/www.conf has this line in it:
listen = /var/run/php5-fpm.sock
After changing the nginx configuration, reload it with this command:
sudo nginx -s reload
and start php5-fpm with this command:
sudo service php5-fpm start
Now when you create a php file it will be executed when you visit the site hosted on your Raspberry Pi.

Raspberry Pi Sales Hit 2 Million


I wrote about the first million Raspberry Pi's built in the UK, and now the Raspberry Pi Foundation has announced that 2 million Pi's have been sold since they started. They expected the sales to hit this mark in January of next year, but it's Christmas in November and they were pleasantly surprised:
"So it was a bit of a shock at the end of last week when we got the latest sales figures and discovered that the 2,000,000th Raspberry Pi was sold in the last week of October. We don’t know who owns it – if you bought one between October 24 and October 31st, it might be yours."
Source: TWO MILLION!

Routing Web Traffic to a Raspberry Pi


If you want people to reach your website hosted on a Raspberry Pi, you will need to route incoming web traffic to the Pi. This means you'll need to change some settings on your home router, mainly the firewall settings. If you haven't installed a web server on your Raspberry Pi, that might be a good place to start: Setting up an nginx web server on a Raspberry Pi.
First, access your router's settings by entering 192.168.1.1 in your browser's address bar. That is the default address for the web interface of most home routers. When you go there, you should see something like this, with the maker of your router's name on it:

Next, find the firewall settings, which are under Firewall > Virtual Servers for my router. You will probably have to enter your router's administrative password at this point. This keeps other people from making changes to your router.

Next, we want to change it so that HTTP web traffic is routed to the Raspberry Pi. You'll need to know your Pi's IP address, which can be found by entering this command in the Pi's terminal:

ifconfig
You're looking for something like this:
inet addr:192.168.1.33
And the IP address is 192.168.1.33 for my Raspberry Pi. Remember that address, because you will be putting it in your router's settings.
The Raspberry Pi's IP address needs to go in the box called something like "Private IP Address". The first record I have entered here is the one for my Pi:


These fields tell us that the virtual server is enabled for port 80 traffic inbound to my home's IP address, and will be routed to port 80 on my Raspberry Pi. You can now check to see if web traffic is getting to your Raspberry Pi by visiting your home's IP address in your browser. To get your home IP address, look for a "Wan IP" in your router's settings or go to Google and enter "my ip". Once you get that address, put it in your browser and see if your Pi serves a webpage. Note that you'll need a web server set up on your Pi for this to work.
Another thing to remember is that many residential internet service providers (ISPs) block the default HTTP port, which is 80. I use Cox Communications for my ISP, and they do not allow servers to listen and serve traffic on port 80. It's even trickier because a Raspberry Pi set up to listen on port 80 will work from within your home when you are connected to the same router, but will not work outside your local network. This fooled me into thinking I was listening on port 80 with my Pi for any traffic coming my way.
To get around an ISP that doesn't allow port 80 traffic, you can host websites from your Raspberry Pi on an alternative port. I also have set my Pi up to receive requests on port 83, which is not blocked by my ISP. Here is my record for port 83 in my router's firewall settings:


That tells my router to send port 83 traffic to port 80 on my Raspberry Pi. The Pi's web server configuration doesn't need changed, but now it will respond to traffic on port 80 and port 83.

Connecting to a Raspberry Pi with VNC


Although you can use a Raspberry Pi's desktop using it's HDMI port and a monitor, you can also use VNC. To do this, you will need to install tightvncserver on your Pi, with this command:
sudo apt-get install tightvncserver
Next, you need to create a desktop to connect to on the Pi:
tightvncserver -geometry 1200x800 -depth 24 :1
This will create a new desktop on port 5901. You can connect to it with another computer using VNC viewing software. You just have to enter the Pi's IP address and the port. Something like 192.168.1.34:5901. You can find your Pi's IP address by using the ifconfigcommand on the Pi and looking for inet addr:
inet addr:192.168.1.34

Backing up a Raspberry Pi's Operating System


The Raspberry Pi is a lot of fun to tinker with. However, you may not want to spend a bunch of time reinstalling the Pi's OS (Raspbian or others) when something goes wrong or you want to start over fresh. Here's how you can create a backup of your Pi's operating system so you can restore it quickly when you need to do so.
Creating a Pi Backup with Linux
You'll need another computer to create the backup of the Pi OS. I will show you how to do this with a Linux machine, since that is what I use. First, shutdown the Pi (sudo shutdown -h now or use the Pi's GUI). Next, remove the SD card or USB drive that has the Pi's OS installed on it. Now plug it into the machine that you are using to create the backup. Open a terminal on that machine and type
dmesg | tail
It will output something like this:
[12519.406486] mmc0: new high speed SDHC card...
[12519.412045] mmcblk0: mmc0:a7e2 SD04G 3.75 GiB 
[12519.412790]  mmcblk0: p1
Use this command to create a backup of the card:
sudo dd if=/dev/mmcblk0 of=backup.iso bs=4M
Notice that I'm using the device name from the output of dmesg, mmcblk0. This will create a file called backup.iso that is a backup of your entire SD card, with the Raspberry Pi's OS installed on it. Note that you will need at least enough free space to hold the entire card on the machine you're using, which would be 4 GB for my card. This may take a while to complete depending on the size of your Raspberry Pi's SD card.
Restoring the Pi's Backup
Whenever you want to restore the Raspberry Pi to the state it was in when you created the backup, use this command after inserting the SD card into the other machine:
sudo dd if=backup.iso of=/dev/mmcblk0 bs=4M
This will restore the backup.iso copy of your Pi's SD card back onto the card. Then you can re-insert the card into the Raspberry Pi and boot it.

Change a Raspberry Pi's Hostname without Rebooting

To change a Raspberry Pi's hostname without rebooting, follow these steps. In this example, I will be changing the hostname from "alpha" to "beta".
1. Edit /etc/hostname
The hostname file is just a single line with your current hostname on it. Edit the hostname file with this command
sudo nano /etc/hostname
and change the line in the file from the current name to the one you want. I would change it from alpha to beta in this instance.
2. Edit /etc/hosts
The hosts file tells your Pi how to resolve any addresses that you place there. Mine looks like this:
127.0.0.1       localhost
::1             localhost [...]
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       alpha
and I would change it to look like this:
127.0.0.1       localhost
::1             localhost [...]
fe00::0         ip6-localnet
ff00::0         ip6-mcastprefix
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       beta
3. Update hostname with hostname.sh scriptNext, execute this command:
sudo /etc/init.d/hostname.sh start
This will refresh the hostname for the Raspberry Pi without rebooting it. Now, if you open a new terminal, the prompt will say username@newhostname:~$ and your hostname is changed.

Deleting a User from a Raspberry Pi




To delete a user's account from a Raspberry Pi, use the following command:
sudo userdel bob
and replace the username "bob" with whatever the username is of the account that you want to delete. When the user account was added to the Pi, a home directory for that user was most likely added, as well. To remove the home directory of the user, use this command:
sudo rm -rf /home/bob
and replace "bob" with the username, once again. After using userdel and removing the home directory, the user account should be completely removed from your Raspberry Pi.
If you would like to add another user to the Pi, check out this post that details the process:Adding a User to a Raspberry Pi.

Adding a User on a Raspberry Pi

When you first boot up and configure a Raspberry Pi, it sets up a Linux user account for you. You might want to add additional users for other people that will log into your Pi or for various other reasons. Here's the command to do that.
sudo adduser bob
This command starts the utility to add a new user with a name of "bob", which you would substitute with what you want the username to be. Your Pi will tell you a bit about what it does while creating the user:
Adding user `bob' ...
Adding new group `bob' (1003) ...
Adding new user `bob' (1002) with group `bob' ...
Creating home directory `/home/bob' ...
Copying files from `/etc/skel' ...
Then it will ask you for the password for the new user:
Enter new UNIX password:
Retype new UNIX password:
Simply type the password that you want the new user to have. Next, you'll be asked for note information about the new user for the Raspberry Pi:
passwd: password updated successfully
Changing the user information for bob
Enter the new value, or press ENTER for the default
    Full Name []: Bob Redfield
    Room Number []: 23
    Work Phone []: 501-234-8967
    Home Phone []: 501-678-9876           
    Other []: 
Is the information correct? [Y/n]
You can leave all of these blank or fill them in depending on your needs. Once this is complete, you can log in with the new user by typing
sudo su bob
Where you replace "bob" with that of the new user. You can also log out of your user and log back in as the new one.
This will create a home directory for the new user, as well, at /home/bob -- with the actual bob of course.
This is how you create a new user on a Raspberry Pi, but it will also work on other GNU/Linux machines that run Debian, Ubuntu and many other distributions.
Here's how to delete a user, as well: Deleting a User from a Raspberry Pi.