CGI, which stands for Common Gateway Interface, is a protocol that allows web servers like Apache to communicate with external programs or scripts. CGI scripts are often used to generate dynamic content on websites, such as search results or user login pages. Configuring CGI scripts in Apache is a relatively simple process, but it does require some basic knowledge of Apache configuration files and server administration.
Here are the steps you need to follow to configure CGI scripts in Apache:
Step 1: Enable CGI support in Apache
Before you can configure CGI scripts in Apache, you need to make sure that CGI support is enabled. To do this, you will need to edit the Apache configuration file. The location of this file varies depending on your server setup, but it is usually located in /etc/httpd/ or /usr/local/apache2/conf/.
Once you have located the configuration file, open it in a text editor and search for the following line:
#LoadModule cgi_module modules/mod_cgi.so
Remove the “#” at the beginning of the line to uncomment it and enable the CGI module. Save the file and exit the text editor.
Step 2: Create a CGI script
Once CGI support is enabled, you can create a CGI script. CGI scripts are usually written in Perl, Python, or shell script, but they can be written in any language that can output data to the standard output.
Create a new file in a text editor and save it with a .cgi
extension. Here is an example of a simple Perl script:
#!/usr/bin/perl
print "Content-type: text/html\n";
print "Welcome! to First CGI Script";
Save the file and upload it to your web server in a directory that is accessible by the Apache web server.
Step 3: Configure Apache to recognize the CGI script
Once you have created the CGI script, you must configure Apache to recognize it as a CGI script. To do this, you will need to edit the Apache configuration file again.
Search for the following lines in the configuration file:
<Directory "/var/www/html">
AllowOverride None
Require all granted
</Directory>
Replace “/var/www/html” with the directory where you uploaded the CGI script. Add the following lines inside the Directory block:
Options +ExecCGI
AddHandler cgi-script .cgi
These lines tell Apache to execute any files with a .cgi
extension as CGI scripts. The complete Apache configuration looks something like below:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ "/usr/local/cgi-bin/"
<Directory "/var/www/cgi-bin/">
Require all granted
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
</VirtualHost>
Save the configuration file and restart the Apache web server for the changes to take effect.
Step 4: Test the CGI script
To test the CGI script, open a web browser and enter the URL of the script in the address bar. For example, if your script is called “test.cgi” and it is located in the “/var/www/cgi-bin/” directory, you would enter the following URL:
- http://yourdomain.com/cgi-bin/test.cgi
If everything is configured correctly, you should see the output of your CGI script displayed in your web browser.
Conclusion
Configuring CGI scripts in Apache is a relatively simple process that can be done by following these four basic steps. Enabling CGI support, creating a CGI script, configuring Apache to recognize the script, and testing the script are all essential steps in configuring CGI scripts in Apache. With these steps, you can create dynamic, interactive web pages that can be used to provide information or services to your website visitors.
3 Comments
I needed to thank you for this excellent read!!
I absolutely loved every little bit of it. I’ve got you book marked to look at new things you post…
1. The Perl-script is missing one newline.
2. For it to work a symbolic link to cgi.load had to be created in modules-enabled. Then a restart of Apache.
sudo ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/
sudo service apache2 restart
You were not mistaken