Raspberry Pi - Part 14 - Web Server
Raspberry Pi - Part 14 - Web Server
-
Web Server
Version: 2019-11-29
Apache is a popular web server application you can install on the Raspberry Pi to allow it to
serve web pages.
On its own, Apache can serve HTML files over HTTP, and with additional modules can serve
dynamic web pages using scripting languages such as PHP.
Install Apache
First, update the available packages by typing the following command into the Terminal:
cd /var/www/html
ls -al
total 12
drwxr-xr-x 2 root root 4096 Jan 8 01:29 .
drwxr-xr-x 12 root root 4096 Jan 8 01:28 ..
-rw-r--r-- 1 root root 177 Jan 8 01:29 index.html
This shows that by default there is one file in /var/www/html/ called index.html and it is
owned by the root user (as is the enclosing folder). In order to edit the file, you need to
change its ownership to your own username. Change the owner of the file (the default pi user
is assumed here) using
sudo chown pi: index.html.
You can now try editing this file and then refreshing the browser to see the web page change.
sudo rm index.html
Now save and refresh your browser. You should see "hello world". This is not dynamic but still
served by PHP. Try something dynamic:
First run
This also give you the installation order. The first point is Linux, we’ve already running. So we
start the apache installation with the following command:
You need to state the password of the mysql root user during the installation. Don’t forget it,
we will need it later!
And last but not least we install php and php-mysql on our pi:
Note: The php-mysql is only needed if this is required for one of the applications you will run
on your Pi
When your finished installing you can check your installation with browsing to
http://[ip-of-your-pi]
If you installed mysql, you can administer mysql from within the command line, but I prefer
the graphical tool phpmyadmin. To install this type:
During the installation we were asked to select the installed webserver – so choose apache2
and proceed with Ok.
Now we need to remember the password for the mysql root user:
http://[ip-of-your-pi]/phpmyadmin
To easily access your web-content directory for uploading or changing files, we also need to
install a ftp server. I decided to use proftp
To test the installation, I use a ftp client (I prefer filezilla) from my client pc and connect to
[ip-of-your-pi] with user pi and its password.
As you can see the ftp client brings me to my home directory, instead of the www root-
directory. So addition configuration is needed.
# Defaultroot ~
to
Defaultroot /var/www
This sets the default root directory for all users to /var/www, the www root directory.
After connecting via ftp to your pi again your should see this:
But you cannot write or alter anything, because of missing user rights. We have to change this
too. Add the user pi to the group www-data and change the ownership of /var/www to the user
pi.
Now we can write into the www root-directory and so we can start adding files.
Configure apache2
By default, the file index.html will be displayed when you open the website in your browser (as
we saw earlier). But we want support for php-based websites, so we need to change this
default behavior:
Open the config file
and change the order for which apache searches for index pages
The default dir.conf looks like this:
<IfModule mod_dir.c>
DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Change it to
<IfModule mod_dir.c>
DirectoryIndex index.php index.cgi index.pl index.html index.xhtml index.htm
</IfModule>
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
<?php
// show all info availalble
phpinfo();
?>
When you have finished press [Ctrl] + X. This will ask if you want to save the modified files.
Press 'Y' and then hit [Return] to save the file with the same name.
http://[ip-of-your-pi]
You can now use your Raspberry Pi as your personal web server. Be sure it’s not exposed to
the internet, because we didn’t think about security.
Note: if you want to use another directory to store your web files you need to change 1 or 2
config files of apache2
and change
...
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mydir
...
When you have finished press [Ctrl] + X. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
<Directory /home/pi>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
When you have finished press [Ctrl] + X. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
and change
...
ServerAdmin webmaster@localhost
DocumentRoot /home/pi/www
...
When you have finished press [Ctrl] + X. This will ask if you want to save the modified
files. Press 'Y' and then hit [Return] to save the file with the same name.
If not as above it will give you an error when loading the server: Forbidden You don't have
permission to access / on this server
Good to know:
To handle error pages put a file .htaccess named in your root folder of your site with the
following content:
FallbackResource /maintenance.html
This redirects automatically to this page, if the called page is not existing.
To install the SOAP client SUDS module run following commands from a terminal:
When you install a package, apt-get retrieves the needed files from the hosts listed in
/etc/apt/sources.list, stores them in a local repository (/var/cache/apt/archives/), and then
proceeds with installation.
In time the local repository can grow and occupy a lot of disk space. Fortunately, apt-get
provides tools for managing its local repository: apt-get's clean and autoclean methods.
apt clean removes everything except lock files from /var/cache/apt/archives/ and
/var/cache/apt/archives/partial/. Thus, if you need to reinstall a package apt-get should
retrieve it again.
apt autoremove removes only package files that can no longer be downloaded.