Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Raspberry Pi - Part 14 - Web Server

This document provides instructions for setting up a web server on a Raspberry Pi using Apache and PHP. It discusses installing Apache and testing the default web page. It also covers changing the default page, installing PHP to enable dynamic content, and installing LAMP (Linux, Apache, MySQL, PHP) to allow database functionality. The document describes using phpMyAdmin for database administration and ProFTPd for file transfer. It includes steps for configuring Apache directories and default pages to support PHP sites.

Uploaded by

Arslan Coskun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Raspberry Pi - Part 14 - Web Server

This document provides instructions for setting up a web server on a Raspberry Pi using Apache and PHP. It discusses installing Apache and testing the default web page. It also covers changing the default page, installing PHP to enable dynamic content, and installing LAMP (Linux, Apache, MySQL, PHP) to allow database functionality. The document describes using phpMyAdmin for database administration and ProFTPd for file transfer. It includes steps for configuring Apache directories and default pages to support PHP sites.

Uploaded by

Arslan Coskun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Part 14

-
Web Server

Version: 2019-11-29

Raspberry Pi - Part 14 - Web Server.odt Page 1 of 12


Option 1 : Setting up an Apache Web Server

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:

sudo apt update

Then, install the apache2 package with this command:

sudo apt -y install apache2

Test the web server


By default, Apache puts a test HTML file in the web folder. This default web page is served
when you browse to http://localhost/ on the Pi itself, or http://192.168.1.10 (whatever
the Pi's IP address is) from another computer on the network. To find the Pi's IP address, type
hostname -I at the command line.
Browse to the default web page either on the Pi or from another computer on the network and
you should see the following:

This means you have Apache working!

Raspberry Pi - Part 14 - Web Server.odt Page 2 of 12


Changing the default web page
This default web page is just an HTML file on the filesystem. It is located at
/var/www/html/index.html.
Navigate to this directory in a terminal window and have a look at what's inside:

cd /var/www/html
ls -al

This will show you:

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.

Your own website


If you know HTML you can put your own HTML files and other assets in this directory and serve
them as a website on your local network.

Additional - install PHP


To allow your Apache server to process PHP files, you'll need to install the latest version of PHP
and the PHP module for Apache. Type the following command to install these:

sudo apt install php libapache2-mod-php -y

Now remove the index.html file:

sudo rm index.html

and create the file index.php:

sudo nano index.php

Put some PHP content in it:

<?php echo "hello world"; ?>

Now save and refresh your browser. You should see "hello world". This is not dynamic but still
served by PHP. Try something dynamic:

<?php echo date('Y-m-d H:i:s'); ?>

or show your PHP info:

<?php phpinfo(); ?>

Raspberry Pi - Part 14 - Web Server.odt Page 3 of 12


Option 2: Installing LAMP

LAMP stands for


 Linux
 Apache
 MySQL
 PHP

First run

sudo apt update

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:

sudo apt –y install apache2

Then we install MySQL


Note: This is only needed if this is required for one of the applications you will run on your Pi

sudo apt –y install mysql-server

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:

sudo apt –y install php


sudo apt –y install php-mysql

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]

Raspberry Pi - Part 14 - Web Server.odt Page 4 of 12


phpmyadmin

If you installed mysql, you can administer mysql from within the command line, but I prefer
the graphical tool phpmyadmin. To install this type:

sudo apt –y install phpmyadmin

During the installation we were asked to select the installed webserver – so choose apache2
and proceed with Ok.

Just press enter – to choose the easy way!

Now we need to remember the password for the mysql root user:

Raspberry Pi - Part 14 - Web Server.odt Page 5 of 12


You also need to state a password for the phpmyadmin accessing the mysql db, but if you
leave this blank the installer will choose one by it’s own.
After finishing the installation process you can point your browser to

http://[ip-of-your-pi]/phpmyadmin

You should see something like this:

Raspberry Pi - Part 14 - Web Server.odt Page 6 of 12


ftp-server

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

sudo apt –y install proftpd-basic

Choose “server mode” rather than “start with inetd”

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.

sudo nano /etc/proftpd/proftpd.conf

Change the line

# Defaultroot ~

to

Defaultroot /var/www

This sets the default root directory for all users to /var/www, the www root directory.

Raspberry Pi - Part 14 - Web Server.odt Page 7 of 12


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.

Restart the proftpd:

sudo /etc/init.d/proftpd restart

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.

sudo usermod -a -G www-data pi


sudo chown pi:www-data /var/www/html

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

sudo nano /etc/apache2/mods-enabled/dir.conf

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.

and restart the apache webserver.

sudo /etc/init.d/apache2 restart

Now the file index.php, if present, will be processed.

To test this create a new file called “index.php”

Raspberry Pi - Part 14 - Web Server.odt Page 8 of 12


sudo nano /var/www/html/index.php

and add the following content:

<?php
// show all info availalble
phpinfo();
?>

These simple lines displays the php capabilities of your webserver.

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.

After saving the file, we point our browser to

http://[ip-of-your-pi]

and we should see something similar to this:

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

 You want to store your files into /var/www/mydir

You only need to change the file /etc/apache2/sites-available/000-default.conf and


change DocumentRoot to /var/www/mydir instead of /var/www/html

sudo nano /etc/apache2/sites-available/000-default.conf

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.

and restart the apache webserver.

sudo /etc/init.d/apache2 restart

 You want to store your files into eg. /home/pi/www

You need to change the file /etc/apache2/apache2.conf.

sudo nano /etc/apache2/apache2.conf

Raspberry Pi - Part 14 - Web Server.odt Page 9 of 12


and change. It should be as below without the directory name www:

<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.

Now change the file /etc/apache2/sites-available/000-default.conf to change the


DocumentRoot to the custom directory name i.e. www:

sudo nano /etc/apache2/sites-available/000-default.conf

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.

and restart the apache webserver.

sudo /etc/init.d/apache2 restart

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.

Raspberry Pi - Part 14 - Web Server.odt Page 10 of 12


Appendix: Installing SOAP/SUDS module

To install the SOAP client SUDS module run following commands from a terminal:

sudo apt –y install python-suds

Raspberry Pi - Part 14 - Web Server.odt Page 11 of 12


Cleanup packages

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.

sudo apt –y clean


sudo apt –y autoremove

Raspberry Pi - Part 14 - Web Server.odt Page 12 of 12

You might also like