Time has come to experiment with Raspberry Pi which was getting a bit dusty.
In this post I will describe step by step how to set up Raspberry Pi as a web server. Let’s start with the items which are required to proceed further:
- Raspberry Pi, preferably model B,
- Power adapter such as a mobile charger,
- WiFi USB dongle,
- WiFi router.
It is assumed that you have already installed operating system and it is working correctly. For the purpose of this article Raspbian has been installed.
Setting up WiFi
Firstly, let’s start with setting up a wireless connection. For this the WiFi USB dongle is used. Connect it to the one of free USB ports. One other thing which is required is a piece of software which makes things easier, wicd. For this type in terminal:
sudo apt-get install wicd-curses
After installation you can launch it by typing:
This application allows you to set which wireless access point to use. While configuring your network do not forget to set up a static IP address. It will definitely come in handy. Other option is to configure the router by assigning a static IP address to a specific MAC address.
At this stage the RPi is capable of connecting to your local network. Now, it is time to make it visible from the outside. Unless, your ISP delivers you with a global IP address you have to follow a few steps. But before this you should do some additional configuration of your router.
You need to set up Port Forwarding. It allows you to redirect connections from outside to your local network. Let’s say that the RPi has an IP address 192.168.0.2. What you need to do is to forward ports 21 (FTP), 22 (SSH), 80 (HTTP). By doing so you will be able to connect to your RPi via ssh, view webpages and connect via FTP.
Port forwarding can be done by configuring the router to which Raspberry Pi is connected. If you have two routers the situation is still the same. However, the first router which has the access to the Internet has to have the port forwarding configured for the second router (a sub network). Finally, at the second router you have to configure port forwarding for RPi.
Now, RPi can be reached from outside. However, as I mentioned before you need to make it a permanent situation. In this case Dynamic DNS (DDNS) comes in handy. If you already have a domain you can use it to link it to RPi. This means to link your temporary global IP to the domain. You can use a piece of software which finds out your global IP address and sends it to the DNS where it can replace the old IP with the current one. Most of the time a Bash or a Python script is delivered by the domain provider. In some cases it may require some alterations like detecting the global IP address. For this you can use a simple command:
curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
The above commands retrieves your IP from checkip.dyndns.org and process the output to just leave the IP address.
Let Cron handle it!
The second step is to make the script periodical. Using
You can add following line
*/10 * * * * /path/to/the/script
It tells cron to run the script every 10 minutes.
Setting up web services
Ok, now, you have Raspberry Pi set up for installing web services. There is a few packages which you need to install in order to change your Raspberry Pi into a web server. Let’s then start. Run this command:
sudo apt-get install apache2 php5 libapache2-mod-php5 \
mysql-server mysql-client php5-mysql \
Above will install Apache 2 with PHP 5 support. The second line installs MySQL database while the last line installs FTP server. During the installation you will be prompted for some additional informations like password for MySQL database.
After the installation which may take a few moments it is time to configure the newly installed packages. Let’s start with PHP.
And edit php5.conf file.
sudo vim php5.conf
At the bottom add line:
php_admin_flag display_errors on
This will enable error reporting. It is useful when you try out some PHP code. While still in Apache configuration directory run:
mv mods-available/userdir.* mods-enabled/
It will enable any user to host content inside public_html directory which is located in user’s home directory. There is no such a directory by default thus you have to create it. When everything is set up you can restart Apache demon with
sudo service apache2 restart
Finally, now it is time to configure FTP service.
Run this command:
sudo chown -R pi /var/www
Above will grand access to pi user to the /var/www directory.
Now you are all set up to use Raspberry Pi as a Web Server.