Enable Apache CGI on Ubuntu 24.04
You enable Apache’s Common Gateway Interface (CGI) on Ubuntu 24.04 by activating the `cgid` module and configuring your Apache virtual host settings.
CGI allows your Apache web server to execute external scripts, such as those written in Perl or Python, to generate dynamic web content. This method goes beyond static HTML by enabling server-side processing of requests.
For instance, you’ll need to ensure the `cgid` module is present and enabled within your Apache configuration. This setup is crucial for running scripts that interact with your website’s backend logic.
Activate the `cgid` module with `sudo a2enmod cgid` and restart Apache. Place your CGI scripts in `/usr/lib/cgi-bin` or configure a custom directory in `/etc/apache2/conf-available/`. Make scripts executable and reload Apache.
Enable the Apache CGI module
Turning on the Apache CGI module is the first step to running CGI scripts with Apache on Ubuntu 24.04.
sudo a2enmod cgid
sudo systemctl restart apache2
After enabling, CGI scripts execute in the [/usr/lib/cgi-bin] directory by default. You must add your script to this directory to execute it.
For example, if you have a Perl script named [index.cgi], you’d place it in the /usr/lib/cgi-bin/ directory.
Client requests sent to this URL http://localhost/cgi-bin/index.cgi will execute the script.
This is the default behavior.
If you want to change the default behavior or place your CGI scripts elsewhere, create an Apache configuration file and define your settings.
For example, if you want to store your CGI scripts in this directory [/var/www/html/custom-cgi] instead, you’ll create an Apache configuration file and specify that new location.
First, create the CGI directory by running the command below.
sudo mkdir /var/www/html/custom-cgi
Next, run the command below to create an Apache configuration file.
sudo nano /etc/apache2/conf-available/custom-cgi.conf
Then, copy the block below, paste it into the file, and save.
<Directory "/var/www/html/custom-cgi">
Options +ExecCGI
AddHandler cgi-script .cgi .pl .py .rb
</Directory>
Enable the configuration by running the command below.
sudo a2enconf custom-cgi.conf
sudo systemctl reload apache2
Test Apache CGI execution
Once you’ve set up Apache CGI on Ubuntu 24.04, it’s time to test if your scripts are running correctly.
Run the command below to create a blank CGI index file.
sudo nano /var/www/html/custom-cgi/index.cgi
Copy the script below, paste it into the file, and save.
#!/usr/bin/python3
print("Content-type: text/htmln")
print("<html>n<body>")
print("<p style="width: 100%; font-weight: bold; font-size: 60px; text-align: center;">")
print("CGI is Enabled!!!")
print("</p>")
print("</body>n</html>")
Make the file executable by running the command below.
sudo chmod 755 /var/www/html/cgi-enabled/index.cgi
Now, open your browser and browse the script.
http://example.com/custom-cgi/index.cgi

That should do it!
Conclusion:
Enabling the Apache CGI module on Ubuntu 24.04 lets you create dynamic and interactive web applications. You can successfully run CGI scripts on your server by following the steps outlined here.
- CGI scripts facilitate interactive features on websites, enhancing user experience.
- Enabling the CGI module on Apache involves simple commands and configuration adjustments.
- The default CGI script directory is
/usr/lib/cgi-bin, but you can create custom directories. - Familiarity with Apache configuration files is essential for customizing your CGI setup.
- Testing your CGI setup ensures everything works as expected before deploying to a live environment.
With these steps and insights, you’ll be well-equipped to use CGI in your web development projects!
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!