Webserver · 4 min read · Jan 23, 2026

CGI-Skripte mit Nginx auf Fedora 18 bereitstellen

CGI-Skripte mit Nginx auf Fedora 18 bereitstellen

Version 1.0
Autor: Falko Timme
Folge mir auf Twitter

Dieses Tutorial zeigt, wie Sie CGI-Skripte (Perl-Skripte) mit nginx auf Fedora 18 bereitstellen können. Während nginx selbst keine CGI-Skripte bereitstellt, gibt es mehrere Möglichkeiten, dies zu umgehen. Ich werde zwei Lösungen skizzieren: Die erste besteht darin, Anfragen für CGI-Skripte an Thttpd, einen kleinen Webserver mit CGI-Unterstützung, weiterzuleiten, während die zweite Lösung einen CGI-Wrapper verwendet, um CGI-Skripte bereitzustellen.

Ich gebe keine Garantie, dass dies bei Ihnen funktioniert!

1 Vorbemerkung

Ich verwende hier die Website www.example.com mit dem Dokumentenstamm /var/www/www.example.com/web/; die vhost-Konfiguration befindet sich in /etc/nginx/conf.d/www.example.com.vhost.

2 Verwendung von Thttpd

In diesem Kapitel werde ich beschreiben, wie man nginx konfiguriert, um Anfragen für CGI-Skripte (Erweiterungen .cgi oder .pl) an Thttpd weiterzuleiten. Ich werde Thttpd so konfigurieren, dass es auf Port 8000 läuft.

Zuerst installieren wir Thttpd. Es gibt ein Thttpd-Paket für Fedora 18, aber die nginx ThttpdCGI-Seite sagt, dass Thttpd gepatcht werden sollte - daher laden wir das src.rpm-Paket für Fedora 18 herunter, patchen es und erstellen ein neues rpm-Paket daraus.

Wir müssen die Werkzeuge installieren, die erforderlich sind, um ein neues rpm-Paket zu erstellen:

yum groupinstall 'Development Tools'

Installieren Sie yum-utils (das Paket enthält das yumdownloader-Tool, mit dem wir ein src.rpm herunterladen können):

yum install yum-utils

Als nächstes laden wir das Thttpd src.rpm-Paket für Fedora 18 herunter:

cd /usr/src  
yumdownloader --source thttpd
ls -l
[root@server1 src]# ls -l  
total 164  
drwxr-xr-x. 2 root root   4096 Feb  3  2012 debug  
drwxr-xr-x. 3 root root   4096 Jun  4 18:21 kernels  
-rw-r--r--  1 root root 155690 Mar 28 03:21 thttpd-2.25b-28.fc18.src.rpm  
[root@server1 src]#
rpm -ivh thttpd-2.25b-28.fc18.src.rpm

Sie können die folgenden Warnungen ignorieren:

[root@server1 src]# rpm -ivh thttpd-2.25b-28.fc18.src.rpm  
   1:thttpd             warning: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
warnings: user mockbuild does not exist - using root  
warnings: group mockbuild does not exist - using root  
########################################### [100%]  
[root@server1 src]#

Jetzt laden wir den Patch in das Verzeichnis /root/rpmbuild/SOURCES/ herunter und ändern die Datei /root/rpmbuild/SPECS/thttpd.spec entsprechend:

cd /root/rpmbuild/SOURCES/  
wget -O thttpd-2.25b-ipreal.patch http://www.danielclemente.com/amarok/ip_real.txt  
cd /root/rpmbuild/SPECS/  
vi thttpd.spec

Fügen Sie die Zeilen Patch3: thttpd-2.25b-ipreal.patch und %patch3 -p1 -b .ipreal hinzu:

| [...] Patch0: thttpd-2.25b-CVE-2005-3124.patch Patch1: thttpd-2.25b-fixes.patch Patch2: thttpd-2.25b-getline.patch Patch3: thttpd-2.25b-ipreal.patch [...] %prep %setup -q %patch0 -p1 -b .CVE-2005-3124 %patch1 -p1 -b .fixes %patch2 -p1 -b .getline %patch3 -p1 -b .ipreal [...] |

Jetzt bauen wir unser Thttpd rpm-Paket wie folgt:

rpmbuild -ba thttpd.spec

