What is Apache CGI?

CGI (Common Gateway Interface) is an early standard for interfacing external applications with web servers. With Apache HTTP Server, CGI scripts can be written in any language (like Perl, Python, Bash, or C) and executed when a client requests a specific resource. This is useful for dynamic content generation, legacy script support, and lightweight web utilities.

On Ubuntu 24.04 LTS, Apache supports CGI using the mod_cgi or mod_cgid module. While CGI has been largely replaced by more modern technologies (PHP-FPM, FastCGI, application servers), it’s still relevant for scripting tasks, legacy apps, or educational environments.


Key Features of Apache CGI

  • Language-agnostic: Write scripts in any language (Bash, Perl, Python, etc.)
  • Simple architecture: Each request spawns a new process to handle input/output
  • Full Apache integration: Works alongside virtual hosts, authentication, logging, and other modules
  • Secure when sandboxed: Ideal for isolated script execution environments

Use Cases

  • Running legacy web scripts or educational tools
  • Building lightweight command-line interface (CLI) to web bridges
  • Testing small utilities or tools via a web interface
  • Integrating Perl/Python shell scripts into intranet dashboards

How Apache Handles CGI on Ubuntu

There are two modules:

  • mod_cgi: Used for non-threaded MPM (e.g., prefork)
  • mod_cgid: Used for threaded MPMs (e.g., event, which is default on Ubuntu)

Ubuntu 24.04 usually uses mod_cgid, since Apache runs with the event MPM by default.


Security Tips

  • Avoid running CGI scripts with unnecessary privileges
  • Sanitize all user input to prevent command injection
  • Disable CGI in directories with public write access
  • Use AppArmor or chroot for script sandboxing
  • Monitor logs (/var/log/apache2/error.log) for suspicious activity

CGI vs Other Web Execution Models

FeatureCGIFastCGIPHP-FPMmod_php
Execution ModelNew process per requestPersistent processesPersistent processesEmbedded interpreter
PerformanceLow (due to process spawn)HighHighMedium
Language SupportAny executablePHP (mostly)PHP onlyPHP only
Use CaseLegacy, scriptsModern high-performance appsPHP websitesSimple PHP setups

Apache CGI on Ubuntu 24.04 is a functional and flexible way to run dynamic scripts for basic web applications, prototypes, or legacy systems. While it’s not suited for high-performance needs, it remains a valuable tool in environments where simplicity, scripting flexibility, or legacy compatibility is required.

Ubuntu 24.04 offers full support for Apache CGI through mod_cgid, with modern security tools and easy package management to make setup and maintenance straightforward.


Step 1: Deploy a Clean VPS on Shape.Host

Go to https://shape.host and sign in.

Click Create, then choose Instance.

Shape.host Resources dashboard with Create > Instance highlighted.

Pick a server location close to your users.

Shape.host instance form with the data center location selector highlighted.

Select Ubuntu 24.04 (64-bit) as your OS.

Choose a plan with 2 or more CPUs, 4 GB RAM, and at least 20 GB SSD.

Shape.host instance form with the operating system image selector highlighted.

Hit Create Instance to launch your VPS.

Shape.host instance form with the Create Instance button highlighted.

Find your IP address under the Resources section — you’ll use it to connect via SSH.

Shape.host Resources dashboard with the server IP address highlighted.

Step 2: Connect to Your Server

Linux/macOS:

ssh root@your_server_ip

Windows:
Use PuTTY, enter your IP, and log in as root.


Step 3: Update Your Package List

apt update

Makes sure all packages available for installation are up to date.

Terminal showing APT package management commands and output.

Step 4: Install Apache Web Server

apt install apache2

This installs the Apache HTTP server.

Terminal showing APT package management commands and output.

Step 5: Allow Web and SSH Traffic Through Firewall

ufw allow 'Apache Full'
ufw allow ssh
ufw enable

Opens ports for Apache and SSH in the system firewall.

Terminal showing firewall configuration commands and rules.

Step 6: Enable the CGI Module

a2enmod cgid
systemctl restart apache2

Turns on Apache’s CGI module and restarts the server to apply changes.

Terminal showing systemd service management commands or output.

Step 7: Create a Directory for Your CGI Scripts

mkdir /var/www/html/custom-cgi

This directory will hold your CGI scripts.


Step 8: Create a Custom CGI Configuration File

nano /etc/apache2/conf-available/custom-cgi.conf

Paste the following:

<Directory "/var/www/html/custom-cgi">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl .py .rb
</Directory>

This config allows CGI scripts in the /custom-cgi folder.

Terminal showing the GNU nano editor and configuration text.

Step 9: Enable the Custom CGI Config

a2enconf custom-cgi.conf
systemctl reload apache2

Applies the new config without restarting the entire Apache server.

Terminal showing systemd service management commands or output.

Step 10: Create a Test CGI Script

nano /var/www/html/custom-cgi/index.cgi

Paste this Python script:

#!/usr/bin/python3

print("Content-type: text/html\n")
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>")
Terminal showing the GNU nano editor and configuration text.

Step 11: Make the Script Executable

chmod 755 /var/www/html/custom-cgi/index.cgi

Grants permission for Apache to execute the CGI script.


Step 12: Open It in Your Browser

Visit:

http://your_server_ip/custom-cgi/index.cgi

You should see: “CGI is Enabled!!!”

Browser displaying the CGI is Enabled test message.

Apache is now configured to serve CGI scripts on Ubuntu 24.04, using Python, Perl, or other supported interpreters.


This guide was built and tested on a Shape.Host Cloud VPS — the perfect home for lightweight web apps and custom server setups.

  • Full root access
  • Fast SSD-powered virtual machines
  • Easy scaling for developers

Launch your server today at https://shape.host