The Common Gateway Interface (CGI) is a standard protocol that enables web servers to execute external scripts and return dynamic content to web clients. Apache, one of the most widely used web servers, supports CGI scripts out of the box. In this article, we will explain how to enable or disable CGI scripts in Apache on various platforms, including Ubuntu, CentOS, and Fedora.
Prerequisites
Before proceeding, ensure you have the following:
- A server running Ubuntu, CentOS, or Fedora with Apache installed.
- Access to a user account with sudo or root privileges.
- A text editor, such as nano or vi, for editing configuration files.
Enabling CGI Scripts in Apache
Step 1: Install the CGI Module (Ubuntu and Debian)
On Ubuntu and Debian-based systems, you need to install the CGI module for Apache. Run the following command to install the module:
sudo apt-get install libapache2-mod-cgi
Step 2: Enable the CGI Module
To enable the CGI module in Apache, use the following command:
- On Ubuntu and Debian-based systems:
sudo a2enmod cgi
- On CentOS and Fedora:
The CGI module is enabled by default, so no additional steps are required.
Step 3: Configure Apache to Execute CGI Scripts
- Open the Apache configuration file using a text editor:
- On Ubuntu and Debian-based systems:
sudo nano /etc/apache2/apache2.conf
- On CentOS and Fedora:
sudo nano /etc/httpd/conf/httpd.conf
- On Ubuntu and Debian-based systems:
- Locate the following block of text within the configuration file:
<Directory "/var/www/html"> </Directory>
(The directory path may vary depending on your system’s configuration.)
- Add the following lines within the
block: <Directory "/var/www/html"> Options +ExecCGI AddHandler cgi-script .cgi .pl </Directory>
This configuration allows Apache to execute CGI scripts with .cgi and .pl file extensions.
- Save the changes and close the text editor.
Step 4: Restart Apache
Restart the Apache web server to apply the changes:
- On Ubuntu and Debian-based systems:
sudo systemctl restart apache2
- On CentOS and Fedora:
sudo systemctl restart httpd
Disabling CGI Scripts in Apache
To disable CGI scripts in Apache, follow these steps:
Step 1: Disable the CGI Module
- On Ubuntu and Debian-based systems:
sudo a2dismod cgi
- On CentOS and Fedora:
The CGI module is enabled by default, so proceed to Step 3.
Step 2: Uninstall the CGI Module (Ubuntu and Debian)
On Ubuntu and Debian-based systems, run the following command to uninstall the CGI module:
sudo apt-get remove libapache2-mod-cgi
Step 3: Update the Apache Configuration
- Open the Apache configuration file using a text editor:
- On Ubuntu and Debian-based systems:
sudo nano /etc/apache2/apache2.conf
- On CentOS and Fedora:
sudo nano /etc/httpd/conf/httpd.conf
- On Ubuntu and Debian-based systems:
- Locate the “<Directory>” block where the CGI settings were added (refer to Step 3 in the “Enabling CGI Scripts” section).
- Remove or comment out the following lines:
Options +ExecCGI AddHandler cgi-script .cgi .pl
- Save the changes and close the text editor.
Step 4: Restart Apache
Restart the Apache web server to apply the changes:
- On Ubuntu and Debian-based systems:
sudo systemctl restart apache2
- On CentOS and Fedora:
sudo systemctl restart httpd
Conclusion
You now know how to enable or disable CGI scripts in Apache on various platforms, including Ubuntu, CentOS, and Fedora. With CGI support enabled, you can leverage the power of external scripts to create dynamic web content and enhance the functionality of your website.
It’s essential to ensure that your server is secure when enabling CGI scripts, as poorly written or outdated scripts can introduce security vulnerabilities. Always use secure coding practices, keep your scripts up to date, and monitor your server for any signs of unauthorized access or unusual activity.
By following the steps outlined in this article, you can successfully enable or disable CGI scripts in Apache, providing you with greater control over your web server’s features and capabilities.
3 Comments
Thank you. My site hosted on AWS has been subjected to malicious activity. As I do not use CGI I am turning it off.
Like your site alot thanks
I just want to say that I like your posting. In fact I am using your site regularly. Your articles are very effective and i am very thankful to you for sharing this site with knowledgeable content .