it's a raspberry pi

Editorials

 Setups & configurations

anchor life saver

 We got some boring movies

🎥 Cinematique

 Regarding unity3d

🙂 In-game char icons
Externals

 All about pirate games

⚓ Pirates ahoy!

 This is German language

💻 Linux - ubuntusers.de
Windward bathtub
Tasharen Windward Game

 Social news aggregation

🌍 Windward #reddit
Windward intensive

 Official Windward wiki

🛠 Wiki @gamepedia
Front desk clerk

No ads, no trackers,
no web beacons

rum barrel
Something else

 Get the weather widget

🌤 Weather code snippet

 Hotchpotch of weblinks

📖 Yellow pages
Archives
Tag cloud

Raspberry Pi | Get CGI/Perl scripts running on Apache 2.4 web server


Apache Setup Raspberry Pi mini computer to dedicated game server. LAMP. Apache Raspberry Pi mini computer. Dedicated game server.
That is simply just another http web server powered by Apache Software
and the Raspberry Pi Foundation with Raspbian Debian OS Stretch Lite on it.


CGI scripts (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":

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

apache2.conf (path = /etc/apache2/apache2.conf)
serve-cgi-bin.conf (path = /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


Path to Perl


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


      /usr/bin/perl
	  
Your 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 setup.

root@raspberry:~# apache2 -v


      Server version: Apache/2.4.10 (Raspbian)
      Server built: Apr 3 2018 16:25:06
      
Version of Apache installed on a Debian machine.

Perl have not installed?


root@raspberry:~# apt-get install perl


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 & the AddHandler tells the web server to handle 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> & </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", and upload into your "cgi-bin" folder.
Chmod "test.cgi" to 755 (rwxr-xr-x). This allows everyone to read and execute the file, and the file owner - You - is allowed to write to the file as well.

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

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
➋ Read and correct if neccessary
/etc/apache2/apache2.conf
/etc/apache2/conf-available/serve-cgi-bin.conf
Do never forget to restart the apache2 service afterwards.

Examples to possible error codes in /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 Tutorial: Dynamic Content with CGI


13-Oct 2017

Revised 03-Dec 2018

Hafenzoll 2019