Raspberry Pi Pico Tips and Tricks

Tuesday 13 January 2015

Setting up a web server with PHP on a Raspberry Pi

The following post is a section of the book 'Raspberry Pi: Measure, Record, Explore'.  The entire book can be downloaded in pdf format for free from Leanpub or you can read it online here.
Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
---------------------------------------

Web Server and PHP on our Pi

Because we want to be able to explore the data we will be collecting, we need to set up a web server that will present web pages to other computers that will be browsing within the network (remembering that this is not intended to be connected to the internet, just inside your home network). At the same time as setting up a web server on the Pi we will install PHP.
At the command line run the following command;
sudo apt-get install apache2 php5 libapache2-mod-php5
We’re familiar with apt-get already, but this time we’re including more than one package in the installation process. Specifically we’re including apache2, php5 and libapache2-mod-php5.
‘apache2’ is the name of the web server and php5, libapache2-mod-php5 are for PHP.
The Raspberry Pi will advise you of the range of additional packages that will be installed at the same time (to support those we’re installing (these additional packages are dependencies)). Agree to continue and the installation will proceed. This should take a few minutes or more (depending on the speed of your Internet connection).
Once complete we need to restart our web server with the following command;
sudo service apache2 restart
We can now test our web server from the windows desktop machine.
Open up a web browser and type in the IP address of the Raspberry Pi into the URL bar at the top. Hopefully we will see…
Testing the web server
We could also test the web server from the Pi itself. From the Raspberry Pi’s desktop start the Epiphany Web Browser and enter either 10.1.1.8 (or 127.0.1.1 which is the address that the Pi can ‘see’ internally (called the ‘localhost’ address)) into the URL bar at the top. We should see the following…
Testing the web server
Congratulations! We have a web server.

Tweak permissions for easier web file editing

The default installation of the Apache web server has the location of the files that make up our web server owned by the ‘root’ user. This means that if we want to edit them we need to do so with the permissions of the root user. This can be easily achieved by typing something like sudo nano /var/www/index.html, but if we want to be able to use an editor from our GUI desktop, we will need the permissions to be set to allow the ‘pi’ user to edit them without having to invoke sudo.
We’re going to do this by making a separate group (‘www-data’ will be its name) the owners of the /var/wwwdirectory (where our web files will live) then we will add the ‘pi’ user to the ‘www-data’ group.
We start by making the ‘www-data’ group and user the owner of the /var/www directory with the following command;
sudo chown www-data:www-data /var/www
(For more information on the chown command check out the Glossary)
Then we allow the ‘www-data’ group permission to write to the directory;
sudo chmod 775 /var/www
(For more information on the chmod command check out the Glossary)
Then we add the ‘pi’ user to the ‘www-data’group;
sudo usermod -a -G www-data pi
(For more information on the usermod command check out the Glossary)
This change in permissions are best enacted by rebooting the Raspberry Pi;
sudo reboot
Now the ‘pi’ user has the ability to edit files in the /var/www directory without problem.

The post above (and heaps of other stuff) is in the book 'Raspberry Pi: Measure, Record, Explore' that can be downloaded for free (or donate if you really want to :-)).

No comments:

Post a Comment