I need to execute a perl script in the root directory of my server from a webpage (on same server). 4 parameters need to be passed to this script from input boxes on the page for it to work.
What would be the best way to do this? Please advise. If possible please provide an example.
Many thanks
Servers like Apache have a /cgi-bin/
handler. You would make a request to
http://site.tld/cgi-bin/script.pl?param=val¶m2=val
or something similar. This script.pl actually resides elsewhere. One common location is /usr/lib/cgi-bin
.
I made a quick screen cast about how this would be done. I've only done this for Python before, so this was a learning and teaching at once for me.
Links from the video:
- http://httpd.apache.org/docs/2.0/mod/mod_mime.html#addhandler
- tutorial
- tutorial hello world
- Page address: http://localhost/cgi-bin/test.pl
This is the end result of the video
I type comments on the command line explaining certain parts. One that I usually forget is to chmod
the scripts. If they aren't executable, the server won't execute them and you'll have no idea why.
Your web server's software defines an interface to cooperate with external scripts. The most prevalent of these is the Common Gateway Interface (CGI). Without knowing more about your server's setup, we can't say anything more specific.
If you are using Apache, take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html which tells how to set CGI up.
There are other approaches to this, and they all depend intimately on the particular server software you are running.
Why not have the webpage post a form to a CGI script that calls the perl script (or make the perl script the CGI page)?