Background start Linux, install Apache2
ssh root@107.170.93.222 # aptitude update # aptitude safe-upgrade
ssh root@107.170.93.222 # aptitude install apache2-mpm-prefork
# ps axuw | grep apache
root 1961 0.0 0.5 71284 2608 ? Ss 14:16 0:00 /usr/sbin/apache2 -k start www-data 1964 0.0 0.4 360448 2220 ? Sl 14:16 0:00 /usr/sbin/apache2 -k start www-data 1965 0.0 0.4 360448 2220 ? Sl 14:16 0:00 /usr/sbin/apache2 -k start root 2091 0.0 0.1 9452 908 pts/0 S+ 14:16 0:00 grep --color=auto apache
Creating the first CGI script in Perl
mkdir /var/cgi-bin
#!/usr/bin/perl use strict; use warnings; print qq(Content-type: text/plain\n\n); print "hi\n";
chmod +x /var/cgi-bin/echo.pl
# /var/cgi-bin/echo.pl
Content-type: text/plain hi
Configure Apache to serve CGI files
Open the configuration file of Apache /etc/apache2/sites-enabled/It has the following in it with a bunch of comments between the lines:
<VirtualHost *:80 >
ServerAdmin webmaster@localhost DocumentRoot /var/www ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
< /VirtualHost >
Add the following lines after the DocumentRoot line:
ScriptAlias /cgi-bin/ /var/cgi-bin/ <Directory "/var/cgi-bin" > AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Require all granted </Directory >
Now you should have:
<VirtualHost *:80 > ServerAdmin webmaster@localhost DocumentRoot /var/www ScriptAlias /cgi-bin/ /var/cgi-bin/
AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Require all granted ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost >
By default this Apache instance does not have the CGI module enabled. This we can see by noticing that the mods-enabled directory does not have any of the cgi files that are available in the mods-available directory:
# ls -l /etc/apache2/mods-enabled/ | grep cgi # ls -l /etc/apache2/mods-available/ | grep cgi -rw-r--r-- 1 root root 115 Jul 20 2013 cgid.conf -rw-r--r-- 1 root root 60 Jul 20 2013 cgid.load -rw-r--r-- 1 root root 58 Jul 20 2013 cgi.load -rw-r--r-- 1 root root 89 Jul 20 2013 proxy_fcgi.load -rw-r--r-- 1 root root 89 Jul 20 2013 proxy_scgi.load
# ln -s /etc/apache2/mods-available/cgid.load /etc/apache2/mods-enabled/ # ln -s /etc/apache2/mods-available/cgid.conf /etc/apache2/mods-enabled/ # ls -l /etc/apache2/mods-enabled/ | grep cgi lrwxrwxrwx 1 root root 37 Mar 19 14:39 cgid.conf -> /etc/apache2/mods-available/cgid.conf lrwxrwxrwx 1 root root 37 Mar 19 14:39 cgid.load -> /etc/apache2/mods-available/cgid.load
# service apache2 reload
Dynamic results
#!/usr/bin/perl use strict; use warnings; print qq(Content-type: text/plain\n\n); print scalar localtime;
Trouble shooting
500 Internal Server Error
[Wed Mar 19 15:19:15.740781 2014] [cgid:error] [pid 3493:tid 139896478103424] (8)Exec format error: AH01241: exec of '/var/cgi-bin/echo.pl' failed [Wed Mar 19 15:19:15.741057 2014] [cgid:error] [pid 3413:tid 139896186423040] [client 192.120.120.120:62309] End of script output before headers: echo.pl
#!/usr/bin/perl [Wed Mar 19 15:24:33.504988 2014] [cgid:error] [pid 3781:tid 139896478103424] (2)No such file or directory: AH01241: exec of '/var/cgi-bin/echo.pl' failed [Wed Mar 19 15:24:33.505429 2014] [cgid:error] [pid 3412:tid 139896261957376] [client 192.120.120.120:58087] End of script output before headers: echo.pl
dos2unix /var/cgi-bin/echo.pl [Wed Mar 19 15:40:31.179155 2014] [cgid:error] [pid 4796:tid 140208841959296] (13)Permission denied: AH01241: exec of '/var/cgi-bin/echo.pl' failed
[Wed Mar 19 15:40:31.179515 2014] [cgid:error] [pid 4702:tid 140208670504704] [client 192.120.120.120:60337] End of script output before headers: echo.pl
chmod +x /var/cgi-bin/echo.pl Wed Mar 19 16:02:20.239624 2014] [cgid:error] [pid 4703:tid 140208594970368] [client 192.120.120.120:62841] malformed header from script 'echo.pl': Bad header: hi
#!/usr/bin/perl use strict; use warnings; print "hi\n"; print qq(Content-type: text/plain\n\n); [Wed Mar 19 16:08:00.342999 2014] [cgid:error] [pid 4703:tid 140208536221440] [client 192.120.120.120:59319] End of script output before headers: echo.pl
$| = 1;
503 Service Unavailable
[Wed Mar 19 15:30:22.515457 2014] [cgid:error] [pid 3927:tid 140206699169536] (22)Invalid argument: [client 192.120.120.120:58349] AH01257: unable to connect to cgi daemon after multiple tries: /var/cgi-bin/echo.pl
# service apache2 restart
404 Not Found
[Wed Mar 19 15:35:13.487333 2014] [cgid:error] [pid 4194:tid 139911599433472] [client 192.120.120.120:58339] AH01264: script not found or unable to stat: /usr/lib/cgi-bin/echo.pl