Unser Thttpd rpm-Paket wird in /root/rpmbuild/RPMS/x86_64 ( /root/rpmbuild/RPMS/i386, wenn Sie auf einem i386-System sind) erstellt, also gehen wir dorthin:

cd /root/rpmbuild/RPMS/x86_64  
ls -l
[root@server1 x86_64]# ls -l  
total 224  
-rw-r--r-- 1 root root  69881 Sep  3 23:17 thttpd-2.25b-28.fc18.x86_64.rpm  
-rw-r--r-- 1 root root 151685 Sep  3 23:17 thttpd-debuginfo-2.25b-28.fc18.x86_64.rpm  
[root@server1 x86_64]#

Installieren Sie das Thttpd-Paket wie folgt:

rpm -ivh thttpd-2.25b-28.fc18.x86_64.rpm

Dann machen wir eine Sicherung der ursprünglichen /etc/thttpd.conf-Datei und erstellen eine neue wie folgt:

mv /etc/thttpd.conf /etc/thttpd.conf_orig  
vi /etc/thttpd.conf

| # ACHTUNG : Keine leeren Zeilen sind erlaubt! # Dieser Abschnitt überschreibt die Standardwerte # Dieser Abschnitt _dokumentiert_ die wirksamen Standardwerte # port=80 # nosymlink # standard = !chroot # novhost # nocgipat # nothrottles # host=0.0.0.0 # charset=iso-8859-1 host=127.0.0.1 port=8000 user=thttpd logfile=/var/log/thttpd.log pidfile=/var/run/thttpd.pid dir=/var/www cgipat=.cgi|.pl |

Dies wird Thttpd dazu bringen, auf Port 8000 auf 127.0.0.1 zu hören; sein Dokumentenstamm ist /var/www.

Erstellen Sie die Systemstartlinks für Thttpd…

systemctl enable thttpd.service

… und starten Sie es:

systemctl start thttpd.service

Erstellen Sie als nächstes /etc/nginx/proxy.conf:

vi /etc/nginx/proxy.conf

| proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; |

Jetzt öffnen Sie Ihre vhost-Konfigurationsdatei…

vi /etc/nginx/conf.d/www.example.com.vhost

… und fügen Sie einen Abschnitt location /cgi-bin {} zum server {} Container hinzu:

| server { [...] location /cgi-bin { include proxy.conf; proxy_pass http://127.0.0.1:8000; } [...] } |

Laden Sie nginx neu:

systemctl reload nginx.service

Da der Dokumentenstamm von Thttpd /var/www ist, übersetzt location /cgi-bin in das Verzeichnis /var/www/cgi-bin (dies gilt für alle Ihre vhosts, was bedeutet, dass jeder vhost seine CGI-Skripte in /var/www/cgi-bin ablegen muss; dies ist ein Nachteil für Shared-Hosting-Umgebungen; die Lösung besteht darin, anstelle von Thttpd einen CGI-Wrapper wie in Kapitel 3 beschrieben zu verwenden).

Erstellen Sie das Verzeichnis…

mkdir /var/www/cgi-bin

… und platzieren Sie dann Ihre CGI-Skripte darin und machen Sie sie ausführbar. Zu Testzwecken werde ich ein kleines Hello World Perl-Skript erstellen (anstatt hello_world.cgi können Sie auch die Erweiterung .pl -> hello_world.pl verwenden):

vi /var/www/cgi-bin/hello_world.cgi

| #!/usr/bin/perl -w # Sage Perl, dass es einen HTML-Header senden soll. # Damit Ihr Browser die Ausgabe erhält # anstelle von (Befehlszeile # auf dem Server.) print "Content-type: text/html\n\n"; # Drucken Sie Ihre grundlegenden HTML-Tags. # und den Inhalt davon. print "Hello World!! \n"; print "

Hello world

\n"; |

chmod 755 /var/www/cgi-bin/hello_world.cgi

Öffnen Sie einen Browser und testen Sie das Skript:

http://www.example.com/cgi-bin/hello_world.cgi

Wenn alles gut geht, sollten Sie die folgende Ausgabe erhalten:

Share: X/Twitter LinkedIn

Erhalte neue Beiträge in deinem Posteingang.

Kein Spam. Jederzeit abmelden.