Skip to content
Follow
Ubuntu Linux

Enable Apache CGI on Ubuntu 24.04

Richard
Written by
Richard
Feb 20, 2025 Updated Sep 15, 2026 3 min read
Enable Apache CGI on Ubuntu 24.04
Enable Apache CGI on Ubuntu 24.04

Apache CGI (Common Gateway Interface) is a built-in feature in Apache version 2.4.57 on Ubuntu 24.04 that lets your web server run interactive scripts and handle form inputs. This interface acts as a translator between the server and external programs, making it possible to connect your site to databases.

Advertisement

Turn on the cgid module and update your site settings to get everything working on your server.

⚡ Quick Answer

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.

Advertisement

Enable the Apache CGI module

To enable Apache CGI Ubuntu 24.04 support, you need to turn on the built-in cgi module so your web server can process dynamic scripts. Open your terminal and run sudo a2enmod cgi, then restart the web server by running sudo systemctl restart apache2. Once you finish these steps, your server will look for and run any scripts you place inside the default /usr/lib/cgi-bin folder.

sudo a2enmod cgid
sudo systemctl restart apache2

Once enabled, CGI scripts run from the [/usr/lib/cgi-bin] directory by default. Place your script here for execution.

For instance, a Perl script named [index.cgi] goes into the /usr/lib/cgi-bin/ directory.

Client requests sent to the URL http://localhost/cgi-bin/index.cgi execute the script.

Advertisement

This is the default behavior.

If you need to change this default or use a different directory for your CGI scripts, create a custom Apache configuration file and define your settings.

For example, if you want to store your CGI scripts in the [/var/www/html/custom-cgi] directory instead, create an Apache configuration file and specify that new location.

First, create the CGI directory by running the command below.

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

Test your CGI scripts after setting up Apache CGI on Ubuntu 24.04 to confirm they work correctly. Create a file named index.cgi in the /var/www/html/custom-cgi/ folder. Use sudo nano /var/www/html/custom-cgi/index.cgi to create and edit the file, then paste your script. Make the file executable with the following command.

Advertisement

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>")
⚠️WarningMake the file executable by running the command below.
sudo chmod 755 /var/www/html/cgi-enabled/index.cgi

Now, open your browser and navigate to the script.

Advertisement

http://example.com/custom-cgi/index.cgi

Apache CGI enabled
Apache CGI enabled

That completes the setup.

Conclusion:

Enabling the Apache CGI module on Ubuntu 24.04 lets you create dynamic and interactive web applications. Following the steps outlined here allows you to successfully run CGI scripts on your server.

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

Following these steps equips you to use CGI in web development projects, enabling dynamic content generation through scripts.

Was this guide helpful?

Was this helpful?
Richard

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.

Advertisement

📚 Related Tutorials

How to Install Apache ActiveMQ on Ubuntu Linux
Ubuntu Linux How to Install Apache ActiveMQ on Ubuntu Linux
How to Install Apache Cassandra on Ubuntu Linux
Ubuntu Linux How to Install Apache Cassandra on Ubuntu Linux
How to Install UVdesk with Apache on Ubuntu Linux
CMS How to Install UVdesk with Apache on Ubuntu Linux
How to Install ProjectSend with Apache on Ubuntu Linux
Ubuntu Linux How to Install ProjectSend with Apache on Ubuntu Linux

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *