Open In App

How To Enable or Disable CGI Scripts in Apache?

Last Updated : 04 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This article will guide you on how to enable or disable CGI scripts in Apache. Configuring CGI scripts in Apache is a crucial aspect of managing dynamic content generation on web servers. The Common Gateway Interface (CGI) provides a standardized protocol for executing programs, allowing websites to produce dynamic output. Whether enabling or disabling CGI scripts, Apache’s configuration plays a pivotal role. This article guides you through the necessary steps to seamlessly control the execution of CGI scripts on your Apache server, ensuring a tailored and secure environment for dynamic web content in Python.

Enable or Disable CGI Scripts in Apache

To enable or disable CGI scripts in Apache, we will create a sample CGI script and demonstrate the straightforward steps for both enabling and disabling CGI scripts. Follow the outlined steps below to gain a clear understanding of how to manage CGI script execution in Apache.

Create Folder

First, create a folder named “GeeksforGeeks” in the “htdocs” directory. Inside this “GeeksforGeeks” folder, create two files: “html.html” and “python.py”. In the “html.html” file, design the registration form.

In the “python.py” file, write a CGI script to retrieve data from the registration form and generate a card with a success message.

jjjjj

Write HTML Code

This HTML code defines a webpage with a title, “Welcome to GeeksforGeeks,” and a form that collects user input for a name. The form has a submit button, and the page includes some basic styling using inline CSS to set the color of the heading and style the form elements.

The form is set to submit data to a Python script named “python.py” using the POST method.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Welcome to GeeksforGeeks</title>
    <style>
        /* Style for the "GeeksforGeeks" heading */
        h2{
            color: green;
        }
        /* Style for form labels and input fields */
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="submit"] {
            background-color: green;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h2>Welcome to GeeksforGeeks</h2>
    <div class="form-container">
        <h3>fill below data</h3>
        <form action="python.py" method="post">
            <label for="name">Name:</label>
            <input type="text" name="name" required><br><br>
            <input type="submit" name="Register">
        </form>
    </div>
</body>
</html>


Output

first

Write Python CGI Script

This Python script, intended for execution on a web server, processes form data received from an HTML form submission. It prints an HTML response indicating that the data has been submitted and displays the submitted name.

The response includes some basic styling for a card container with a success message and an emoji. The script uses the Common Gateway Interface (CGI) module to handle form data.

Python3




#!C:\Program Files\Python311\python.exe
 
print("Content-type: text/html\n\n")
import cgi
 
# Get form data
form = cgi.FieldStorage()
 
# Retrieve values from the form
name = form.getvalue('name')
 
# HTML response
print("<html>")
print("<head>")
print("<title>Data Submitted</title>")
print("<style>")
print("  /* Style for the card container */")
print("  .card {")
print("    width: 300px;")
print("    padding: 20px;")
print("    margin: 20px ;")
print("    border: 1px solid #ccc;")
print("    border-radius: 5px;")
print("    text-align: center;")
print("  }")
print("</style>")
print("</head>")
print("<body>")
print("<div class='card'>")
print("<h3>Data Submitted</h3>")
print("<p>Name: " + name + "</p>")
print("<p>Data Saved ! 👍</p>")  # Emoji for success
print("</div>")
print("</body>")
print("</html>")


Output

second

Configuration and Start the Xampp Server

You can refer Create a CGI Script for complete configuration and how we can start the server to run out CGI Script.

Run the Script

In this step, we will run the CGI Script by using the following command in your web browser.

http://127.0.0.1/GeeksforGeeks/html.html

Output

Conclusion

The phrase “How To Enable or Disable CGI Scripts in Apache?” suggests that there is a discussion or set of instructions related to configuring Apache web server settings to either allow or restrict the execution of CGI (Common Gateway Interface) scripts. Enabling CGI scripts would typically involve configuring Apache to recognize and execute scripts in response to specific HTTP requests. This capability is often used for dynamic content generation on websites.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads