CGI stands for Common Gateway Interface. It’s a standard set of commands that a server can use to communicate with external applications, usually on the same server. You’ve probably heard of APIs (Application Programming Interfaces) before. They’re the code libraries and methods that software developers use to make their programs talk to each other and share information. CGI allows Apache web servers to execute scripts or programs and return dynamic content as a response. The scripts or programs are also referred to as “CGI applications” or “CGI executables”. CGI is commonly used by websites with user-generated content, such as forums and wikis, where different users have different privileges, roles, and capabilities depending on their usernames and personal information stored in a database. Let’s take a look at some excellent Apache CGIs Scripts that you can use in your projects right away!
This article will help you to configure your own script as CGI scripts in Apache VirtualHost. We will use some test CGI scripts for example. Follow the below steps.
Step 1 – Create a CGI Script
Create any test script in any scripting language. For this example, I am creating two scripts, one with the bash scripting and the other with Perl scripting.
Bash Script: [/usr/local/cgi-bin/mybash.cgi]
echo -e "Content-type: text/html\n\n"
echo "Welcome! to First CGI Script"
Perl Script: [/usr/local/cgi-bin/myperl.cgi ]
print "Content-type: text/html\n";
print "Welcome! to First CGI Script";
Now put your both scripts under the scripts directory which can be created anywhere in the system. Make sure that it is accessible by Apache. For this example, we have created both scripts under the /usr/local/cgi-bin/ directory.
Step 2 – Configure Apache VirtualHost
Now edit the Apache configuration file and go to the defined VirtualHost. Add the following highlighted lines in the VirtualHost configuration.
ScriptAlias /cgi-bin/ "/usr/local/cgi-bin/"
Require all granted
AddHandler cgi-script .cgi
Save the configuration file and restart the Apache service.
Step 3 – Verify CGI Script
Now test your CGI scripts by opening URLs like the below in your favorite web browser. You will show the text “Welcome! to First CGI Script” on your webpage.
The Apache CGIs Scripts are among the most useful scripts you can find. They can be used for any number of things, from very simple applications to complicated systems that do multiple functions. With these scripts, you’ll always be able to get the job done and make your server more efficient. Now that you’re done reading about these Apache CGIs Scripts, it’s time to get writing some code!