The concept of setting up the personal web server on the Raspberry PI really was for a hobby project and shouldn’t really be used for hosting an external website. There are various reasons for this such as security, the power of the Raspberry PI, bandwidth and the fact that your external IP address will change at regular intervals.
This article goes some way to addressing some of the things you will need to do to connect to the website on your Raspberry PI from the outside world.
This is a proof of concept and just ideas on paper (or screen) and should not be seen as a gospel guide.
I will warn you now that I have never been that good with the networking side of things so I will try and explain as best I can. I may or may not be wrong in places and I am relying on people to correct me in the comments section.
Get your External IP address
There are basically 2 IP addresses that are important. There is the internal IP address which you can get by running IFCONFIG in a terminal.
When you connect to the internet you will actually be using an external IP address.
I installed lynx first by running the following in the command line:
sudo apt-get install lynx
Then I ran the following command to get the external IP address.
lynx –dump http://ipecho.net/plain
Another way to get your external IP address is to log onto http://www.whatismyip.com/
but I will explain later why I bothered to install lynx.
Now this is the part that I can’t really explain in much detail because it depends on the ISP and the router that you are using as to how it is done.
I am using 3 Mobile Broadband and to forward ports using this service you have to open a web browser and navigate to http://192.168.1.1.
This takes you to the home page of the router. There is a login section where you can then access more intricate settings for the router.
On 3 Mobile Broadband there is a settings tab and then under the security heading there is a section called virtual servers.
This is where the port forwarding takes place.
All I had to do was add a new row into the grid and enter the following details:
- wan port
- lan ip address
- lan port
The name is just a description so that I can remember what the purpose of the entry is in the list. The wan port is the port from the outside world and so for the web server I entered 80 and for ssh I set this to 22
The lan ip address is the address to the Raspberry PI (run IFCONFIG to get this).
The lan port is the port to forward to on the Raspberry PI. For a web page I set this to 80. For ssh I set this to 22.
The protocol is either TCP, UDP or ALL.
The status can be set to on or off and obviously I set this to on.
Pressing Apply applied the changes.
Testing it out
It is not possible to test this on the same network that the Raspberry PI is on. So if you are using the same broadband connection as the PI then you can’t test connecting from the internet to the Raspberry PI as you would essentially be accessing the Raspberry PI from inside the network.
You will therefore need to test it from something not connected using the same network (broadband connection). If you have a 3G smart phone then you can use the browser on the phone to try to connect to your external ip address, alternatively pop round to the local library or pop to your nearest electronics store and buy a cheap 3G dongle.
If it worked correctly then you will see the web page appear.
What happens if you cannot connect to the Raspberry PI?
First of all it is a good idea to see which ports are available from the outside world.
You can enter the external IP address and ports to check and you will be told whether they are visible from the outside world.
If the port is not open or visible then you have set up the port forwarding incorrectly.
Static IP address
To prevent everyone setting themselves up as web hosts, ISPs give each of their customers a dynamic IP address.
This means that the external IP address you will use today to connect to your site will be different tomorrow or even in just a few hours time.
If you are determined to use your Raspberry PI as a web server (not recommended) then there is a service at http://dyn.com/dns/
which enables you to set up an address that will remain static so you can always access the websites and ssh on your Raspberry PI.
Remote access to the Raspberry PI
If however you just want to be able to access your Raspberry PI from say your work then you won’t want to pay money to access your Raspberry PI.
I use the following method to continuously be able to access my Raspberry PI when I need to.
To install SSMTP I ran the following command:
sudo apt-get install ssmtp
Next I edited the ssmtp.conf file by running the following command:
sudo nano /etc/ssmtp/ssmtp.conf
I then added the following to the end of the file:
I pressed CTRL O to save the file and CTRL X to exit nano.
Next I edited the revaliases file by typing the following command.
sudo nano /etc/ssmtp/revaliases
Within the file I added the following line
I then saved the file by pressing CTRL O and exited the file by pressing CTRL and X
Finally I set the permissions to 774 by running the following command:
sudo chmod 774 /etc/ssmtp/ssmtp.conf
All of this basically enables me to send an email.
To send an email all I have to do is run a command and output it to the mail command. For example:
lynx –dump http://ipecho.net/plain | mail -s “Your PI IP” firstname.lastname@example.org
This emails my IP address to my email address
Obviously this only does it once and so it would be better if it sent me an email at regular intervals so that I can see if there has been a change.
To do this I added the above command to a script called sendmemyIP.sh. I opened the script by typing the following:
sudo nano sendmemyIP.sh
I then copied the lynx command into the script and pressed CTRL O to save and CTRL X to exit nano.
To get the script to run regularly I added it as a cron job by copying sendmemyIP.sh to /etc/cron.hourly.
sudo cp sendmemyIP.sh /etc/cron.hourly/sendmemyIP.sh
Now when my Raspberry PI is turned on it will send the IP address to my email inbox once an hour.
I can then use this IP address within putty or ssh to access my Raspberry PI from anywhere.
This was all done as a proof of concept and I’m looking to the community to tell me if there are any glaring no-nos with the approach I have taken.
It certainly works for me and hopefully it will help some other people out there looking to try the same thing.
Thankyou for reading.