Raspberry Pi as server

Setup & config options

Apache 2.4+ LAMP server

»Windward« server

The game & not the islands
 Setup & config options

Some off-topics
Free weather app. Weather widget

Lat. 52.27, Long. 8.01

 

Meteorological service

Front desk clerk

🚫  No ads & tracking

Tag cloud
QRC bookmark

QR quick response code




Github VSCO Vimeo | Madeira | Madeirense Watch on Youtube

The prologue


Most of the articles, descriptions and instructions written here are applicable to the most common Debian-based Linux derivatives. Depending on the respective operating system, there may be minor or major discrepancies.
This website is for educational purposes only. Please do not deploy anything in manufacturing plants.
No warranty or compensation is given for loss of data or hardware.

It should be also mentioned that this modest web server is hosted on a Raspberry Pi type 4B at home.


The Raspberry Pi mini-computer board as multi-purpose server deployed
A competent allrounder for domestic purposes and micro-enterprises


Raspberry Pi : Apache as multiple web server. Print server, scan server, backup and NAS server. Raspberry Pi : Apache as multiple web server. Print server, scan server, backup and NAS Server.

Raspberry Pi is a series of small single-board computers (SBCs) developed in the United Kingdom by the Raspberry Pi Foundation in association with Broadcom. The mini-computer with its armv7l processor has quickly become the favourite of hobbyists. Projects can be started with suitable Linux distributions. Even an aged RasPi e.g. the models 2B and 2B+ can definitely serve to simple tasks quite well.


First of all, you always have to remember that you must trust the writers of the CGI scripts/programs or your ability to spot potential security holes in CGI, whether they were deliberate or accidental. CGI scripts can run essentially arbitrary commands on your system with the permissions of the web server user and can therefore be extremely dangerous if they are not carefully checked.

Raspberry Pi | Get CGI/Perl scripts running on Apache v2.4+ web server


CGI Common Gateway Interface) scripts were the initial mechanism used to make websites interact with databases and other applications.

The case history : CGI and Perl scripts are called by a web server when a user requests particular URLs or interacts with webpages such as insert a weblink in a directory or leave a forum comment or new thread. After the script is called and finishes executing, the output is used by the web server to create a page displayed to the users and visitors.


If you upload and install scripts, you may need to know the location (path) of your »cgi-bin« directory on the server's disk. The path to your »cgi-bin« directory is valid for this demonstration on a common Raspberry Pi and Raspbian operating system »Stretch Lite« and »Buster«.


/var/www/html/cgi-bin/


The »cgi-bin« folder does not exist until you create it with your FTP program at the top level of your website directory. From the perspective of your FTP program, it's just a ordinary folder that you create, but it's treated differently by the web server because of its name. Set this folder with chmod to 755.


Assuming you have already Apache v2.4+ installed, if not please visit this page.


If yes, now it is higly recommended to backup both *.conf files prior continuing with the next steps.

/etc/apache2/apache2.conf and /etc/apache2/conf-available/serve-cgi-bin.conf


Initially bring your system up to date


Start your terminal program e.g. PuTTy and login to your Raspberry.
Continue as superuser.


user@raspberry:~ $ sudo su
root@raspberry:~# apt-get update && apt-get upgrade && apt-get dist-upgrade


Reboot your Linux machine if required.


root@raspberry:~# reboot


Installation and the path to Perl


user@raspberry:~ $ sudo su
root@raspberry:~# apt install perl
root@raspberry:~# which perl


	  /usr/bin/perl
	  

Your actual path to perl is :


	  #!/usr/bin/perl
	  

One of the first things you must know when configuring a script, is to declare the correct path to the Perl interpreter, which is the engine responsible for processing the script.


/var/www/html is the absolute path for any *.cgi *.pl or *.py you did set up.


root@raspberry:~# apache2 -v


	  Server version: Apache/2.4+.38 (Raspbian)
	  Server built:   2020-08-25T20:08:29
	  

The version of Apache installed on a Raspbian / Debian / Linux machine.


Perl configuration


You have to make changes on two configuration files.


/etc/apache2/apache2.conf


root@raspberry:~# nano /etc/apache2/apache2.conf


Add the code at the end of file.


	  # Capability to run CGI scripts
	  ServerName localhost
	  ScriptAlias /cgi-bin/ /var/www/html/cgi-bin/
	  Options +ExecCGI
	  AddHandler cgi-script .cgi .pl .py
	  

Note : converting the »cgi-bin« address to the above mentioned specified address and the »AddHandler« tell the Apache web server to handle all files with *.cgi *.pl *.py extensions as CGI scripts.


Save the file and exit your editor.


/etc/apache2/conf-available/serve-cgi-bin.conf


root@raspberry:~# nano /etc/apache2/conf-available/serve-cgi-bin.conf


Modify the source code between lines <IfDefine ENABLE_USR_LIB_CGI_BIN> and </IfDefine>


	  <IfDefine ENABLE_USR_LIB_CGI_BIN>
	  ScriptAlias /cgi-bin/ /var/www/html/cgi-bin/
	  Directory "/var/www/html/cgi-bin/">
	  AllowOverride None
	  Options +ExecCGI
	  /Directory>
	  </IfDefine>
	  

Save the file and exit your editor.


Restart the apache2 service. ✔


root@raspberry:~# service apache2 restart


Simplest CGI script & browser check


	  #!/usr/bin/perl
	  print "Content-Type: text/html\n\n";
	  print "Congratulations! Your CGI script is working!";
	  

Save the code to a file called »test.cgi«, the upload into your »cgi-bin« folder and afterwards chmod »test.cgi« to 755 (rwxr-xr-x). This allows everyone can read and execute the file, and only the owner of the file additionally is allowed to write the file.


root@raspberry:~# chmod 755 /var/www/html/cgi-bin/test.cgi


Open your web browser and type »http://yourdomain.tld/cgi-bin/test.cgi«. Of course, »yourdomain.tld« has to be replaced with your own domain name.


Congratulations! Your CGI script is working!



🙁  Troubleshooting


user@raspberry:~ $ sudo su
root@raspberry:~# a2enmod cgi


The good news :

Module cgi already enabled


The bad news, if you encounter an error :


Consult the apache2 error log file first /var/log/apache2/error.log


Correct if neccessary both configuration files /etc/apache2/apache2.conf

/etc/apache2/conf-available/serve-cgi-bin.conf


Do never forget to restart the service apache2 afterwards.


Examples to possible error messages and codes : /var/log/apache2/error.log


      [Thu Oct 10 13:57:48.734544 2017] [cgi:error] [pid 8864] 
      [client 89.166.147.86:50985] AH01215: (2)No such file or 
      directory: exec of '/var/www/html/cgi-bin/test.cgi' failed

      [Thu Oct 10 13:57:48.737467 2017] [cgi:error] [pid 8864] 
      [client 89.166.147.86:50985] End of script output before headers: test.cgi
	  

Security issues with CGI/Perl scripts


One big source of security problems in CGI & Perl scripts is improperly validated (or unvalidated) user input. Any time your program might take input from an untrusted user, even indirectly, you should be cautious.


For example, if you are writing CGI scripts in Perl, expect that »malicious users« will send you »malicious input«. Improper user input to such applications can cause many things to go wrong. Think about it.


Apache's tutorial : Dynamic Content with CGI


13-Oct 2017

Updated 21-Jun 2022

dosboot.org 2024 | Design and layout handmade in Northwest Europe