Getting Started with a Web Server on the Pi
Now that students are making fine online Smart Pascal programs, a school might want to show off the creations on a local network to inspire others to emulate them. On this page James Farnsworth provides guidance on setting up a web server on a Raspberry Pi. (This was first published on CodeShow). On the next page we show you how to use SSH to control the Pi from another computer on the network and how to upload files to the web server using a Pascal application.
The Raspberry Pi is a brilliant computer for mini projects. Its low power draw and low cost make it practical for these uses, but it is powered by an ARM chip, the same sort that you might find in a basic smartphone, giving it plenty of power to run web servers, scripts and such the like. I will show you how to do a few interesting things with it over a network.
First of all, you need to set up the Raspberry Pi. If you already have done the initial set up of the Raspberry Pi, skip most of the next section - then run sudo raspi-config, and enable SSH as shown at the end of the section.
Plug the Raspberry Pi into a micro USB mains adaptor (some phone chargers with micro USB connections will work, but for best compatibility I recommend one made for the Raspberry Pi). Then plug the Raspberry Pi into an ethernet connection, a keyboard and into a monitor. Certain video output adaptors will work, but often need a degree of tweaking not covered here. Turn the power on and wait for the Pi to boot. You will see a screen like this:
Select expand file system and confirm. Change the password for the user 'pi' if you want to - it defaults to 'raspberry'. Then select 'Advanced Options', and then select 'SSH' and enable it. From the 'Advanced Options' menu, you can also change the hostname - the name of the Pi on the network. Use the left and right arrow keys to select finish, and then reboot.
SSH stands for Secure Shell. It is a way of getting into a command line on the Pi remotely. A brilliant program for connecting to the Pi via SSH on a Windows machine is PuTTY, but if you are on a Mac or Linux machine, you can just open a terminal and type 'ssh pi@ip-address-of-pi' to SSH into the Pi. On PuTTY, just launch it and type in the IP address of the Raspberry Pi. You will likely find yourself using SSH later on in the tutorial.
Before continuing, it is a good idea to set up a static IP address for the Raspberry Pi, as it can be annoying for it to change from time to time. I suggest following the instructions on this website.
To install a web server on the Pi, login (via SSH or otherwise - if you are using SSH you can disconnect everything except power and network) and type into the terminal 'sudo apt-get install apache2 php5 php5-mysql mysql-server'. This might take a while. It will ask you to specify a password for the MySQL root user. I suggest you do so.
Once that is done, you have a web server set up on your Pi. This will be accessible to computers inside the same network, but you could potentially set your network router up to allow access from outside the network. Point a web browser at the Raspberry Pi's IP address (the command ifconfig will show you this under the entry for eth0), and you should see a screen showing that the web server works.
All the data that will appear on the website is stored in /var/www. You are now ready to put content on your website. Keep in mind that when a browser visits your Pi's IP address or a folder inside of /var/www (accessed by adding /name-of-folder to the end of the IP address), it will look for a file called index.html. Also, during this and subsequent parts of the tutorial, you may run into issues with file permissions that might stop things from working, often without you knowing. This means you may have to change what access permissions are given for a certain file, or who the owner of the file is, and the user group that owns the file (most users have a group with the same name as their username, which they are a member of). The user that represents access from the web is called www-data. To change permissions of files, I found this tutorial useful. Most of what applies to file permissions applies to directory permissions as well.