I’m trying to follow this tutorial and run sample script under cgi.
Below are steps which I did:
1) Made sample script
$ cat > test.pl <<EOF
> #!/usr/bin/perl
> print "Content-type: text/htmlnn";
> print "Hello, World.";
EOF
2) run httpd docker container and mounted created script as volume in cgi-bin directory:
$ docker run --name cgi_test --rm -p 3000:80 -v $(pwd)/test.pl:/usr/local/apache2/cgi-bin/test.pl httpd
3) check if CGI module is enabled:
$ docker exec -it cgi_test cat /usr/local/apache2/conf/httpd.conf | grep modules/mod_cgid.so
#LoadModule cgi_module modules/mod_cgid.so
CGI module was turned out so I enabled it:
$ docker exec -it cgi_test sed -i 's,#(LoadModule cgid_module modules/mod_cgid.so),1,g' /usr/local/apache2/conf/httpd.conf
4) Checked ScriptAlias
directive and enabling of alias_module
:
$ docker exec -it cgi_test cat /usr/local/apache2/conf/httpd.conf | grep "ScriptAlias /cgi-bin/"
ScriptAlias /cgi-bin/ "/usr/local/apache2/cgi-bin/"
$ docker exec -it cgi_test httpd -M | grep -e cgid -e alias
cgid_module (shared)
alias_module (shared)
It was ok
5) Checked exec permissions:
$ docker exec -it cgi_test ls -la /usr/local/apache2/cgi-bin/test.pl
-rwxr-xr-x 1 1000 1000 76 Mar 20 12:41 /usr/local/apache2/cgi-bin/test.pl
It was ok
6) Restarted httpd in docker container:
$ docker kill --signal="USR1" cgi_test
But when I to to http://localhost:3000/cgi-bin/test.pl I see 503 Service Unavailable
and this error in log:
[Wed Mar 20 19:03:02.323430 2019] [cgid:error] [pid 1446:tid 139952364902144] (22)Invalid argument: [client 172.17.0.1:34328] AH01257: unable to connect to cgi daemon after multiple tries: /usr/local/apache2/cgi-bin/test.pl
What else I need to do to run script ?
2
Answers
To make everything work I was needed to restart
httpd
twice instead of once :I thought maybe it's
USR1
signal (Graceful Restart) issue , but i had same withHUP
signal (Restart Now). To apply changes I need to send signal twice. So looks likehttpd
container issue.To run a script under CGI you don’t need to change any file in the official
httpd
Docker image.Make sure that your script is executable with a
chmod +x test.pl
and pass to the server program the-c
option with the configuration directive to load the CGI module, as described in the following command:Some explanations:
Alpine variant image is used because it’s smaller than the default Debian based image and it’s more than enough for our purposes
httpd-foreground
script is available in all officialhttpd
images and it allows to execute Apache HTTP Server in foreground mode (it’s equivalent tohttpd -D FOREGROUND
)publish option
-p
contains also the loopback IP address to prevent access to this test server from the outside