Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Web Servers»Apache»How To Enable or Disable CGI Scripts in Apache

    How To Enable or Disable CGI Scripts in Apache

    By RahulJuly 28, 20233 Mins Read

    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

    1. 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 
        
    2. Locate the following block of text within the configuration file:

      1
      <Directory "/var/www/html">

      (The directory path may vary depending on your system’s configuration.)

    3. Add the following lines within the block:

      1
      2
      3
      4
      <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.

    4. 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

    1. 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 
        
    2. Locate the “<Directory>” block where the CGI settings were added (refer to Step 3 in the “Enabling CGI Scripts” section).
    3. Remove or comment out the following lines:

      1
      2
      Options +ExecCGI
      AddHandler cgi-script .cgi .pl

    4. 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.

    Apache Apache 2.4 CGI mod_cgi
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Enable Server Side Include (SSI) in Apache

    How To Enable Server Side Includes (SSI) in Apache

    How to Enable Caching in Apache

    Installing Tomcat 10

    How to Install and Configure Tomcat 10 on Ubuntu 22.04

    View 3 Comments

    3 Comments

    1. steve on October 10, 2019 7:37 pm

      Thank you. My site hosted on AWS has been subjected to malicious activity. As I do not use CGI I am turning it off.

      Reply
    2. Marco on December 5, 2018 4:23 am

      Like your site alot thanks

      Reply
    3. O2infosystems on January 27, 2016 5:23 am

      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 .

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.